PowerShell的脚本块不执行 [英] Powershell ScriptBlock Not Executing

查看:256
本文介绍了PowerShell的脚本块不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行code。使用远程计算机调用命令上。该方法的部分包括脚本块参数,我得到的,我不是在做正确的事情感。

首先,我试图创建一个方法脚本,它是这样的:

 参数([字符串] $文件名)

功能ValidatePath($文件,$的fileType =容器)
{
    $ fileExist = $空
    如果(-not(测试路径$文件-PathType $的fileType))
    {
        抛出路径$文件不存在!
        $ fileExist = FALSE
    }
    其他
    {
        回声$找到的文件名!
        $ fileExist =真
    }
    回报$ fileExist
}


$ responseObject =调用命令-ComputerName MININT,OU9K10R
    -ScriptBlock {validatePath($文件名)} -AsJob

$结果=接收在职-id $ responseObject.Id

回声$结果
 

要调用这个,我会做 \ myScriptName.ps1 -filename C:\文件\到\测试。该脚本将执行,但不会调用该函数。

后来我想,也许我应该把功能到一个新的脚本。这看起来像:

文件1:

  $ responseObject =调用命令-ComputerName MININT-OU9K10R -ScriptBlock {
  \ file2.ps1 -filename C:\东西} -AsJob

$结果=接收在职-id $ responseObject.Id

回声$结果
 

文件2:

 参数([字符串] $文件名)
 

无论这些方法将执行功能,我不知道为什么;或者,我需要做的,使其工作。

 函数ValidatePath($文件,$的fileType =容器)
{
    $ fileExist = $空
    如果(-not(测试路径$文件-PathType $的fileType))
    {
        抛出路径$文件不存在!
        $ fileExist = FALSE
    }
    其他
    {
        回声$找到的文件名!
        $ fileExist =真
    }
    回报$ fileExist
}
 

解决方案

这是因为调用命令执行code。在远程计算机上的脚本块。该ValidatePath功能在远程计算机上定义和脚本文件的 file2.ps1 不存在那里。没有什么给人以code远程计算机访问在执行调用命令的脚本,或者在其上运行脚本的计算机上的文件。你需要可以复制的 file2.ps1 到远程计算机,或者提供它的UNC路径到您的计算机上的共享该文件可用,或将ValidatePath函数的内容在脚本块。一定要改变的 $文件所有实例为 $文件名或反之亦然,并调整code以交互方式运行,比如你想消除的 $ fileExist 返回语句。

要放的路径验证code在被传递到远程计算机的脚本块,你会做这样的事情:

  $脚本块= @
  如果(-not(测试的路径$文件名-PathType'集装箱')){
    抛出路径$文件不存在!
  } 其他 {
    回声$找到的文件名!
  }
@

$ responseObject =调用命令-ComputerName MININT-OU9K10R -ScriptBlock {$脚本块} -AsJob
 

NB 确保在@ 不缩进。它的必须的是,在该行的开头。

顺便说一句,虽然这是没有实际意义,什么是设置变量点后立即抛出语句?一旦你抛出一个错误,函数终止。 $ fileExist = FALSE 永远不会在任何情况下执行。你可能想使用写入错误

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
}

解决方案

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.

这篇关于PowerShell的脚本块不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