【发布时间】:2018-12-01 06:15:35
【问题描述】:
我仍在研究实现此目的的最佳方法。如果有人有建议,请告诉我。
- 我希望将 PowerShell 命令传递到远程计算机并接收命令是否成功的验证。
- 在收到结果之前,脚本不应继续执行。
- 如果命令失败,它应该再次尝试,直到它在远程计算机上成功,如果 AD 和 Skype 复制尚未完成,则可能会发生故障。
这是我目前所拥有的。
除了上述 3 条规则外,它还不是很有效。如果有更好的方法可以做到这一点,我会全力以赴。
$SkypeFeRemoteComputers = Get-SkypeRegistrarPoolInfo -Pool $RegistrarPool
$RemoteFEComputerCompletedSuccessfull = $false
foreach ($Computer in $SkypeFeRemoteComputers) {
if ($RemoteFEComputerCompletedSuccessfull -eq $false) {
try {
Invoke-Command -ComputerName $Computer -AsJob -JobName myJob_NewCommonAreaPhone -ErrorAction Stop -ArgumentList $LineURI,$RegistrarPool, $OU_CommonArea, $Description, $DisplayName, $DisplayNumber, $SIP -ScriptBlock {
Param($R_LineURI, $R_RegistrarPool, $R_OU_CommonArea, $R_Description, $R_DisplayName, $R_DisplayNumber, $R_SIP)
New-CsCommonAreaPhone -LineUri $R_LineURI -RegistrarPool $R_RegistrarPool -OU $R_OU_CommonArea -Description $R_Description -DisplayName $R_DisplayName -DisplayNumber $R_DisplayNumber -SipAddress $R_SIP -WhatIf;
Move-CsCommonAreaPhone -Identity $R_SIP -Target $R_RegistrarPool;
Grant-CsClientPolicy -PolicyName "SkypeUI" -Identity $R_SIP;
Grant-CsVoicePolicy -PolicyName "NA-TX-LAP" -Identity $R_SIP;
Grant-CsDialPlan -PolicyName "NA-TX-LAP" -Identity $R_SIP;
Set-CsClientPin -Identity $R_SIP -Pin 1111;
}
Get-Job
$Result = Invoke-Command -Session $Session -ScriptBlock {
Receive-Job -Name myJob_NewCommonAreaPhone -Keep
}
if ($Result -eq $true) {
$RemoteFEComputerCompletedSuccessfull = $true
}
} catch {
Add-Content Unavailable-Computers.txt $Computer
}
}
}
Write-Host "New-CsCommonAreaPhone -LineUri $lineuri -RegistrarPool $SFBFQDNRegistrarPool -OU $OU -Description $Description -DisplayName $DisplayName -DisplayNumber $DisplayNumber -SipAddress $SIP"
} else {
Write-Host "Set-CsCommonAreaPhone -identity $sip -sipaddress $sip -DisplayName $phone.Display -DisplayNumber $phone.Display"
}
}
我正在研究的东西可能也是Invoke-Expression。
【问题讨论】:
-
您的代码有些地方看起来不正确。例如,您使用
-ComputerName调用命令,然后稍后尝试使用会话对象$session进行连接,该对象似乎没有在任何地方定义。您在本地计算机上创建了一个作业对象,但尝试在另一台计算机上检索它。-WhatIf参数将使 cmdlet 告诉您它会做什么而不实际执行工作,因此它永远不会成功。你对成功的定义是什么?您如何确定这将帮助您决定如何解决问题(使用或不使用 Jobs 等)。 -
这里简单地说我想做的事情。
-
这里简单地说我想做的事。 `Invoke-Command -ComputerName $Computer -AsJob -JobName myJob_NewCommonAreaPhone -ErrorAction Stop -ArgumentList $LineURI,$RegistrarPool, $OU_CommonArea, $Description, $DisplayName, $DisplayNumber, $SIP -ScriptBlock { Param($R_LineURI, $R_RegistrarPool, $ R_OU_CommonArea、$R_Description、$R_DisplayName、$R_DisplayNumber、$R_SIP)
-
New-CsCommonAreaPhone -LineUri $R_LineURI -RegistrarPool $R_RegistrarPool -OU $R_OU_CommonArea -Description $R_Description -DisplayName $R_DisplayName -DisplayNumber $R_DisplayNumber -SipAddress $R_SIP -WhatIf; }` 我知道我可以使用 -cred 参数来完成这项工作。我想要的是,如果命令成功完成,则命令返回 True,如果失败则返回 false。
-
我的全部原因是,如果 AD 和 Skype 没有完成复制,命令 (Set-CsClientPin -Identity $R_SIP -Pin 1111) 将失败。所以我正在寻找一种方法来捕捉这个失败,并重新运行它直到它通过。
标签: powershell command skype