#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 Function Get-ComputerInfo { <# .Synopsis Get computer system information .Description This function will gather computer and operating system information from computers in the domain. Domain : jdhlab.local Commputer : SERVER01 Reported : 3/22/2011 8:11:51 AM ServicePack : 1 User : Windows User OS : Microsoft Windows Server 2008 R2 Enterprise You can also specify a file name to create an ASCII report. Use -Append if you want to keep an existing file. Computers that can't be contacted will be recorded in an error log. The default file name is Failed.txt and it will be appended. .Parameter Computername The name of the computer you want to query. The default is the local host. .Parameter FilePath The filename and path for the report. .Parameter Append Specify that you want to append to an existing report. .Parameter ErrorLog The filename and path to record the names of computers with failed connection attempts .Example PS C:\> Get-Content R:\Computers.txt | Get-ComputerInfo -filepath R:\CSData.txt -append Go through the list of computers in R:\Computers.txt and append the results to R:\CSData.txt. Computers that can't be queried will be recorded in C:\failed.txt .Example PS C:\> Get-ComputerInfo "client1","client2","client3" Get computer and operating system information for these 3 computers and write the results to the pipeline. .Example PS C:\> Get-ADComputer -filter * | foreach {Get-ComputerInfo $_.name -filepath R:\DomainReport.txt -ErrorLog R:\DomainError.txt} Assuming the Microsoft Active Directory module is loaded, get all computers in Active Directory and pipe them to the function. A report will be created in R:\DomainReport.txt and failures will be recorded in R:\DomainError.txt. .Notes NAME: Get-ComputerInfo AUTHOR: Jeffery Hicks VERSION: 1.0 LASTEDIT: 03/22/2011 .Link Get-WMIObject .Inputs Strings .Outputs Custom object #> [cmdletBinding()] Param ( [Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] [ValidateNotNullorEmpty()] [Alias("name")] [string[]]$Computername=$env:Computername, [Parameter(Position=1,Mandatory=$False,HelpMessage="Enter the full filename and path for your report.")] [string]$FilePath, [switch]$Append, [string]$ErrorLog="Failed.txt" ) Begin { Set-StrictMode -Version 2 Write-Verbose "$(Get-Date) Starting script" Write-Host "Gathering computer information. Please wait..." -ForegroundColor Cyan #initialize an array to hold computer information $data=@() } #close Begin scriptblock Process { #process the array of computernames either piped in or passes as parameter values Foreach ($computer in $computername) { Try { Write-Verbose "$(Get-Date) Getting information from $($Computer.toUpper())" #write a progress message on the console if a file path was specified #otherwise nothing is displayed which for a long running command migh #lead the user to think something is wrong. When a file is not used each #computer is written to the console if ($Filepath) { Write-Host $Computer.toUpper() -ForegroundColor Cyan } #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 $object=New-Object -TypeName PSObject -Property @{ User=$ComputerSystem.UserName Commputer=$OperatingSystem.CSName Domain=$ComputerSystem.Domain OS=$OperatingSystem.Caption ServicePack=$OperatingSystem.ServicePackMajorVersion Model=$ComputerSystem.Model Reported=(Get-Date) } if ($FilePath) { #add the object to the data array in case we want to save it to a file $data+=$object } else { #Otherwise write the new object to the pipeline Write-Output $object } } else { #display a warning if the WMI information is not present. This should never really #be reached. Write-Warning "There was an unknown problem. Not all information was available." } } #close Try scriptblock 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 } #close Foreach } #close Process script block End { #if a file was specified then record information if ($FilePath -AND $Append) { Write-Verbose "$(Get-Date) Appending to $filepath" $data | Out-File -FilePath $Filepath -Encoding ASCII -Append } elseif ($FilePath) { Write-Verbose "$(Get-Date) Writing data to $filepath" $data | Out-File -FilePath $Filepath -Encoding ASCII } Write-Verbose "$(Get-Date) Ending script" } #close End scriptblock } #end Function #end of script