Powershell |第二个调用:变量

发布于 2025-02-08 10:17:32 字数 641 浏览 2 评论 0原文

我有一个简单的问题。我想在invoke-command中使用变量,然后将其传递给第二个命令命令。在第二个调用命令中,变量为空,

这是我的代码:

$WaitSeconds = 1234

Invoke-Command -ComputerName $remote -Credential $cred -ScriptBlock {
    $computer = dsquery computer "DC=domain,DC=local" -o rdn 
    $computers = $computer -replace ('"', '') 
    write-host $computers

    foreach ($computer in $computers) {
        if ($computer -notmatch "AZUREADSSOACC")
        {
            Invoke-Command -ComputerName $computer -Credential $using:cred -ScriptBlock {
                #### here script
                shutdown -s -t $using:waitseconds
            }
        }
    }
}

i have a simple question. I want to use a variable in an invoke-command and pass this to the second invoke-command. In the second invoke-command the variable is empty

here is my code:,

$WaitSeconds = 1234

Invoke-Command -ComputerName $remote -Credential $cred -ScriptBlock {
    $computer = dsquery computer "DC=domain,DC=local" -o rdn 
    $computers = $computer -replace ('"', '') 
    write-host $computers

    foreach ($computer in $computers) {
        if ($computer -notmatch "AZUREADSSOACC")
        {
            Invoke-Command -ComputerName $computer -Credential $using:cred -ScriptBlock {
                #### here script
                shutdown -s -t $using:waitseconds
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哽咽笑 2025-02-15 10:17:32

在第二个调用命令中,变量为空

这是因为该变量在调用会话中不存在(第一个Invoke-command呼叫的执行上下文)。

确保首先指示PowerShell将变量复制到“ Outer” Invoke-Command呼叫:

$WaitSeconds = 1234

Invoke-Command {
    # PowerShell will now copy the $WaitSeconds variable value from the calling scope to this remote session
    $WaitSeconds = $using:WaitSeconds

    # ...

    Invoke-Command {
        # This will now resolve the variable value correctly
        shutdown -s -t $using:WaitSeconds
    }

}

In the second invoke-command the variable is empty

That's because the variable doesn't exist inside the calling session (the first Invoke-Command call's execution context).

Make sure you first instruct PowerShell to copy the variable to the "outer" Invoke-Command call:

$WaitSeconds = 1234

Invoke-Command {
    # PowerShell will now copy the $WaitSeconds variable value from the calling scope to this remote session
    $WaitSeconds = $using:WaitSeconds

    # ...

    Invoke-Command {
        # This will now resolve the variable value correctly
        shutdown -s -t $using:WaitSeconds
    }

}
稀香 2025-02-15 10:17:32

我得到了它
我的解决方案是:

$WaitSeconds = 1234

Invoke-Command {
param($WaitSeconds)
   
    $WaitSeconds = $WaitSeconds

    # ...

    Invoke-Command {
    param($WaitSeconds)
        
        shutdown -s -t $WaitSeconds
    } -ArgumentList $WaitSeconds

} -ArgumentList $WaitSeconds

i got it
my solution is:

$WaitSeconds = 1234

Invoke-Command {
param($WaitSeconds)
   
    $WaitSeconds = $WaitSeconds

    # ...

    Invoke-Command {
    param($WaitSeconds)
        
        shutdown -s -t $WaitSeconds
    } -ArgumentList $WaitSeconds

} -ArgumentList $WaitSeconds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文