#Requires -version 2.0 #You need to collect the user name, computer name, domain name, and the operating system information #from a number of computers. To make matters easier, you have decided to write the information to a #text file. The text file needs to be saved as an ASCII file, and not as Unicode. You should include #the date in which the information was gathered to determine reliability of the information. #Design points # Extra points for a script that will work against a remote machine # Extra points for a script that will accept a Text file, CSV or other type of input of remote computer names # Extra points for adding useful help information and comments # Design points for clear easy to read code, and use of native Windows PowerShell cmdlets Param ( [string]$File="computers.txt", [string]$LogFile="ComputerReport.txt", [string]$ErrorLog="failed.txt", [Switch]$Append ) Set-StrictMode -Version 2 #Verify list of computers exists If (Test-Path -Path $File) { #if error log already exists, delete it so we get a new file if (Test-Path -Path $ErrorLog) { Remove-Item -Path $ErrorLog } #retrieve computer names filtering out any blank lines $Computers=Get-Content -Path $file | Where {$_} Write-Host "Gathering computer information. Please wait..." -ForegroundColor Cyan #initialize an array to hold computer information $data=@() #for each computer in the list Foreach ($Computer in $Computers) { #trim off any spaces $Computer=$Computer.Trim() #write the computername to the console as a status message Write-Host $Computer.toUpper() -ForegroundColor Cyan #ping the computer using Test-Connection. I'm saving the results to a variable #so that I can get the IP address. I set the ErrorAction preference to SilentlyContinue #to suppress error messages $ping=Test-Connection -ComputerName $Computer -Count 2 -ErrorAction "SilentlyContinue" if ($ping) { #if the computer is pingable get WMI information Try { #attempt to retrieve WMI information and if there is an error catch it $OperatingSystem=Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop $ComputerSystem=Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer -ErrorAction Stop #only process if WMI queries were sucessful if ($OperatingSystem -AND $ComputerSystem) { #create a custom object using a property hash table #I'm including ServicePack information since that might come in handy $data+=New-Object -TypeName PSObject -Property @{ User=$ComputerSystem.UserName Commputer=$OperatingSystem.CSName IPAddress=$ping[0].ipv4address.ToString() Domain=$ComputerSystem.Domain OS=$OperatingSystem.Caption ServicePack=$OperatingSystem.ServicePackMajorVersion Model=$ComputerSystem.Model Reported=(Get-Date) } } else { #otherwise record the failure in a log file $Computer.ToUpper() | Add-Content -Path $ErrorLog } } #close Try Catch { Write-Warning "There was a problem retrieving information from $($Computer.ToUpper())" #write the exception message as a warning Write-Warning $_.Exception.Message #write the failed computername to a text file $computer.ToUpper() | Add-Content -Path $ErrorLog } #close Catch scriptblock } #if pingable else { #computer is not pingable $Computer.ToUpper() | Add-Content -Path $ErrorLog } } #foreach #measure how many computers were queried and save the count property to a variable $count=($data | measure-object).Count Write-Host "Successfully queried $Count computers." -ForegroundColor Cyan #write the WMI information to a log file if $data has anything in it if ($count -gt 0) { #if a file was specified then record information if ($LogFile -AND $Append) { $data | Out-File -FilePath $LogFile -Encoding ASCII -Append } else { $data | Out-File -FilePath $LogFile -Encoding ASCII } } #if $count greater than 0 Write-Host "See $LogFile for details and $ErrorLog for failed computers." -ForegroundColor Cyan } #if file exisits else { #Failed to verify text file of computers Write-Warning "Cannot find or verify $file." } #end of script