locked
Below code does repeat the same hptfix for all the servers even though that was not installed in there, I believe there is some break in the loop. Could someone help me? RRS feed

  • Question

  • Below code does repeat the same hptfix for all the servers even though that was not installed in there, I believe there is some break in the loop.
    Could someone help me?

    $Servers = Get-Content 'C:\temp\Servers.txt'
    $gg = foreach ($Server in $Servers) {
    Try {
    $Hotfix = Get-Hotfix -computername $Server | Where {$_.hotfixid -eq 'KBXXXXXX' -or  $_.hotfixid -eq 'KBXXXXXX' -or $_.hotfixid -eq 'KBXXXXXX'} -ErrorAction Stop
    }
    Catch {
    #Write-host "Unable to reach the Server " $Server
    $ErrorMessage = $_.Exception.Message;
    Continue
    }
    $HotfixID = $Hotfix.HotFixID;
    $NotFound = "NotFound";
    " " | Select-Object -Property @{N="Server_Name";E={$Server}}, @{N="HotFixID";E={
    If ($HotfixID -eq $null) {Return $ErrorMessage}
    Elseif ($HotfixID -ne $null -and $ErrorMessage -eq $null) {Return $HotfixID}
    Elseif ($HotfixID -eq $null -and $ErrorMessage -eq $null) {Return $NotFound}}}
    }
    $gg | Export-CSV -NoTypeInformation 'C:\Temp\Output.csv'
    Sunday, May 14, 2017 8:39 PM

All replies

  • Try posting your code using the code posting tool and indent so that it is readable.

    What is "continue" supposed to do?


    \_(ツ)_/

    Sunday, May 14, 2017 9:04 PM
  • Below code does repeat the same hptfix for all the servers even though that was not installed in there, I believe there is some break in the loop.
    Could someone help me?

    $Servers = Get-Content 'C:\temp\Servers.txt'
    $gg = foreach ($Server in $Servers) {
    Try {
    $Hotfix = Get-Hotfix -computername $Server | Where {$_.hotfixid -eq 'KBXXXXXX' -or  $_.hotfixid -eq 'KBXXXXXX' -or $_.hotfixid -eq 'KBXXXXXX'} -ErrorAction Stop
    }
    Catch {
    #Write-host "Unable to reach the Server " $Server
    $ErrorMessage = $_.Exception.Message;
    Continue
    }
    $HotfixID = $Hotfix.HotFixID;
    $NotFound = "NotFound";
    " " | Select-Object -Property @{N="Server_Name";E={$Server}}, @{N="HotFixID";E={
    If ($HotfixID -eq $null) {Return $ErrorMessage}
    Elseif ($HotfixID -ne $null -and $ErrorMessage -eq $null) {Return $HotfixID}
    Elseif ($HotfixID -eq $null -and $ErrorMessage -eq $null) {Return $NotFound}}}
    }
    $gg | Export-CSV -NoTypeInformation 'C:\Temp\Output.csv'

    $Servers = Get-Content 'C:\temp\Servers.txt'
    Try {
    foreach ($Server in $Servers) {
        $Hotfix = Get-Hotfix -computername $Server | Where {$_.hotfixid -eq 'KBXXXXXX' -or  $_.hotfixid -eq 'KBXXXXXX' -or $_.hotfixid -eq 'KBXXXXXX'} -ErrorAction Stop
        $Hotfix | select source,hotfixid | Export-CSV -NoTypeInformation 'C:\Temp\Output.csv'
    }Catch {
        $ErrorMessage = $_.Exception.Message
    }
    

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Hello_2018 Monday, June 5, 2017 2:46 AM
    Monday, May 15, 2017 8:08 AM