none
Invoke-command RRS feed

  • Question

  • when I ran the following command at prompt

      

    Invoke-Command -ComputerName serverName -ScriptBlock { Get-Childitem -Path c:\windows\ -Recurse -Force | Where-Object {$_.name -eq "file.dll"} }

    I get what I am expecting.... but I script the above code such as

    $list=invoke-command-computername$computer-ErrorVariableerr-script{ Get-Childitem-Path$filePath-recurse-Force|Where-Object{$_.name -eq$file}};

    I return nothing and $lastexitcode is nothing too.

    I add the credential option but did not work too.....any suggestion


    michael john ocasio

    Friday, May 9, 2014 7:34 PM

Answers

  • Think I see the issue, using Invoke-Command you're running the command on the remote machine, and that machine doesn't have access to the variables already configured on the local machine, so doesn't know what $file is, and that's why the result you're getting is blank (since nothing matches). You need to adjust the script to pass that value to the remote machine like this :

    $list=invoke-command -computername $computer -script { 
    	param($passedfilepath,$passedfile)
    	Get-Childitem -Path $passedfilePath -recurse -Force|Where-Object{$_.name -eq $passedfile}
    } -ArgumentList $filepath,$file
    so the param entries are what you use in the script remotely, and the argumentlist ones are the corresponding local variables you're passing to them.

    • Marked as answer by mjocasio23 Friday, May 9, 2014 8:31 PM
    Friday, May 9, 2014 8:25 PM

All replies

  • Think I see the issue, using Invoke-Command you're running the command on the remote machine, and that machine doesn't have access to the variables already configured on the local machine, so doesn't know what $file is, and that's why the result you're getting is blank (since nothing matches). You need to adjust the script to pass that value to the remote machine like this :

    $list=invoke-command -computername $computer -script { 
    	param($passedfilepath,$passedfile)
    	Get-Childitem -Path $passedfilePath -recurse -Force|Where-Object{$_.name -eq $passedfile}
    } -ArgumentList $filepath,$file
    so the param entries are what you use in the script remotely, and the argumentlist ones are the corresponding local variables you're passing to them.

    • Marked as answer by mjocasio23 Friday, May 9, 2014 8:31 PM
    Friday, May 9, 2014 8:25 PM
  • Right I forgot the arguments to the script block... good catch

    michael john ocasio

    Friday, May 9, 2014 8:31 PM