Run a script located at a remote machine which inturn access files from a network share

by Sanjeev on January 30, 2011

I recently ran into the below issue.

I was trying to execute a powershell script located at a remote machine using the below command and it works great

invoke-command (powershell c:\temp\install.ps1) -computername box1 -credential (get-credential)

The above script in-turn copies files from a network share (using robocopy) – It fails complaining the location doesn’t exist. Note that box1 already had access to \\share1 and the script works fine when ran from the box itself, but fails when ran from a remote machine.

Solution 1: Using CredSSP.

Problem : Is not supported in Win 2003 and XP machines.

Solution 2: Run another ps1 script before executing the install.ps1 script to use credentials for the share.

net use \\share1\files password /user:username

Problem : I didn’t like this solution as the password is exposed as a clear text in the remote machine.

Solution 3 : Run the net use command from the local machine before executing install.ps1 command.

Invoke-command {powershell.exe net use \\share1\files password /user:username} -computername box1 -credential (get-credential)

Problem : Again I didn’t want to type the net use command everytime I had to trigger a remote powershell execution. Also I didn’t want to store then in a text file at my local workstation.

Solution 3: Adding the below line to the profile.ps1 file in the remote machine. Remember this doesn’t work if you are trying to trying to access a network resource in a different domain.

New-PSDrive SharedLocation FileSystem \\share1\files

Now everytime I invoke powershell to execute the script file “install.ps1″, the shared location can be accessed as a drive “SharedLocation”.

Sweet! My passwords are safe. In case if you want to access a network share located in a different machine, you may find this post useful.

{ 4 comments… read them below or add one }

mushoku November 22, 2011 at 5:18 PM

I’m trying your solution and not having any success. I’ve added the New-PSDrive lines to my $profile on the remote machine, as well as %windir%\system32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 . Even so, I am unable to see the PSDrives when executing Invoke-Command from my system.


Sanjeev December 3, 2011 at 10:42 AM

What error are you getting ? Are you trying to access two machine in the same domain and does the user account you are using has access to the target machine as an administrator?


Tom January 21, 2012 at 7:48 AM

Found your Post via Stackoverflow, hope your solution will solve my problems ;-) Thanks for sharing!


kiquenet May 30, 2012 at 5:07 AM

Any final solution with full source code ?

I want call to powershell script (in local) that run another script located in the same local machine, but run it in remote machine,

and I want call to powershell script (in local) that run another script located in remote machine, but run it in remote machine,



Leave a Comment

Previous post:

Next post: