0

救命!
在我的服务器上,我正在运行 hMailServer,并且该服务使用本地系统帐户。

我需要将文件复制到另一台机器。所以我有一个脚本,它将使用 cmdkey.exe 保存凭据,然后复制文件。

如果我在登录服务器时自己运行此功能(在独立的 vbs 文件中),它可以工作,但我是管理员。但是,如果我让 hMailServer 服务运行此函数,该函数运行,但它总是说目标不存在。

请注意,我已注释掉凭据的删除。如果我去服务器并运行,cmdkey /list我会看到从未设置凭据,这意味着命令失败。这意味着凭据的第一次设置也可能失败,这就是为什么“objFSO”找不到目录的原因。

同样,如果我将所有这些放在一个单独的文件中并test.vbs通过双击该文件来运行它,它就可以工作。但是如果我在 hMailServer 中使用它,它就会失败。

我想这意味着 hMailServer (本地系统帐户)无权设置凭据?我怎样才能让它工作?

option explicit

dim SPcopyMessage
SPcopyMessage = CopyFileToRemoteMachine("SERVER", "mydomain\username", "password", "c:\test2.txt", "\\SERVER\somefolder\otherfolder")
MsgBox SPcopyMessage


function CopyFileToRemoteMachine(whatMachine, whatUsername, whatPassword, whatSourceFile, whatDestination)
    dim errormessage, CredentialCreate, CredentialDelete
    errormessage    = "Sharepoint Mail Delivered"
    CredentialCreate = "cmd.exe /c cmdkey /add:" & whatMachine & " /user:" & whatUsername & " /pass:" & whatPassword

    Dim objShell, objFSO
    Set objShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    CALL objShell.Run(CredentialCreate, 0, True)        'add username to the credentials list

    If objFSO.FileExists(whatSourceFile) Then
          If objFSO.FolderExists(whatDestination) Then
            If Right(whatDestination, 1) <> "\" Then
                  whatDestination = whatDestination & "\"
            End If        
            objFSO.CopyFile whatSourceFile, whatDestination, True
          Else
                errormessage = "Destination does not exist: " & whatDestination
          End If
    Else
          errormessage = "Source file does not exist: " & whatSourceFile
    End If

    'CredentialDelete = "cmd.exe /c cmdkey /delete:" & whatMachine
    'CALL objShell.Run(CredentialDelete, 0, True)

    set objFSO = nothing
    set objShell = nothing

    CopyFileToRemoteMachine = errormessage
end function
4

1 回答 1

0

想了个办法!首先,我确保将目的地共享给 machine2 上的正确用户帐户。然后在 machine1 上制作脚本来映射网络驱动器,然后复制文件。只要 N 驱动器从未用于该机器上的其他任何东西,这将起作用。

这是代码是否对任何人有帮助!

function CopyFileToRemoteMachine(whatMachine, whatUsername, whatPassword, whatSourceFile, whatDestination)
    dim errormessage, mdrive
    errormessage    = "File successfully copied"

    mdrive = "N:"

    Dim objFSO, objNetwork
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject("Wscript.Network")

    If not objFSO.FileExists(mdrive)  Then
        objNetwork.MapNetworkDrive mdrive, whatDestination, False, whatUsername, whatPassword
    End If

    If Right(whatDestination, 1) <> "\" Then
          whatDestination = whatDestination & "\"
    End If  
    If objFSO.FileExists(whatSourceFile) Then
          If objFSO.FolderExists(whatDestination) Then  
            objFSO.CopyFile whatSourceFile, whatDestination, True
          Else
                errormessage = "Destination does not exist: " & whatDestination
          End If
    Else
          errormessage = "Source file does not exist: " & whatSourceFile
    End If

    objNetwork.RemoveNetworkDrive mdrive,TRUE,TRUE

    set objFSO = nothing
    set objNetwork = nothing

    CopyFileToRemoteMachine = errormessage
end function
于 2012-11-08T21:35:43.463 回答