Executing Remote Script

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
PXL_Posh
Posts: 18
Joined: Wed Jan 10, 2018 9:37 am

Executing Remote Script

Post by PXL_Posh » Thu Apr 12, 2018 6:45 am

Code: Select all

$RemotePC = "Local-PC"
Invoke-Command -ComputerName $RemotePC -ScriptBlock {param($Arg0,$Arg1) Invoke-Expression "C:\Scripts\foo.ps1 -arg0 $Arg0 -arg1 $Arg1"} -ArgumentList $Arg0,$Arg1
SapienHelp.ps1
(1.04 KiB) Downloaded 3 times
Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list..
+ CategoryInfo : InvalidOperation: (Location found:
{0}:String) [], RuntimeException
+ FullyQualifiedErrorId : FormatError
+ PSComputerName : Local-PC
This is what I get and it happens right where the script tries to iterate through the folder progession..

The Do{ }Until( ) statement
This script works when launched from the source system.
The script also works when launched directly from Local-PC ( the target system ).

It only fails when run as a remote command.

Thank you

User avatar
jvierra
Posts: 12094
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Executing Remote Script

Post by jvierra » Thu Apr 12, 2018 6:52 am

You don't need or want "Invoke-Expression".

Code: Select all

$RemotePC = 'Local-PC'
$sb = {
param($Arg0,$Arg1)
    C:\Scripts\foo.ps1 -arg0 $Arg0 -arg1 $Arg1
} 
Invoke-Command -ComputerName $RemotePC -ScriptBlock $sb -ArgumentList $Arg0,$Arg1

User avatar
PXL_Posh
Posts: 18
Joined: Wed Jan 10, 2018 9:37 am

Re: Executing Remote Script

Post by PXL_Posh » Mon Apr 16, 2018 10:11 am

This issue turns out to be a credentials issue.
While I agree the process for sending the command to the remote system is better handled by your example I still end up with the same results.

The error listed above is due to not having permissions to access the NAS.

Any ideas on how I can get my current user credentials to authorize or authenticate against the NAS?

Enter-PSSession does have the -EnableNetworkAccess switch..

User avatar
jvierra
Posts: 12094
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Executing Remote Script

Post by jvierra » Mon Apr 16, 2018 10:18 am

If you don't have permissions on a NAS then you cannot gain access with a PsSession as a NAS is not a PsRemotable device except if it is a Windows NT based NAS and has WsMan enabled. For all others you must have been granted the correct permissions to access the system.

These are the most obvious issues you will have to resolve.

User avatar
PXL_Posh
Posts: 18
Joined: Wed Jan 10, 2018 9:37 am

Re: Executing Remote Script

Post by PXL_Posh » Mon Apr 16, 2018 11:04 am

The NAS is Windows NT based.
If I RDP to the remote system, from that remote system I can UNC to any path on the NAS without additional credentials.

I tried changing the script from robocopy to copy-item but I don't think copy-item will do remote computer to remote computer. None of the examples in -FromSession or -ToSession suggest this is possible.

I'm throwing spaghetti at the wall at this point trying to see what sticks.

I can run the entire script from here and everything works without issue but these files are VERY far away. It can take 45min to copy running the copy portion of the script from here and seconds if I run the copy while logged onto the remote system.

The remote system is on the same FQDN as the NAS.

User avatar
jvierra
Posts: 12094
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Executing Remote Script

Post by jvierra » Mon Apr 16, 2018 11:07 am

RC can copy UNC paths easily.
If the NAS does not have WsMan enabled or if you are no an admin on the NAS then you cannot use a remote session.

RDP has nothing to do with remoting.

User avatar
PXL_Posh
Posts: 18
Joined: Wed Jan 10, 2018 9:37 am

Re: Executing Remote Script

Post by PXL_Posh » Mon Apr 16, 2018 12:11 pm

To provide clarity ( because I'm confused ).

From a remote system named Remote-PC.contoso.int I am able to copy files from \\NAS-001.contoso.int\folder-001\file1.txt to \\NAS-001.contoso.int\folder-002\ using RoboCopy or xcopy or anything I try, it all works. I'm using FQDN where ever it applies to a path.

From a local system named Local-PC.contoso.int I am able to perform the same process ( it just takes far longer ).

My Goal: launch the process to call the copy from Local-PC such that the operation of the copy is natively run from Remote-PC.

Does this apply to what you said about the NAS and WSMan?

User avatar
jvierra
Posts: 12094
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Executing Remote Script

Post by jvierra » Mon Apr 16, 2018 12:17 pm

Log into the remote system and run the copy. Without WsMan there is no other option.

User avatar
PXL_Posh
Posts: 18
Joined: Wed Jan 10, 2018 9:37 am

Re: Executing Remote Script

Post by PXL_Posh » Mon Apr 16, 2018 12:24 pm

Bummer, ok thanks!

User avatar
PXL_Posh
Posts: 18
Joined: Wed Jan 10, 2018 9:37 am

Re: Executing Remote Script

Post by PXL_Posh » Mon Apr 16, 2018 4:29 pm

Issue resolved

Local Script:
Windows Authentication Popup to Variable $Credential
$Credential passed along with other args to Remote Script/PC

On the Remote Script/PC:
Mapped a PSDrive -Name X and called Variable $Drive1 = "X:\" -Credential $Credential ( not persistent )
Since this script looks in multiple locations two drives were mapped.

Interestingly using domain\ successfully mapped the first NAS but failed the second.

I feared removing domain\ from credential would reverse the circumstance but this was not the case. Both locations accepted it.

Lastly, I dropped RoboCopy and went to copy-item (no creds, drive already mapped).
I haven't tested with RoboCopy but I'm sure it would work now that drives are mapped, just part of trying to figure out how to pass those creds along. Also using copy-item really shortened my script.

Post Reply