### Gathering Information About a Remote Workstation Using Powershell

Do you ever wish you could quickly and easily look up or verify information about a remote workstation? Well using Powershell, WMI, and a little .Net now you can! Follow along with me as we construct a script for gathering some information about a remote workstation.

Note: If you have never used powershell on your machine before you are going to need to run the powershell terminal as an administrator and then type the following and hit enter. This will allow you to run powershell scripts on your workstation after you hit Y to confirm you want to set that execution policy. You can read more about execution policies and the Set-ExecutionPolicy command here. You do not need to do this on target machines, but you WILL need to have your powershell terminal running under an account that has administrative rights on the machines you are running your scripts against.


Set-ExecutionPolicy RemoteSigned



Now onto business! We are going to write our script using the Powershell ISE (Integrated Scripting Environment), so go ahead and fire it up. You’ll be presented with the following:

So, how do we want to get the target machine from the user? We could use good old Read-Host assigned to a variable, but I prefer to use a mandatory parameter that the user uses when they call the script. If they omit it, the script will then ask for the missing information. We do this by including the following at the very beginning of the script:

param(
[Parameter(Mandatory=$true)] [alias("cn")] [string]$ComputerName
)



The [alias(“cn”)] line will allow the user to use either -ComputerName OR -cn to designate the target workstation. You can make the alias for the parameter whatever you like or omit it altogether.

Now, what kind of information are we likely to want from a remote workstation? My list includes things like the OS, model, serial number, BIOS version, what kind of processor it has, total system memory, IP address, and MAC address. Your list might have more or less, but this is what we will base our information gathering script on. Feel free to dig into the WMI documentation for other things you might want to include and try adding them to your version of the script.

param(
[Parameter(Mandatory=$true)] [alias("cn")] [string]$ComputerName
)

CLS
Write-Host "Operating System:" (gwmi win32_operatingsystem -ComputerName $ComputerName).Caption Write-Host "Model:" (gwmi win32_ComputerSystem -ComputerName$ComputerName).Model
Write-Host "Serial:" (gwmi win32_SystemEnclosure -ComputerName $ComputerName).SerialNumber Write-Host "BIOS Version:" (gwmi win32_BIOS -ComputerName$ComputerName).SMBIOSBIOSVersion
Write-Host "Processor:" (gwmi win32_Processor).Name
Write-Host "System Memory:" ([math]::truncate((gwmi win32_ComputerSystem -ComputerName $ComputerName).TotalPhysicalMemory / 1GB))"GB" Write-Host "IP Address:" ([system.net.dns]::Resolve($ComputerName)).AddressList.IPAddressToString



We are using WMI for everything but the IP address. To easily get that we will use a little bit of .NET. Read more about the system.net.dns class here. You can use .net by wrapping it in brackets [] so feel free to experiment with them in your powershell scripts as long as you have NET Framework installed on the target machines.

Once you are satisfied with the way it runs (F5 in the ISE to run and test it) save the file as a .ps1. When you are ready to use it you will simply open powershell, browse to the location of the file with cd c:\path\to\script\ and then run it against a target by typing script_name.ps1 -ComputerName TargetMachine or script_name.ps1 -cn TargetMachine (you did remember the alias bit, right?). If you want to be really fancy, you could put a help file inside your script. How you might ask? Why, it’s easy. Simply edit your script to add the following to the very beginning.


<#
.SYNOPSIS
Gather information from target workstation
.DESCRIPTION
This is a wrapper function that gathers information about a target PC
.PARAMETER ComputerName
The name of the Computer you want the script to run against
.EXAMPLE
EventLog -ComputerName SomeComputer
Runs the script against the workstation SomeComputer
.EXAMPLE
Eventlog -cn SomeOtherPC
Runs the script against the workstation SomeOtherPC using the -cn alias for -ComputerName
oppositeofserious.wordpress.com
#>



Adding that to the beginning of your script will allow users to use the built-in Get-Help function to get help on your script. It will look like the following if you use exactly what I have typed for each section.

They can use Get-Help Gather-Information.ps1 -examples to be given every example and description you have added, so add as many examples as you feel necessary.

Put it all together and your script should look something like this.


<#
.SYNOPSIS
Gather information from target workstation
.DESCRIPTION
This is a wrapper function that gathers information about a target PC
.PARAMETER ComputerName
The name of the Computer you want the script to run against
.EXAMPLE
EventLog -ComputerName SomeComputer
Runs the script against the workstation SomeComputer
.EXAMPLE
Eventlog -cn SomeOtherPC
Runs the script against the workstation SomeOtherPC using the -cn alias for -ComputerName
oppositeofserious.wordpress.com
#>

param(
[Parameter(Mandatory=$true)] [alias("cn")] [string]$ComputerName
)

CLS
Write-Host "Operating System:" (gwmi win32_operatingsystem -ComputerName $ComputerName).Caption Write-Host "Model:" (gwmi win32_ComputerSystem -ComputerName$ComputerName).Model
Write-Host "Serial:" (gwmi win32_SystemEnclosure -ComputerName $ComputerName).SerialNumber Write-Host "BIOS Version:" (gwmi win32_BIOS -ComputerName$ComputerName).SMBIOSBIOSVersion
Write-Host "Processor:" (gwmi win32_Processor).Name
Write-Host "System Memory:" ([math]::truncate((gwmi win32_ComputerSystem -ComputerName $ComputerName).TotalPhysicalMemory / 1GB))"GB" Write-Host "IP Address:" ([system.net.dns]::Resolve($ComputerName)).AddressList.IPAddressToString