Welcome Forums General PowerShell Q&A Invoke-Command

This topic contains 4 replies, has 3 voices, and was last updated by

1 year, 3 months ago.

  • Author
  • #83332

    Points: 0
    Rank: Member

    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 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

  • #83335

    Points: 2
    Rank: Member

    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:

    PowerShell: Bypass ExecutionPolicy to run downloaded scripts


  • #83339

    Points: 0
    Rank: Member

    not working like this...still same error message

  • #83341

    Points: 0
    Rank: Member

    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
    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

  • #83432

    Points: 711
    Helping Hand
    Rank: Major Contributor

    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'}

    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

    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 ..

The topic ‘Invoke-Command’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort