Modifying PowerShell to export Windows Update to. CSV file for specific OU only not working?

I need to get the list of the server last windows update patch from multiple different OU and then export it as in CSV file with the below column and its sample result I gather manually running Get-HotFix locally on each server:

ServerName, Last Time Update Installed, KB Number, KB Update Name, InstalledBy
PRODSQL01-VM, 31/12/2018 02:46:55, KB4462930, Cumulative Update, NT AUTHORITY\SYSTEM
PRODSQL02-VM, 18/12/2018 12:00:00 AM, KB4471324, Security Update, DOMAIN\SVC_SCCM
PRODDC01-VM, 16/1/2019 02:16:31, KB4343669, Cumulative Update, DOMAIN\SVC_SCCM
PRODDC02-VM, 13/1/2018 03:00:00 AM, KB4457146, Security Update, DOMAIN\Admin-Staff1

This is the modified script for multiple OU processing, but somehow the result is still blank.CSV file?

$CsvFile = 'C:\Result.csv'
$key = 'SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install'
$keytype = [Microsoft.Win32.RegistryHive]::LocalMachine 

$OUList = @(
    "OU=TEST Servers,OU=MyDomain Testing,DC=MyDomain,DC=com"
    "OU=PROD Servers,OU=Servers,OU=MyDomain Sydney,DC=MyDomain,DC=com"
    "OU=PROD Servers,OU=Servers,OU=New Company,DC=MyDomain,DC=com"
)

$OUList | ForEach-Object {
    $OU = $_
    $Computers = Get-ADComputer -Filter {Enabled -eq $True -and OperatingSystem -like "*Server*"} -SearchBase $OU |
        Select-Object -ExpandProperty DNSHostName |
        ForEach-Object {
        If (Test-Connection $_ -Count 1 -Quiet) {
            $_
        }
        Else {
            Write-Host "Cannot reach $($_)" -ForegroundColor Red
        }
    }
    ForEach ($computer in $Computers) {
        Try {
            $remoteBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($keytype, $computer) 
            $regKey = $remoteBase.OpenSubKey($key) 
            $keyValue = $regkey.GetValue('LastSuccessTime')
            Write-Host '' 
            Write-Host "$($computer): last time updates were installed was $($keyValue)"
        }
        Catch {
            $ | Write-Error
        }
        Finally {
            If ($regKey) {$regKey.Close()}
        }
    }
} | Export-Csv -Path $Csvfile -NoTypeInformation

Any help would be greatly appreciated.

Thanks,

1 answer

  • answered 2019-02-08 03:37 Trix

    As others have mentioned, you're doing write-host rather than adding anything to your CSV file.

    Note that I haven't tested any of the below code - it's just a bit of rearranging.

    $CsvFile = 'C:\Result.csv'
    $Results = @() #object to hold the output
    $key = 'SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install'
    $keytype = [Microsoft.Win32.RegistryHive]::LocalMachine 
    
    $OUList = @(
        "OU=TEST Servers,OU=MyDomain Testing,DC=MyDomain,DC=com"
        "OU=PROD Servers,OU=Servers,OU=MyDomain Sydney,DC=MyDomain,DC=com"
        "OU=PROD Servers,OU=Servers,OU=New Company,DC=MyDomain,DC=com"
    )
    
    ForEach ($OU in $OUList) {
        Get-ADComputer -Filter {Enabled -eq $True -and OperatingSystem -like "*Server*"} -SearchBase $OU | 
        Select-Object -ExpandProperty DNSHostName | 
        ForEach-Object {
            If (Test-Connection $_ -Count 1 -Quiet) {
                Try {
                    $remoteBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($keytype, $_) 
                    $regKey = $remoteBase.OpenSubKey($key) 
                    $keyValue = $regkey.GetValue('LastSuccessTime')
                    # Add result to Results array
                    $result += [pscustomobject]@{
                        Computer = $_
                        LastSuccessTime = $KeyValue
                    }
                }
                Catch {
                    $ | Write-Error
                }
                Finally {
                    If ($regKey) {$regKey.Close()}
                }
            }
            Else {
                Write-Host "Cannot reach $($_)" -ForegroundColor Red
            }
        }
    }
    #export result object to CSV
    $Results | Export-Csv -Path $Csvfile -NoTypeInformation