Unable to export the result to .CSV file ?

Ask your Windows 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.
Locked
User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

Unable to export the result to .CSV file ?

Post by ITEngineer » Mon Feb 04, 2019 5:20 pm

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 */

User avatar
jvierra
Posts: 13682
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Feb 04, 2019 6: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 = $_.Name; LastSuccessTime = $keyValue }
        } Catch {
            Throw
        }
    } | 
    Export-Csv -Path $Csvfile -NoTypeInformation

User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Thu Feb 07, 2019 4:23 pm

jvierra wrote:
Mon Feb 04, 2019 6: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 */

User avatar
jvierra
Posts: 13682
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Thu Feb 07, 2019 5:27 pm

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

Locked