我有一个脚本,我可以通过调用命令远程运行

Invoke-Command -ComputerName (Get-Content C:\Scripts\Servers.txt) `
               -FilePath C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1

只要我使用默认参数,它工作正常。然而,该脚本有2名为[开关]参数(-Debug和 - 清除)

如何通过经由调用命令的切换参数?我已经试过-ArgumentList,但我发现错误,所以我必须有语法错误或东西。任何帮助感激。

有帮助吗?

解决方案

-ArgumentList是基于使用与脚本块命令,如:

Invoke-Command -Cn (gc Servers.txt) {param($Debug=$False, $Clear=$False) C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 } -ArgumentList $False,$True

当您使用-File称之为它仍然通过像一个哑splatted阵列的参数。我已经提交了的特征请求以具有添加到命令(请投即向上)。

所以,你有两个选择:

如果你有一个看起来像这样,在网络位置访问远程机器的脚本(注意,-Debug暗示,因为当我使用Parameter属性,脚本获取列出CmdletBinding含蓄,因此,所有的常用参数):

param(
   [Parameter(Position=0)]
   $one
,
   [Parameter(Position=1)]
   $two
,
   [Parameter()]
   [Switch]$Clear
)

"The test is for '$one' and '$two' ... and we $(if($DebugPreference -ne 'SilentlyContinue'){"will"}else{"won't"}) run in debug mode, and we $(if($Clear){"will"}else{"won't"}) clear the logs after."

而不会挂在$Clear的意思......如果你想调用,您既可以使用以下Invoke-Command语法:

icm -cn (gc Servers.txt) { 
    param($one,$two,$Debug=$False,$Clear=$False)
    C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 @PSBoundParameters
} -ArgumentList "uno", "dos", $false, $true

在的那一个,我复制有关在脚本块所以我可以通过值的所有我关心的参数。如果我可以硬编码它们(这是我实际上做了),没有必要做到这一点,利用PSBoundParameters,我可以通过我需要的人。在第二个例子下面我将通过$清除一个,只是为了演示如何传递开关参数:

icm -cn $Env:ComputerName { 
    param([bool]$Clear)
    C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 "uno" "dos" -Debug -Clear:$Clear
} -ArgumentList $(Test-Path $Profile)

另一种选择

如果脚本是在本地计算机上,并且你不想改变的参数是位置,还是要指定是常见的参数(这样你就无法控制他们),你会希望得到的参数该脚本的内容并将其嵌入在脚本块的:

$script = [scriptblock]::create( @"
param(`$one,`$two,`$Debug=`$False,`$Clear=`$False)
&{ $(Get-Content C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 -delimiter ([char]0)) } @PSBoundParameters
"@ )

Invoke-Command -Script $script -Args "uno", "dos", $false, $true

的PostScript:

如果你真的需要在脚本名称的变量传递,你会怎么做取决于变量是本地或远程定义。在一般情况下,如果有一个可变$Script或与脚本的名称的环境变量$Env:Script,可以与呼叫操作员执行它(&):&$Script&$Env:Script

如果它是一个的远程计算机上的已定义过的环境变量,这是所有有给它。如果它是一个的本地的变量,那么你就必须把它传递给远程脚本块:

Invoke-Command -cn $Env:ComputerName { 
    param([String]$Script, [bool]$Clear)
    &$Script "uno" "dos" -Debug -Clear:$Clear
} -ArgumentList $ScriptPath, $(Test-Path $Profile)

其他提示

我向该溶液中以动态地编写脚本块与[scriptblock]:Create

# Or build a complex local script with MARKERS here, and do substitutions
# I was sending install scripts to the remote along with MSI packages
# ...for things like Backup and AV protection etc.

$p1 = "good stuff"; $p2 = "better stuff"; $p3 = "best stuff"; $etc = "!"
$script = [scriptblock]::Create("MyScriptOnRemoteServer.ps1 $p1 $p2 $etc")
#strings get interpolated/expanded while a direct scriptblock does not

# the $parms are now expanded in the script block itself
# ...so just call it:
$result = invoke-command $computer -script $script

传递参数是非常令人沮丧,尝试各种方法,例如,点击 -arguments$using:p1等,并且这只是担任,没有任何问题所需的。

因为我控制的内容和它创建[scriptblock](或脚本文件)这样的字符串变量扩展,存在与“调用命令”咒语没有真正的问题。

(它不应该是困难的。:))

我怀疑它的一个新的功能,因为这个帖子被创建 - 通过使用$使用参数的脚本块:VAR。然后它的一个简单脑膜传递参数提供的脚本是已经在机器上或在公知的网络位置相对于机器一个

以主例子这将是:

icm -cn $Env:ComputerName { 
    C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 -one "uno" -two "dos" -Debug -Clear $Using:Clear
}

我需要的东西来调用脚本与命名参数。我们还没有使用的参数顺序定位和要求参数名称的政策。

我的方法类似于上述的那些,但得到与要调用的脚本文件的内容和发送包含参数和值的参数块。

之一的这个优点是可以任选地选择要发送到脚本文件允许非强制性参数为默认设置哪些参数。

假设有一个在具有以下参数块的临时路径称为“MyScript.ps1”脚本:

[CmdletBinding(PositionalBinding = $False)]
param
(
    [Parameter(Mandatory = $True)] [String] $MyNamedParameter1,
    [Parameter(Mandatory = $True)] [String] $MyNamedParameter2,
    [Parameter(Mandatory = $False)] [String] $MyNamedParameter3 = "some default value"
)

这是我怎么会调用这个脚本从另一个脚本:

$params = @{
    MyNamedParameter1 = $SomeValue
    MyNamedParameter2 = $SomeOtherValue
}

If ($SomeCondition)
{
    $params['MyNamedParameter3'] = $YetAnotherValue
}

$pathToScript = Join-Path -Path $env:Temp -ChildPath MyScript.ps1

$sb = [scriptblock]::create(".{$(Get-Content -Path $pathToScript -Raw)} $(&{
        $args
} @params)")
Invoke-Command -ScriptBlock $sb

我已经在很多场合下用这个和它的作品真的很好。 一件事,你偶尔需要做的就是把引号将参数值分配块。这始终是当存在在值空间的情况下。

e.g。此PARAM块用于调用的脚本副本的各种模块到由PowerShell中C:\Program Files\WindowsPowerShell\Modules使用的标准位置,其包含空格字符。

$params = @{
        SourcePath      = "$WorkingDirectory\Modules"
        DestinationPath = "'$(Join-Path -Path $([System.Environment]::GetFolderPath('ProgramFiles')) -ChildPath 'WindowsPowershell\Modules')'"
    }

希望这有助于!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top