完全披露:我的问题可能是基于对Xen Desktop的Citrix PowerShell模块的不完全理解。
我有以下脚本块。它在循环中调用,对列表中的每个 VM 调用一次。我正在使用PowerShell Jobs
,因为我想在作业运行时保持 UI 线程自由更新 UI。
代码 "A">
$j = Start-Job -Name $jobName -ScriptBlock {
param($url, $uuid, $cred, $snapshotName)
$ErrorActionPreference = "Stop"
try
{
$error.clear()
$xSS = $cred | Connect-XenServer -url $url -NoWarnCertificates -SetDefaultSession -PassThru;
$vm = (Get-XenVM -SessionOpaqueRef $xss.opaque_ref -uuid $uuid)
#Create snapshot
Invoke-XenVM -Async -SessionOpaqueRef $xss.opaque_ref -VM $vm -XenAction Snapshot -NewName $snapshotName
return "OK"
}
catch
{
return ("ERROR: "+$error)
}
} -ArgumentList $global:configFileVmMetaData.poolUrl, $xenVm.key, $global:cred, $snapshotName
代码"A">工作正常,但需要更长的时间,因为我每次调用脚本块时都会执行Connect-XenServer
cmdlet。
因此,我尝试在循环外调用一次Connect-XenServer
并传入会话变量,如下面的代码"B">所示。结果是错误Could not find open sessions to any XenServers
被抛出在脚本块内。我假设 $xss 会话变量在传递到脚本块时以某种方式被挂起。
任何想法为什么会话变量$xss被挂起?
代码 "B">
$xSS = $cred | Connect-XenServer -url $global:configFileVmMetaData.poolUrl -NoWarnCertificates -SetDefaultSession -PassThru;
loop
{
$j = Start-Job -Name $jobName -ScriptBlock {
param($xss, $uuid, $snapshotName)
$ErrorActionPreference = "Stop"
try
{
$error.clear()
$vm = (Get-XenVM -SessionOpaqueRef $xss.opaque_ref -uuid $uuid)
#Create snapshot
Invoke-XenVM -Async -SessionOpaqueRef $xss.opaque_ref -VM $vm -XenAction Snapshot -NewName $snapshotName
return "OK"
}
catch
{
return ("ERROR: "+$error)
}
} -ArgumentList $xss, $xenVm.key, $snapshotName
}
受
Robert Cotterman
答案启发的其他示例在所有情况下,我仍然收到
Could not find open sessions to any XenServers
错误仅供参考 - 使用 PowerShell 5.1
使用$using
的示例。变量内容按预期传入和传出
cls
$aLocal = "AAA"
$bLocal = "BBB"
$j = Start-Job -Name "TestJob" -ScriptBlock {
return ($using:aLocal + " *** " + $using:bLocal)
}
while ($true)
{
$g = get-job -name "TestJob"
write-host ("get-job " + $g.Name + " is " + $g.State)
if ($g.State -ne "Running")
{
break
}
start-sleep -Seconds 1
}
write-host ("receive-Job='" + (receive-Job -Name "TestJob") +"'")
$g = get-Job -Name "TestJob"
Write-Host ("get-Job "+$g.name + " " + $g.state + " " + $g.HasMoreData + " " + $g.id)
if($g)
{
Remove-Job -Name "TestJob"
}
输出
get-job TestJob is Running
get-job TestJob is Completed
receive-Job='AAA *** BBB'
get-Job TestJob Completed False 45
Remove-Job
使用参数的示例。 变量内容按预期传入和传出
cls
$aLocal = "AAA"
$bLocal = "BBB"
$j = Start-Job -Name "TestJob" -ScriptBlock {
return ($args[0] + " *** " + $args[1])
} -ArgumentList ($aLocal, $bLocal)
while ($true)
{
$g = get-job -name "TestJob"
write-host ("get-job " + $g.Name + " is " + $g.State)
if ($g.State -ne "Running")
{
break
}
start-sleep -Seconds 1
}
write-host ("receive-Job='" + (receive-Job -Name "TestJob") +"'")
$g = get-Job -Name "TestJob"
Write-Host ("get-Job "+$g.name + " " + $g.state + " " + $g.HasMoreData + " " + $g.id)
if($g)
{
Remove-Job -Name "TestJob"
}
输出
get-job TestJob is Running
get-job TestJob is Completed
receive-Job='AAA *** BBB'
get-Job TestJob Completed False 49
使用命名参数的示例。 变量内容按预期传入和传出
cls
$aLocal = "AAA"
$bLocal = "BBB"
$j = Start-Job -Name "TestJob" -ScriptBlock {
param($a, $b)
return ($a + " *** " + $b)
} -ArgumentList ($aLocal, $bLocal)
while ($true)
{
$g = get-job -name "TestJob"
write-host ("get-job " + $g.Name + " is " + $g.State)
if ($g.State -ne "Running")
{
break
}
start-sleep -Seconds 1
}
write-host ("receive-Job='" + (receive-Job -Name "TestJob") +"'")
$g = get-Job -Name "TestJob"
Write-Host ("get-Job "+$g.name + " " + $g.state + " " + $g.HasMoreData + " " + $g.id)
if($g)
{
Remove-Job -Name "TestJob"
}
输出
get-job TestJob is Running
get-job TestJob is Completed
receive-Job='AAA *** BBB'
get-Job TestJob Completed False 55
我只是不认为它那样工作。与 Xen 服务器的连接是为 PowerShell 会话创建的,有关该连接的信息收集在 $xss 变量中。每个作业都运行自己的 PowerShell 会话。因此,仅将$xss传递给作业是不一样的,它只是对连接详细信息的描述。
作业和 invoke-command 要求您指定正在使用变量。只需将变量从
$variable
自
$using:variable
内部变量不需要这个。但是从父脚本调用变量确实如此。
或者,由于您传递$xss作为参数,因此您不会用$xss而是
$args[0]
因为这是你的第一个论点。和$args[1]为第二个等等... 原因是因为整个 xss 变量被打印为参数,而不是在作业中命名。它被命名为 $args,并在第一个插槽 (0( 中占有一席之地。
我更喜欢$using:变量,因为它减少了混乱