title: How to run command to set credentials from within vbs script using system account?
body: <p>Salvete!<br>
  On my server I am running hMailServer, and that service uses the local system account.</p>
  
  <p>I need to copy a file to another machine.  So I have this a script that will use cmdkey.exe to save the credentials and then copy the file.</p>
  
  <p>If I run this function myself (in a standalone vbs file) whilst logged into the server, it works, but I am admin.
  However, if I let the hMailServer service run this function, the function runs, but it always says the destination does not exist.  </p>
  
  <p>Notice I have commented out the deletion of the credentials.  If I go to the server and run <code>cmdkey /list</code> I see that the credentials were never set, which means the command failed.  That means the first setting of the credentials probably failed too, which is why 'objFSO' cannot find the directory.</p>
  
  <p>Again, if I put all this in a separate file and run it as <code>test.vbs</code> by double-clicking the file, it works.  But if I use it from within hMailServer, it fails.</p>
  
  <p>I suppose this means the hMailServer (local system account) doesn't have rights to set credentials?  How do I get this to work?</p>
  
  <pre><code>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:" &amp; whatMachine &amp; " /user:" &amp; whatUsername &amp; " /pass:" &amp; 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) &lt;&gt; "\" Then
                    whatDestination = whatDestination &amp; "\"
              End If        
              objFSO.CopyFile whatSourceFile, whatDestination, True
            Else
                  errormessage = "Destination does not exist: " &amp; whatDestination
            End If
      Else
            errormessage = "Source file does not exist: " &amp; whatSourceFile
      End If
  
      'CredentialDelete = "cmd.exe /c cmdkey /delete:" &amp; whatMachine
      'CALL objShell.Run(CredentialDelete, 0, True)
  
      set objFSO = nothing
      set objShell = nothing
  
      CopyFileToRemoteMachine = errormessage
  end function
  </code></pre>