Unable to export the result to .CSV file ?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Unable to export the result to .CSV file ?

Post by ITEngineer »

People,

I'm trying to the list of the installed update like the below result into .CSV like running the Get-HotFix manually one by one for each server:

Code: Select all

ServerName, Last Time Update Installed, KB Number, KB Update Name, InstalledBy
PRODDB01, 31/12/2018 02:46:55, KB4462930, Cumulative Update, NT AUTHORITY\SYSTEM
PRODDB02, 18/12/2018 12:00:00 AM, KB4471324, Security Update, DOMAIN\SVC-SCCM
PRODDC01, 16/1/2019 02:16:31, KB4343669, Cumulative Update, DOMAIN\SVC-SCCM
PRODDC02, 13/1/2018 03:00:00 AM, KB4457146, Security Update, DOMAIN\Admin-IT1
Using the PowerShell script below:

Code: Select all

$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
However, even with the code above, there is no result in the .CSV and it is still blank.

Any help would be greatly appreciated.

Thank you.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to export the result to .CSV file ?

Post by jvierra »

Code: Select all

$CsvFile = 'C:\Result.csv'
$key = 'SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install'

$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 {
        Get-ADComputer -Filter { Enabled -eq $True -and OperatingSystem -like '*Server*' } -SearchBase $_ |
        Where-Object {
            If (Test-Connection $_ -Count 1 -Quiet) {
                $_
            } else {
                Write-Host "Cannot reach $($_)" -ForegroundColor Red
            }
        }
    } |
    ForEach-Object{
        Try {
            $remoteBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_.Name)
            $regKey = $remoteBase.OpenSubKey($key)
            $keyValue = $regkey.GetValue('LastSuccessTime')
            [pscustomobject]@{ Computer = $_.Name; LastSuccessTime = $keyValue }
        } Catch {
            Throw
        }
    } | 
    Export-Csv -Path $Csvfile -NoTypeInformation
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Unable to export the result to .CSV file ?

Post by ITEngineer »

jvierra wrote: Mon Feb 04, 2019 5:21 pm

Code: Select all

$CsvFile = 'C:\Result.csv'
$key = 'SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install'

$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 {
        Get-ADComputer -Filter { Enabled -eq $True -and OperatingSystem -like "*Server*" } -SearchBase $_ |
            Where-Object {
                If (Test-Connection $_ -Count 1 -Quiet) {
                    $_
                }else{
                    Write-Host "Cannot reach $($_)" -ForegroundColor Red
                }
            } 
    } |
    ForEach-Object{
        Try {
            $remoteBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_.Name)
            $regKey = $remoteBase.OpenSubKey($key)
            $keyValue = $regkey.GetValue('LastSuccessTime')
            [pscustomobject]@{Computer = $computer; LastSuccessTime = $keyValue}
        } 
        Catch {
            Throw
        } 
    } | 
    Export-Csv -Path $Csvfile -NoTypeInformation
Hi Mr. Vierra,

Somehow it does not work when I execute the script you've suggested above:?:

The .CSV is still blank.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to export the result to .CSV file ?

Post by jvierra »

You really need to learn how to read PS code and how to debug. I post examples and methods but I cannot test in your exact environment. The examples are intended to show you what you need to understand to accomplish your goals. They are not intended to be complete, tested and production ready scripts. That is beyond the scope of all technical forums.

Code: Select all

$CsvFile = 'C:\Result.csv'
$key = 'SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install'

$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 {
        Get-ADComputer -Filter { Enabled -eq $True -and OperatingSystem -like '*Server*' } -SearchBase $_ |
        Where-Object {
            If (Test-Connection $_ -Count 1 -Quiet) {
                $_
            } else {
                Write-Host "Cannot reach $($_)" -ForegroundColor Red
            }
        }
    } |
    ForEach-Object{
        Try {
            $remoteBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_.Name)
            $regKey = $remoteBase.OpenSubKey($key)
            $keyValue = $regkey.GetValue('LastSuccessTime')
            [pscustomobject]@{ Computer = $_.Name; LastSuccessTime = $keyValue }
        } Catch {
            Throw
        }
    } | 
    Export-Csv -Path $Csvfile -NoTypeInformation
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked