Forum Discussion

SunPower's avatar
SunPower
Copper Contributor
Jul 09, 2021

Invoke-Command ScriptBlock issue

Hi All, am facing some issues when using invoke commamd.

Issue1: if I say invoke-command -computername abc -scriptblock{c:\abc\runprocess.cmd -create}

This is working fine.

But if I put c:\abc\runprocess.cmd -create in variable abcpath it’s not working as expected.

Command a I tried:

invoke-command -computername abc -scriptblock{$abcpath} this isn’t printing anything.

Command b I tried

invoke-command -computername abc -scriptblock{$args} -ArgumentList this isn’t printing c:\abc\runprocess.cmd -create as is but it’s not executing.

Please advice and any pointers will be helpful.

In summary I want to know how I can use variables in script block and such that command can be executed in machine -computerName I specified.
  • psophos's avatar
    psophos
    Brass Contributor

    SunPower 

    Does calling it like this work?:

    $abcPath = {
        c:\abc\runprocess.cmd -create
    }
    invoke-command -computername abc -scriptblock $abcPath 
    Note the lack of { } after the -scriptblock parameter.
    • SunPower's avatar
      SunPower
      Copper Contributor

      Thanks for your reply psophos but issue is not resolved.  Still same behavior - basically it's not displaying anything.

      • psophos's avatar
        psophos
        Brass Contributor
        Displaying or executing?

        I would not expect it to display anything as the code is being executed on the remote computer.

Resources