Here’s a handy little script to get the OS install date from your servers and export all the information to a CSV file.
The script is split into three sections variables, functions and script main. The script loops through all servers in the “servers.txt” file (must be placed in the same directory as the script) and gets the OS install date using WMI.
Variables
Below is a short walkthrough of the variables section of the script.
#VARIABLES $scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath $servers = @(gc "$dir\servers.txt")
The first two lines of code –
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
are really practical as it gives me an object $dir which always points to where the script was run. This makes the script more robust as I can freely move the script around and run it from many locations without having to change any paths in the script.
$servers is an array populated by running Get-Content on the servers.txt file.
Functions
The script uses two functions to do most of the work.
1. Function Get-OSInstallDate
function Get-OSInstallDate { param ( $computerName = $Env:COMPUTERNAME ) [datetime]$OSInstallDate = ([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem -ComputerName $computerName).InstallDate) if ($?) { return $OSInstallDate } }
This function takes a parameter $computerName and uses the WMI class Win32_OperatingSystem to get the OS install date. The OS install date is also converted so we can return it as a neat datetime object.
2. Function ExportSpecial-CSV
function ExportSpecial-CSV { param ( $inputArray, $InfoType ) $inputArray | Export-Csv "$dir\$InfoType.csv" -noTypeInformation -Force }
This function is extremely simple. It takes an array as input and exports it to CSV in the script directory.
Script Main
$OSInstallDates = @() $servers | % { $date = Get-OSInstallDate -computerName $_ if ($date) { $OSInstallDate = "" | Select "Computer","OSInstallDate" $OSInstallDate.Computer = $_ $OSInstallDate.OSInstallDate = $date $OSInstallDates += $OSInstallDate } } ExportSpecial-CSV -inputArray $OSInstallDates -InfoType "OS_Install_dates"
In the script main I loop through $servers and run the Get-OSInstallDate function to get the info. If the OS install date was retrieved successfully A custom object $OSInstallDate with two properties “Computer” and “OSInstallDate” is created and then added to the array $OSInstallDates. After the foreach loop I use the function ExportSpecial-CSV to export the array $OSInstallDates to the script directory.
That’s it!
Below is the full script. Good luck!
################################################################################################################## ##Script: Get-OSInstallDate.ps1 ##Description: Script uses WMI to get the OS install date from the servers listed in "servers.txt" and then #+ exports the information to CSV. ##Usage: Create a file "servers.txt" in the script directory and put in the names of the servers to query. #+ Run the script and it will create a file called "OS_Install_Dates.csv" in the same directory. ##Created by: Noam Wajnman ##Creation Date: February 5, 2014 ################################################################################################################### #FUNCTIONS function Get-OSInstallDate { param ( $computerName = $Env:COMPUTERNAME ) [datetime]$OSInstallDate = ([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem -ComputerName $computerName).InstallDate) if ($?) { return $OSInstallDate } } function ExportSpecial-CSV { param ( $inputArray, $InfoType ) $inputArray | Export-Csv "$dir\$InfoType.csv" -noTypeINformation -Force } #VARIABLES $scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath $servers = @(gc "$dir\servers.txt") #SCRIPT MAIN $OSInstallDates = @() $servers | % { $date = Get-OSInstallDate -computerName $_ if ($date) { $OSInstallDate = "" | Select "Computer","OSInstallDate" $OSInstallDate.Computer = $_ $OSInstallDate.OSInstallDate = $date $OSInstallDates += $OSInstallDate } } ExportSpecial-CSV -inputArray $OSInstallDates -InfoType "OS_Install_dates"
Thank you for sharing this – exactly what I was looking for today. 🙂
Glad to help! :-0