Created
October 26, 2018 02:03
-
-
Save macostag/1c8769c825df26e642b2d17411dbe9f9 to your computer and use it in GitHub Desktop.
Powershell function example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-SystemInfo { | |
<# | |
.SYNOPSIS | |
Retrieves key system version and model information from one to ten computers. | |
.DESCRIPTION | |
Get-SystemInfo uses Windows Management Instrumentation (WMI) to retrieve information from one or more computers. | |
Specify computers by name or by IP address. | |
.INPUTS | |
System.String | |
.OUTPUTS | |
System.Management.Automation.PSCustomObject | |
.PARAMETER computername | |
One or more computer names or IP addresses, up to maximum of 10. | |
.PARAMETER LogErrors | |
Specify this switch to create a text log file of computers that could not be queried. | |
.PARAMETER ErrorLog | |
When used with -LogErrors, specifies the file path and name to wich failed computer names will be written. Defaults to c:\Retry.txt | |
.EXAMPLE | |
Get-Content names.txt | Get-SystemInfo | |
.EXAMPLE | |
Get-SystemInfo -ComputerName SRV-1,SRV-2 | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True, | |
ValueFromPipeline=$True, | |
HelpMessage="Computer name or IP address")] | |
[Alias('hostname')] | |
#Validate number of computers | |
[ValidateCount(1,10)] | |
[string[]]$ComputerName, | |
[string]$Errorlog = 'C:\Users\Public\retry.txt', | |
[switch]$LogError | |
) | |
BEGIN{ | |
Write-Verbose "Error log will be $ErrorrLog." | |
} | |
PROCESS{ | |
foreach($computer in $ComputerName){ | |
Write-Verbose "Querying $computer." | |
try{ | |
$everything_ok = $True | |
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer -ErrorAction Stop | |
Write-Debug "WMI Query" | |
}catch{ | |
$everything_ok = $false | |
Write-Warning "$computer failed - $_.Exception.Message" | |
if($LogError){ | |
"$computer : $_.Exception.Message" | Out-File $Errorlog -Append | |
Write-Warning "Logged to $Errorlog" | |
} | |
} | |
If ($everything_ok){ | |
$comp = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer | |
$bios = Get-WmiObject -Class Win32_Bios -ComputerName $computer | |
$props = @{ 'computerName' = $computer; | |
'OSVersion' = $os.version; | |
'SPVersion' = $os.servicepackmajorversion; | |
'BIOSSerial' = $bios.serialnumber; | |
'Manufacturer' = $comp.manufacturer; | |
'Model' = $comp.model } | |
Write-Verbose "WMI queries complete." | |
$obj = New-Object -TypeName PSObject -Property $props | |
$obj.PSObject.TypeNames.Insert(0,'MOL.SystemInfo') | |
Write-Output $obj | |
} | |
} | |
} | |
END{} | |
} | |
Get-SystemInfo -hostname localhost | Get-Member |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment