Executing Remote Script

Ask your 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.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
PXL_Posh
Posts: 40
Last visit: Thu Nov 07, 2019 5:43 am

Executing Remote Script

Post by PXL_Posh »

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 135 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Executing Remote Script

Post by jvierra »

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: 40
Last visit: Thu Nov 07, 2019 5:43 am

Re: Executing Remote Script

Post by PXL_Posh »

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..
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Executing Remote Script

Post by jvierra »

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: 40
Last visit: Thu Nov 07, 2019 5:43 am

Re: Executing Remote Script

Post by PXL_Posh »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Executing Remote Script

Post by jvierra »

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: 40
Last visit: Thu Nov 07, 2019 5:43 am

Re: Executing Remote Script

Post by PXL_Posh »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Executing Remote Script

Post by jvierra »

Log into the remote system and run the copy. Without WsMan there is no other option.
User avatar
PXL_Posh
Posts: 40
Last visit: Thu Nov 07, 2019 5:43 am

Re: Executing Remote Script

Post by PXL_Posh »

Bummer, ok thanks!
User avatar
PXL_Posh
Posts: 40
Last visit: Thu Nov 07, 2019 5:43 am

Re: Executing Remote Script

Post by PXL_Posh »

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.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked