尝试验证通过的远程命令在PowerShell中是否成功

时间:2018-06-21 19:55:52

标签: powershell command skype

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

  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

1 个答案:

答案 0 :(得分:0)

我尚未找到验证发送的脚本是否成功运行的解决方案。但是,我能想到的下一个最佳项目是测试WinRM我可以进行通信并接受命令。然后我想我只能相信WinRM完成了Invoke Command。

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
        }   
    }

}