runner-ralf
Goto Top

Mit Powershell WSUS Computer auflisten

Hallo alle zusammen,

ich habe ein Powershell Script mit dem ich alle Computer auslese die an einem WSUS Server angemeldet sind. Das hat auch bisher ganz gut funktioniert. Nur seit ich ein paar Untergruppen angelegt habe, gibt es Probleme und ich bekomme es einfach nicht mehr gebacken.

Beispiel:
Bisher gabe es.

Abteilung_A
PC_1
PC_2
PC_3
PC_4
Abteilung_B
PC_5
Abteilung_C
PC_6

Diese wurden sauber aufgelistet. Nehmen wir an ich hätte nur die Anzahl ermittelt. Also Anzahl 6.

Jetzt wurde noch ein Unterteilung vorgenommen.

Abteilung_A
WIN8
PC_1
PC_2
WIN10
PC_3
PC_4
Abteilung_B
PC_3
Abteilung_C
PC_4

Nun erhalte ich in der Anzahl 10 Rechner. Das Script schreibt PC_1 bis PC_4 unter Abteilung_A und zusätzlich PC_1 und PC_2 unter WIN8 und PC_3 und PC_4 unter WIN10. Die 4 Rechner sollten also nicht mehr unter Abteilung_A aufgelistet werden. Dort stehen sie auch nicht nicht mehr sondern eben unter WIN8 und WIN10. Wie kann ich vermeiden dass die Rechner nicht in Abteilung_A geliestet werden?

Ich weiß, dass ein Teil des Codes doppelt vorhanden ist weil Hauptgruppen und Untergruppen den fast identischen Ablauf haben. Das werde ich noch in eine Funktion auslagern. Aber ich wollte jetzt erst mal testen warum es nicht funktioniert face-smile

Es müsste wohl hier:

$RootComputerGroups = $AllComputersGroup.GetChildTargetGroups() | ?{$_.Name -notmatch "Unassigned computers"}

was angepasst werden. Aber es können weitere ChildGruppen folgen und deshalb kann es nicht hardcodiert eingefügt werden so wie die "Unassigned computers". Habe einiges an Code entfernt um es nicht so aufzublähen. Es geht nur um die doppelten PC's


Ich danke euch!


Code:

$updateServer = "localhost"
$useSecureConnection = $False
$portNumber = 8530
Add-Type -Path "C:\Program Files\Update Services\Api\Microsoft.UpdateServices.Administration.dll"
$AdminProxy = New-Object -TypeName Microsoft.UpdateServices.Administration.AdminProxy
$WSUSServer = $AdminProxy.GetRemoteUpdateServerInstance($updateServer,$useSecureConnection,$portNumber)
$WSUSServer.PreferredCulture = "en"

$AllComputersGroup = $WSUSServer.GetComputerTargetGroups() | ?{$_.Name -match "All computers"}
$RootComputerGroups = $AllComputersGroup.GetChildTargetGroups() | ?{$_.Name -notmatch "Unassigned computers"}
$TempComputerGroups = $RootComputerGroups
$CTScope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope

$LoopTempComputerGroups = @()

If (-Not (Import-Module UpdateServices -PassThru)) {
Add-Type -Path "$Env:ProgramFiles\Update Services\Api\Microsoft.UpdateServices.Administration.dll" -PassThru
}

Do {
ForEach ($ComputerGroup in $TempComputerGroups ) {
If ($RootComputerGroups -contains $ComputerGroup) {

$ComputerGroup.GetComputerTargets($CTScope) | Sort -Property Name | ForEach {

$objSummary = $_.GetUpdateInstallationSummary()
$Down = $objSummary.DownloadedCount
$Fail = $objSummary.FailedCount
$Pend = $objSummary.InstalledPendingRebootCount
$NotI = $objSummary.NotInstalledCount
$Unkn = $objSummary.UnknownCount
$Total = $Down + $Fail + $Pend + $NotI + $Unkn

$intLineCounter = $intLineCounter + 1
$IntStr = [Convert]::ToString($intLineCounter)

if ($Total -eq 0) {$Estado="OK"; $bgcolor="LightGreen"}
elseif ($Pend -ne 0) {$Estado="Reboot needed"; $bgcolor="LightSalmon"}
elseif ($Down -ne 0) {$Estado="Ready to install"; $bgcolor="Cyan"}
elseif ($NotI -ne 0) {$Estado="Pending"; $bgcolor="Yellow"}
elseif ($Fail -ne 0) {$Estado="Error"; $bgcolor="IndianRed"}
elseif ($Unkn -ne 0) {$Estado="Not reported yet"; $bgcolor="Silver"}
else {$Estado=""; $bgcolor="White"}

$LastContact = $_.LastReportedStatusTime
$days = [Math]::Ceiling((New-TimeSpan -Start $LastContact).TotalDays)

if ($days -gt 14) {$Color="Red"}
elseif ($days -gt 7) {$Color="Orange"}
elseif ($days -gt 2) {$Color="Yellow"}
else {$Color="White"}


if ($days -eq 0) {$Dias="Today"}
elseif ($days -eq 1) {$Dias="Yesterday"}
else {$Dias="Since " + $days + " days."}

if ($LastContact -eq [DateTime]::MinValue) {$Dias="Never"; $Color="Silver"}


}

}

$LoopTempComputerGroups += $ComputerGroup.GetChildTargetGroups()
ForEach ($ChildComputerGroup in $LoopTempComputerGroups)
{

$ChildComputerGroup.GetComputerTargets($CTScope) | Sort -Property Name | ForEach {

...Code wie in oben....

}

}
}
$TempComputerGroups = $LoopTempComputerGroups
$TempComputerGroups
$LoopTempComputerGroups = @()
} While ($TempComputerGroups)

