Author Posts

November 1, 2017 at 10:11 am

I am trying to run Invoke-Command from a server to execute a script on another server but I get error message
Invoke-Command -FilePath \\p1\c$\scripts\script1.ps1 -ComputerName UIASFS02

Security warning
Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your
computer. If you trust this script, use the Unblock-File cmdlet to allow the script to run without this warning
message. Do you want to run \\p1\c$\scripts\script1.ps1?
[D] Do not run [R] Run once [S] Suspend [?] Help (default is "D"): R
Invoke-Command : File \\p1\c$\scripts\script1.ps1 cannot be loaded because you opted not to run
this software now.
At line:1 char:1
+ Invoke-Command -FilePath \\p1\c$\scripts\script1.ps1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [Invoke-Command], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.InvokeCommandCommand

November 1, 2017 at 10:36 am

Hi Bill M,

You can either unblock the file specifically (using the Unblock-File cmdlet, or by right-clicking the file | Properties | Unblock) , or set the execution policy to bypass.

Have a look here:

https://4sysops.com/archives/powershell-bypass-executionpolicy-to-run-downloaded-scripts/

DanT

November 1, 2017 at 11:29 am

not working like this...still same error message

November 1, 2017 at 12:07 pm

PS C:\Users\makay> Invoke-Command -ComputerName tatrax1 -ScriptBlock '\\p1\c$\scripts\script1.ps1'
Invoke-Command : Cannot bind parameter 'ScriptBlock'. Cannot convert the
"\\p1\c$\scripts\script1.ps1" value of type "System.String" to type
"System.Management.Automation.ScriptBlock".
At line:1 char:54
+ ... B03 -ScriptBlock '\\p1\c$\scripts\script1.ps1'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeCommandCommand
Hmmm....need to be somehow converted or find out another way to remote run it

November 1, 2017 at 5:45 pm

You really need to clarify what your use case is and how you need to accomplish. SO, we need details on the use case / goal in order to assist.

However, based on what is here.

All scriptblocks must be enclosed in {}.
So, your syntax is not correct. Also, though you are doing this, in the script block, you are not telling it how to run.
So, adding the {} around your string, all that will come back is the string...

Invoke-Command -ComputerName 'UIASFS02' -ScriptBlock {'\\p1\c$\scripts\script1.ps1'}
\\p1\c$\scripts\script1.ps1

Even is you passed it this way...
Invoke-Command -ComputerName 'UIASFS02' -ScriptBlock {powershell '\\p1\c$\scripts\script1.ps1'}
... it will fail with...
\\p1\c$\scripts\script1.ps1 : The term '\\p1\c$\scripts\script1.ps1' is not recognized as the name of a
+ CategoryInfo : NotSpecified: (\\p1\c$\s... the name of a :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : UIASFS02

... Why, because that script file is not available for use in this fashion.

Secondly, this looks like you are calling a remote computer from your local workstation and using that computer to run a script on another computer. You cannot to this by default as this is a Kerberos double hop authentication issue.
See this article for details...
PowerShell Remoting Kerberos Double Hop Solved Securely
https://blogs.technet.microsoft.com/ashleymcglone/2016/08/30/powershell-remoting-kerberos-double-hop-solved-securely

So, even if you told this script how to run, you'd get this error, because of the double hop.
Invoke-Command -ComputerName UIASFS02 -ScriptBlock {Start-Process \\p1\c$\scripts\script1.ps1}
This command cannot be run due to the error: Access is denied.
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
+ PSComputerName : UIASFS02

Lastly, you have to be aware of how to call commands and remote scripts as there are special characters that you can use, that must be escaped before what follows them will be useful.

See the help files for ..
about_special_characters
'docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-5.1'
about_escape_characters
'docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_escape_characters?view=powershell-5.1'