Question

I'm trying to execute code on a remote machine using invoke-command. Part of that method includes a ScriptBlock parameter, and I get the sense that I'm not doing something correctly.

First I tried to create a method in the script, which looked like this:

param([string] $filename)

function ValidatePath( $file, $fileType = "container" )
{
    $fileExist = $null
    if( -not (test-path $file -PathType $fileType) )
    {               
        throw "The path $file does not exist!"
        $fileExist = false
    }
    else
    {
        echo $filename found!
        $fileExist = true
    }
    return $fileExist
}


$responseObject = Invoke-Command -ComputerName MININT-OU9K10R
    -ScriptBlock{validatePath($filename)} -AsJob

$result = Receive-Job -id $responseObject.Id

echo $result

To call this, I would do .\myScriptName.ps1 -filename C:\file\to\test. The script would execute, but would not call the function.

Then I thought that maybe I should put the function into a new script. This looked like:

File 1:

$responseObject = Invoke-Command -ComputerName MININT-OU9K10R -ScriptBlock {
  .\file2.ps1 -filename C:\something } -AsJob

$result = Receive-Job -id $responseObject.Id

echo $result

File 2:

Param([string] $filename)

Neither of these approaches will execute the function and I'm wondering why; or, what I need to do to make it work.

function ValidatePath( $file, $fileType = "container" )
{
    $fileExist = $null
    if( -not (test-path $file -PathType $fileType) )
    {               
        throw "The path $file does not exist!"
        $fileExist = false
    }
    else
    {
        echo $filename found!
        $fileExist = true
    }
    return $fileExist
}
Was it helpful?

Solution

That's because Invoke-Command executes the code in the script block on the remote computer. The ValidatePath function is not defined on the remote computer, and the script file file2.ps1 does not exist there. There is nothing that gives the remote computer access to the code in the script that executes Invoke-Command or to files on the computer on which the script is running. You'd need to either copy file2.ps1 to the remote computer, or provide it a UNC path to a share on your computer where the file is available, or put the contents of the ValidatePath function in the script block. Be sure to change all instances of $file to $filename or vice versa and adapt the code to run interactively, for example you'd eliminate $fileExist and the return statement.

To put the path-validating code in the scriptblock that gets passed to the remote computer, you'd do something like this:

$scriptblock = @"
  if (-not (Test-Path $filename -PathType 'Container') ) {
    throw "The path $file does not exist!"
  } else {
    echo $filename found!
  }
"@

$responseObject = Invoke-Command -ComputerName MININT-OU9K10R -ScriptBlock{$scriptblock} -AsJob

N.B. Make sure the "@ is not indented. It must be at the beginning of the line.

BTW, although this is moot, what's the point of setting a variable immediately after a throw statement? Once you throw an error, the function terminates. $fileExist = false would never execute under any circumstances. You probably wanted to use Write-Error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top