Content-Key: 517514

Url: https://administrator.de/contentid/517514

Ausgedruckt am: 19.03.2024 um 09:03 Uhr

Mitglied: ITvortex
ITvortex 21.11.2019 um 11:38:07 Uhr
Goto Top
Hey,

könntest du deinen Code bitte mit Code Tags ergänzen?
Damit wäre es einfacher zu lesen.
Mitglied: runner-ralf
runner-ralf 25.11.2019 um 08:00:12 Uhr
Goto Top
$updateServer = "localhost"  
$useSecureConnection = $False 
$portNumber = 8530
Add-Type -Path "C:\Program Files\Update Services\Api\Microsoft.UpdateServices.Administration.dll"   
$AdminProxy = New-Object -TypeName Microsoft.UpdateServices.Administration.AdminProxy 
$WSUSServer = $AdminProxy.GetRemoteUpdateServerInstance($updateServer,$useSecureConnection,$portNumber) 
$WSUSServer.PreferredCulture = "en"   

$AllComputersGroup = $WSUSServer.GetComputerTargetGroups() | ?{$_.Name -match "All computers"}  
$RootComputerGroups = $AllComputersGroup.GetChildTargetGroups() | ?{$_.Name -notmatch "Unassigned computers"}  
$TempComputerGroups = $RootComputerGroups 
$CTScope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope 

 
 If (-Not (Import-Module UpdateServices -PassThru)) {
    Add-Type -Path "$Env:ProgramFiles\Update Services\Api\Microsoft.UpdateServices.Administration.dll" -PassThru  
 }

 Do {

    ForEach ($ComputerGroup in $TempComputerGroups ) {

        If ($RootComputerGroups -contains $ComputerGroup) {

            $ComputerGroup.GetComputerTargets($CTScope) | Sort -Property Name | ForEach {

            $objSummary = $_.GetUpdateInstallationSummary() 
            $Down = $objSummary.DownloadedCount 
            $Fail = $objSummary.FailedCount
            $Pend = $objSummary.InstalledPendingRebootCount 
            $NotI = $objSummary.NotInstalledCount
            $Unkn = $objSummary.UnknownCount 
            $Total = $Down + $Fail + $Pend + $NotI + $Unkn 

            $intLineCounter = $intLineCounter + 1 
            $IntStr = [Convert]::ToString($intLineCounter) 

            if ($Total -eq 0) {$Estado="OK"; $bgcolor="LightGreen"}  
            elseif ($Pend -ne 0) {$Estado="Reboot needed"; $bgcolor="LightSalmon"}  
            elseif ($Down -ne 0) {$Estado="Ready to install"; $bgcolor="Cyan"}  
            elseif ($NotI -ne 0) {$Estado="Pending"; $bgcolor="Yellow"}  
            elseif ($Fail -ne 0) {$Estado="Error"; $bgcolor="IndianRed"}  
            elseif ($Unkn -ne 0) {$Estado="Not reported yet"; $bgcolor="Silver"}  
            else {$Estado=""; $bgcolor="White"}  

            $LastContact = $_.LastReportedStatusTime 
            $days = [Math]::Ceiling((New-TimeSpan -Start $LastContact).TotalDays) 

            if ($days -gt 14) {$Color="Red"}   
            elseif ($days -gt 7) {$Color="Orange"}   
            elseif ($days -gt 2) {$Color="Yellow"}   
            else {$Color="White"}   


            if ($days -eq 0) {$Dias="Today"}  
            elseif ($days -eq 1) {$Dias="Yesterday"}  
            else {$Dias="Since " + $days + " days."}  

            if ($LastContact -eq [DateTime]::MinValue) {$Dias="Never"; $Color="Silver"}  


        }

    } ELSE{

        $LoopTempComputerGroups += $ComputerGroup.GetChildTargetGroups() 

        ForEach ($ChildComputerGroup in $LoopTempComputerGroups){

                $ChildComputerGroup.GetComputerTargets($CTScope) | Sort -Property Name | ForEach {

                    ...Code wie oben....

                }

            }
        }
}

    $TempComputerGroups = $LoopTempComputerGroups
    $TempComputerGroups
    $LoopTempComputerGroups = @()

 } While ($TempComputerGroups) 
Mitglied: runner-ralf
runner-ralf 25.11.2019 um 08:02:50 Uhr
Goto Top
Hallo ITvortex,

anbei mit Code Block. Sorry, vor lauter Code anpassen damit keine Firmendaten gepostet werden..... Ich glaube dass der Else Zweig gar nicht greift. Kann das sein?

Gruß

Ralf