【问题标题】:Trying to verify remote command passed was successful in PowerShell尝试验证通过的远程命令在 PowerShell 中是否成功
【发布时间】:2018-12-01 06:15:35
【问题描述】:

我仍在研究实现此目的的最佳方法。如果有人有建议,请告诉我。

  1. 我希望将 PowerShell 命令传递到远程计算机并接收命令是否成功的验证。
  2. 在收到结果之前,脚本不应继续执行。
  3. 如果命令失败,它应该再次尝试,直到它在远程计算机上成功,如果 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


【解决方案1】:

我还没有找到验证发送的脚本是否成功运行的解决方案。然而,我能想出的下一个最好的项目是测试我可以通信并接受命令的 WinRM。那么我想我必须相信 WinRM 完成了调用命令。

Function Enable-CommonAreaPhones
{
    [CmdletBinding()]
    param
    (
        #[Parameter(Position = 2)]
        [Parameter(Mandatory)] 
        $DisplayName, 
        [Parameter(Mandatory)] 
        $SIP, 
        [Parameter(Mandatory)] 
        $LineURI, 
        [Parameter(Mandatory)] 
        $OU_CommonArea, 
        [Parameter(Mandatory)] 
        $CreateNewCAP = $true,
        [Parameter(Mandatory)] 
        $RegistrarPool, 
        [Parameter(Mandatory)] 
        $DisplayNumber,
        [Parameter(Mandatory)] 
        $Description
    )
    <#
    dynamicparam {
        #https://mcpmag.com/articles/2016/10/06/implement-dynamic-parameters.aspx
        #https://stackoverflow.com/questions/42318419/powershell-using-dynamic-parameters-value
        #https://github.com/PowerShell/PowerShell/issues/3704
        $ParameterName_SFBRegistrarPool = 'SFBRegistrarPool'
        $ParamAttrib = New-Object  System.Management.Automation.ParameterAttribute
        $ParamAttrib.Mandatory = $true
        $ParamAttrib.ParameterSetName = '__AllParameterSets'
        
        $AttribColl = New-Object  System.Collections.ObjectModel.Collection[System.Attribute]
        $AttribColl.Add($ParamAttrib)
            $ValidateItems = (get-csservice -Registrar).PoolFqdn
            
            $AttribColl.Add((New-Object  System.Management.Automation.ValidateSetAttribute($ValidateItems)))

        $RuntimeParam = New-Object  System.Management.Automation.RuntimeDefinedParameter($ParameterName_SFBRegistrarPool, [string],  $AttribColl)

        $RuntimeParamDic = New-Object  System.Management.Automation.RuntimeDefinedParameterDictionary
        $RuntimeParamDic.Add($ParameterName_SFBRegistrarPool, $RuntimeParam)

        return $RuntimeParamDic
    }
        begin
        {$RegistrarPool = $PsBoundParameters[$ParameterName_SFBRegistrarPool]}
        process
        {}
        End
        {}
    #>

    if ($CreateNewCAP)
    {
        <#
        #$Description = "Huddle-N405-172B"
        #$DisplayName = "(713)577-3160"
        #$DisplayNumber = "+1-713-577-3160"
        #$LineURI = 'tel:+17135773160;ext=3160'
        #$SIP = 'sip:Huddle-N405-172B.BR172B@mrcglobal.com'
        #$OU = "OU=172B LA PORTE -- TX,OU=Gulf Coast,OU=CAP,OU=Lync,DC=mcjunkinredman,DC=com"
        #$RegistrarPool = 'hexlsfepool.mcjunkinredman.com'
        #>

        param($LineURI,$RegistrarPool, $OU_CommonArea, $Description, $DisplayName, $DisplayNumber,$SIP )
        write-host "New-CsCommonAreaPhone -LineUri $lineuri -RegistrarPool $SFBFQDNRegistrarPool -OU $OU -Description $Description -DisplayName $DisplayName -DisplayNumber $DisplayNumber -SipAddress $SIP"
        
        New-CsCommonAreaPhone -LineUri $LineURI -RegistrarPool $RegistrarPool -OU $OU_CommonArea -Description $Description -DisplayName $DisplayName -DisplayNumber $DisplayNumber -SipAddress $SIP -WhatIf;
        Grant-CsClientPolicy -PolicyName "SkypeUI" -Identity $SIP;
        Grant-CsVoicePolicy -PolicyName "NA-TX-LAP" -Identity $SIP;
        Grant-CsDialPlan -PolicyName "NA-TX-LAP" -Identity $SIP;
        #Set-CsClientPin –Identity $SIP -Pin 1111;
        #Move-CsCommonAreaPhone -Identity $SIP -Target $R_RegistrarPool; 
    }
    Else
    {
        #Get-CsCommonAreaPhone | Set-CsCommonAreaPhone -Enabled $True
        #Get-CsCommonAreaPhone -Filter {Description -eq $Null} | Set-CsCommonAreaPhone -Description "Common area phone"
        #Get-CsCommonAreaPhone -Filter {LineUri -eq "tel:+14255556710"} | Set-CsCommonAreaPhone -DisplayName "Employee Lounge"

    }
}

function invoke-SkypeObject_RemoteModification_CommonAreaPhones
{
    #Finds all Front End Servers
    $SkypeFeRemoteComputers = Get-SkypeRegistrarPoolInfo -Pool $RegistrarPool 
    $RemoteFEComputerCompletedSuccessfull = $false

    ForEach ($Computer in $SkypeFeRemoteComputers)
    {
        #Validates Command has only ran on 1 machine
        #Validates the computer exists and can accept invoke commands via the WinRM Command
        If ((!($RemoteFEComputerCompletedSuccessfull)) -and (Test-Connection -ComputerName $env:COMPUTERNAME -Quiet) -and (Test-WSMan -ComputerName $computer -ErrorAction Ignore))
        {
            #https://ss64.com/ps/syntax-functions.html
            $scriptblock = $executioncontext.InvokeCommand.NewScriptBlock(${function:Enable-CommonAreaPhones})
            Invoke-Command -Session $allsession -ScriptBlock $scriptblock -ArgumentList $R_LineURI,$R_RegistrarPool, $R_OU_CommonArea, $R_Description, $R_DisplayName, $R_DisplayNumber,$R_SIP -ErrorVariable errorMsg
            Invoke-Command -Session $allsession -ScriptBlock ${function:Enable-CommonAreaPhones} -ArgumentList $R_LineURI,$R_RegistrarPool, $R_OU_CommonArea, $R_Description, $R_DisplayName, $R_DisplayNumber,$R_SIP -ErrorVariable errorMsg
            $RemoteFEComputerCompletedSuccessfull = $true
        }   
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2011-04-28
    • 2021-02-19
    • 2020-01-09
    • 2022-03-24
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多