Page 1 of 2

Executing Remote Script

Posted: Thu Apr 12, 2018 6:45 am
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
(1.04 KiB) Downloaded 40 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

Re: Executing Remote Script

Posted: Thu Apr 12, 2018 6:52 am
by jvierra
You don't need or want "Invoke-Expression".

Code: Select all

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

Re: Executing Remote Script

Posted: Mon Apr 16, 2018 10:11 am
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..

Re: Executing Remote Script

Posted: Mon Apr 16, 2018 10:18 am
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.

Re: Executing Remote Script

Posted: Mon Apr 16, 2018 11:04 am
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.

Re: Executing Remote Script

Posted: Mon Apr 16, 2018 11:07 am
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.

Re: Executing Remote Script

Posted: Mon Apr 16, 2018 12:11 pm
by PXL_Posh
To provide clarity ( because I'm confused ).

From a remote system named I am able to copy files from \\\folder-001\file1.txt to \\\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 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?

Re: Executing Remote Script

Posted: Mon Apr 16, 2018 12:17 pm
by jvierra
Log into the remote system and run the copy. Without WsMan there is no other option.

Re: Executing Remote Script

Posted: Mon Apr 16, 2018 12:24 pm
by PXL_Posh
Bummer, ok thanks!

Re: Executing Remote Script

Posted: Mon Apr 16, 2018 4:29 pm
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.