远程变量

huangapple go评论80阅读模式
英文:

Remote variable

问题

我遇到了将变量传递到远程 PowerShell 会话的问题,我已经在互联网上搜索了我能想到的所有地方,所以希望有人能帮助我。
我有一个函数调用一个作业,告诉它启动一个脚本块。

我试图做的是编写一个脚本,允许我从一个虚拟机故障切换到另一个虚拟机。如果所有内容都是硬编码,它可以正常工作,但这意味着我必须为每次故障切换更改脚本,这根本不会节省我的时间。

$Computer = Server1
$Clustergrp = Cluster1

function FAILOVER(){
$job = Start-Job -scriptblock $JobScript
do {[System.Windows.Form.Application]::DoEvent() } until ($Job.State -eq "Completed")
}

$JobScript = 
{
Invoke-Command -ComputerName $using:Computer -ScriptBlock {Move-ClusterGroup -Name $using:Clustergrp}
}

我在这里漏掉了什么?请帮忙!

我尝试过使用参数和 $using:variable,但似乎什么都不起作用。但如果我在脚本块中硬编码所有内容,它每次都能正常工作。

我还尝试过只使用 -ComputerName $Computer -ScriptBlock{.....},但也不起作用。

英文:

I am having an issue with passing variables to a remote powershell session and I've searched everywhere on the internet I can think of so I'm hoping someone out there can help.
I have a function calling a job which is telling it to start a scriptblock

What I'm trying to do is write a script to allow me to failover clusters from one VM. It works fine if everything is hardcoded but that means I have to change the script for every failover and that just won't save me anytime at all.

$Computer = Server1
$Clustergrp = Cluster1


function FAILOVER(){
$job = Start-Job -scriptblock $JobScript
do {[System.Windows.Form.Application]::DoEvent() } until ($Job.State -eq "Completed")


$JobScript = 
{
Invoke-Command -ComputerName $using:Computer -ScriptBlock {Move-ClusterGroup -Name $using:Clustergrp}
}

What am I missing here? Help please!

I've tried using arguments and $using:variable but nothing seems to work. Yet if I hardcode everything in the scriptblock it works everytime.

I've also tried just using -ComputerName $Computer -ScriptBlock{.....} but that didn't work either.

答案1

得分: 1

<!-- language-all: sh -->

**_通用_ 知识点:**

* [`Invoke-Command`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/Invoke-Command) 有一个 `-AsJob` 参数,因此无需首先使用 `Start-Job` 创建作业。

* 若要等待作业完成,使用 [`Wait-Job`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/Wait-Job) 命令。

* 如果要立即等待,即要进行 _同步_ 执行,只需省略 `-AsJob`。
  * 请注意,`Invoke-Command` 的 `-ComputerName` 参数接受一个 _计算机名称数组_,然后会 _并行_ 定向这些计算机。

---

至于 **你尝试了什么**:

你正在 _嵌套_ 两个运行空间外(进程外)的范围,这也需要嵌套使用 [`$using:` 范围](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables#using-remote-variables):

$Computer = 'Server1'
$Clustergrp = 'Cluster1'

$JobScript = {

从调用者的 $Clustergrp 值创建一个 本地 变量,以便在远程执行的 Invoke-Command 脚本块中再次使用 $using: 引用。

$Clustergrp = $using:Clustergrp
Invoke-Command -ComputerName $using:Computer -ScriptBlock {
Move-ClusterGroup -Name $using:Clustergrp
}
}

function FAILOVER {
$job = Start-Job -ScriptBlock $JobScript
$job | Wait-Job
}

英文:

<!-- language-all: sh -->

General points:

  • Invoke-Command has an -AsJob parameter, so there's no need for creating a job with Start-Job first.

  • To wait for a job's completion, use the Wait-Job cmdlet.

  • If you want to wait right away, i.e. if you want synchronous execution to begin with, simply omit -AsJob.

    • Note that Invoke-Command's -ComputerName parameter accepts an array of computer names that are then targeted in parallel.

As for what you tried:

You're nesting two out-of-runspace (out-of-process) scopes, which also requires nesting the use of the $using: scope:

$Computer = &#39;Server1&#39;
$Clustergrp = &#39;Cluster1&#39;

$JobScript = {
  # Create a *local* variable from the caller&#39;s
  # $Clustergrp value, so it can again be referenced with
  # $using: in the remotely executing Invoke-Command script block.
  $Clustergrp = $using:Clustergrp
  Invoke-Command -ComputerName $using:Computer -ScriptBlock { 
    Move-ClusterGroup -Name $using:Clustergrp
  }
}

function FAILOVER {
  $job = Start-Job -ScriptBlock $JobScript
  $job | Wait-Job
}

答案2

得分: 0

Invoke-Command -ComputerName $using:Computer -ArgumentList $arg1 -ScriptBlock {param($arg) Move-ClusterGroup -Name $arg[0]}

英文:
Invoke-Command -ComputerName $using:Computer -ArgumentList $arg1 -ScriptBlock {param($arg) Move-ClusterGroup -Name $arg[0]}

huangapple
  • 本文由 发表于 2023年6月6日 02:31:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定