无法将变量传递给调用命令

Unable to pass variable to Invoke-command

这是我第一次使用工作流,谁能解释一下代码有什么问题吗?

Powershell 版本为 5.1

$Script = {
    return(Get-Service WINRM).Status
}

workflow pushupdate{
##Select OUs
$OUs=
"OU=Workstations,DC=contoso,DC=com",
"OU=Notebooks,DC=contoso,DC=com"

        foreach -parallel ($computer in ($Ous | foreach { Get-ADComputer -Filter {enabled -eq $true} -SearchBase $_} | Select Name)) {
        if ((Test-Connection $computer.name -Quiet) -eq "True") {
            Write-Output "Running update on:" $computer.name

            InlineScript {
                Invoke-Command -ComputerName $computer.name -Script $Script -Verbose
                }
            }
        else{
            Write-Output $computer.name "unreachable!"
        }
    }
}

pushupdate

我一直收到错误消息:

Invoke-Command : Cannot validate argument on parameter 'ScriptBlock'. The argument is null. Provide a valid value for the argument, and then try running the command again. At pushupdate:245 char:245

在 InlineScript 块之外定义的变量对于 Invoke-Command cmdlet 是未知的,除非您将它们用作 $using:<varname>。 但是,您似乎无法使用实际上是脚本块的变量来做到这一点。这需要在 InlineScript 本身 中定义:

workflow pushupdate{
    # Select OUs
    $OUs = "OU=Workstations,DC=contoso,DC=com", "OU=Notebooks,DC=contoso,DC=com"
    # get a string array of computerNames
    $computers = ( $Ous | ForEach-Object { Get-ADComputer -Filter "Enabled -eq 'True'" -SearchBase $_ } ).Name

    foreach -parallel ($computer in $computers) {
        if (Test-Connection -ComputerName $computer -Quiet -Count 1) {
            Write-Output "Running update on:" $computer
            InlineScript {
                # define the scriptblock here
                $script = {(Get-Service WINRM).Status}
                Invoke-Command -ComputerName $using:computer -ScriptBlock $Script -Verbose
            }
        }
        else{
            Write-Output "$computer unreachable!"
        }
    }
}

pushupdate