Modifying PowerShell script to export to .CSV for the computer within specific OU

People,

Can anyone here please assist me in modifying the below powershell script so that it can be exported to CSV script for easy sorting ?

# This script shows the last time that a successfull Windows Update was installed.
cls
add-PSSnapin quest.activeroles.admanagement
$OnlineServers = @()

Get-QADComputer -SearchRoot 'domain.com/Terminal Servers/Production 1' -OSName "Windows*Server*" | where {$_.accountisdisabled -eq $false} | % {
  
  $PingResult = Get-WmiObject -Query "SELECT * FROM win32_PingStatus WHERE address='$($_.Name)'"
  
  If ($PingResult.StatusCode -eq 0) {
     # Add the current name to the array
    $OnlineServers += "$($_.Name)"
  }
}

foreach ($Server in $OnlineServers ) {
		$key = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install"
		$keytype = [Microsoft.Win32.RegistryHive]::LocalMachine
		$RemoteBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($keytype,$Server)
		$regKey = $RemoteBase.OpenSubKey($key)
		$KeyValue = $regkey.GetValue("LastSuccessTime")
	
		$System = (Get-Date -Format "yyyy-MM-dd hh:mm:ss") 
			
		if	($KeyValue -lt $System)	{
			Write-Host " "
			Write-Host $Server "Last time updates were installed was: " $KeyValue
		}
}

Open in new window


Note:

Feel free to replace the Get-QADComputerr with the builtin Get-ADComputerAccount and use Test-Connection instead of the existing convoluted way of doing it.

Ideally the exported result can just be:

ComputerName, last time windows updated, what KBnumber applied if possible.

Thanks in advance,
LVL 12
Senior IT System EngineerSenior Systems EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
If you want to export to .CSV you have to know what columns you want (i.e. their headers).  I've taken a best guess.  The below should work but I haven't tested.
# This script shows the last time that a successfull Windows Update was installed.
cls
add-PSSnapin quest.activeroles.admanagement
$OnlineServers = @()

Get-QADComputer -SearchRoot 'domain.com/Terminal Servers/Production 1' -OSName "Windows*Server*" | where {$_.accountisdisabled -eq $false} | % {
  
  $PingResult = Get-WmiObject -Query "SELECT * FROM win32_PingStatus WHERE address='$($_.Name)'"
  
  If ($PingResult.StatusCode -eq 0) {
     # Add the current name to the array
    $OnlineServers += "$($_.Name)"
  }
}

$(foreach ($Server in $OnlineServers ) {
		$key = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install"
		$keytype = [Microsoft.Win32.RegistryHive]::LocalMachine
		$RemoteBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($keytype,$Server)
		$regKey = $RemoteBase.OpenSubKey($key)
		$KeyValue = $regkey.GetValue("LastSuccessTime")
	
		$System = (Get-Date -Format "yyyy-MM-dd hh:mm:ss") 
			
		if	($KeyValue -lt $System)	{
			New-Object PsObject -properties @{
			                            Server = $Server
                                                    UpdatesLastInstalled = $KeyValue
                                                    }
		}
}) | Export-CSV file.csv -notype

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Senior IT System EngineerSenior Systems EngineerAuthor Commented:
Foo,

Somehow it is failed with empty CSV result file and the below error message:

New-Object : A parameter cannot be found that matches parameter name 'properties'.
At C:\Users\Admin\AppData\Local\Temp\54d4cccd-5dae-4265-9685-9782db9edfe3.ps1:25 char:24
+             New-Object PsObject -properties @{
+                                 ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Object], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

Open in new window

footechCommented:
Sorry, should be "-property".  Sometimes you can use either.
Senior IT System EngineerSenior Systems EngineerAuthor Commented:
You are awesomely cool Foo :-)
thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.