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
}