Wednesday, October 2, 2013

VBScript vs PowerShell - SupportInfo Script

So I've been watching some advanced PowerShell (PS) training videos in advance of a major scripting endeavor that my boss has asked me to come complete and wanted to play with some of my old scripts again.

This was the first real script I "ported" to PowerShell. I've optimized the original VBScript code a little bit since when I first wrote it in 2010, but the bulk of it is the same. I added some of the advanced functions into the PowerShell version today, to create a much more powerful version of SupportInfo. The PS version acts as a kind of CmdLet, since also includes the ability to use export-csv, and you can specify more than one server when calling it through a command line. Tomorrow I'll try to have it run with Invoke-Command so it can be multi-threaded, but for now here are the scripts!
PowerShell Code:
<#
Title: SupportInfoMultiplePCs.ps1
Description: Gathers some basic support information from a remote computer system
Comments: None
Author: Daniel Sarfati
Original Date: April 5 2013
Modified Date: October 2 2013
Version: 1.5
#>
[CmdletBinding()]
param(
[String[]]$ComputerName
)
Begin{
$ScriptName = $MyInvocation.MyCommand.Name
Add-type -AssemblyName "System.Windows.Forms"
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

Function Get-LocalTime($UTCTime)
{
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ)
Return $LocalTime
}

Function objWMIExecutionString([string]$computerName,[string]$NameSpace,[string]$Class)
{
if ($computerName -ne "." -and $computerName -ne $env:COMPUTERNAME)
{
if ($Script:currentCredential -eq $null)
{
Set-Variable -Name currentCredential -Value (Get-Credential) -Scope Script
}
return "Get-WMIObject -Credential $" + "currentCredential -ComputerName $ComputerName -Impersonation Impersonate -NameSpace $NameSpace -Class $Class "
}
else
{
$Script:computername = $env:COMPUTERNAME;
$computerName = $env:COMPUTERNAME;
return "Get-WMIObject -Impersonation Impersonate -NameSpace $NameSpace -Class $Class"
}
}

Function objWMIQueryString([string]$computerName,[string]$Query)
{
if ($computerName -ne "." -and $computerName -ne $env:COMPUTERNAME)
{
if ($Script:currentCredential -eq $null)
{
Set-Variable -Name currentCredential -Value (Get-Credential) -Scope Script
}

return "Get-WMIObject -Credential $" + "currentCredential -ComputerName $ComputerName -Impersonation Impersonate -Query $Query "
}
else
{
return "Get-WMIObject -Impersonation Impersonate -Query $Query "
}
}

Function GenerateForm(){
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "$ScriptName"
$objForm.Size = New-Object System.Drawing.Size(350,140)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x = $objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,75)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x = $objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,75)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,5)
$objLabel.Size = New-Object System.Drawing.Size(250,25)
$objLabel.Text = "Please enter computer name to connect to, or use ""."" for localhost"
$objForm.Controls.Add($objLabel)

$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,35)
$objTextBox.Size = New-Object System.Drawing.Size(300,20)
$objForm.Controls.Add($objTextBox)

$objForm.Controls.Add($objDomainList)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

return $objTextBox.Text

}
if (@($ComputerName)[0] -eq "" -or $ComputerName -eq $null)
{$ComputerName = GenerateForm}
}
process
{
#if ($strComputerName -ne "." -and $strComputerName -ne $env:COMPUTERNAME) {
#$objWMIService = Get-WmiObject -Credential $credential -ComputerName $strComputerName -Impersonation Impersonate -Namespace root\CIMV2 -Class Win32_PerfFormattedData_PerfOS_System }
#else {$strComputerName = $env:COMPUTERNAME;$objWMIService = Get-WmiObject -Impersonation Impersonate -Class Win32_PerfFormattedData_PerfOS_System}
function RunProgram($ComputerName)
{
$objWMIService = Invoke-Expression -Command (& objWMIExecutionString -computerName $ComputerName -NameSpace "root\cimv2" -Class "Win32_OperatingSystem" )
$t=$objWMIService.ConvertToDateTime($objWMIService.LocalDateTime) – $objWMIService.ConvertToDateTime($objWMIService.LastBootUpTime)
$uptimeText = "The system $ComputerName has been on for $($t.Days) day(s), $($t.hours) hour(s), $($t.minutes) minute(s)."
$objWMIService = Invoke-Expression -Command (& objWMIExecutionString -computerName $ComputerName -NameSpace "root\cimv2" -Class "Win32_ComputerSystem" )
$computerModel = $objWMIService.Model
$objWMIService = Invoke-Expression -Command (& objWMIExecutionString -computerName $ComputerName -NameSpace "root\cimv2" -Class "Win32_BIOS" )
$BiosVer = $objWMIService.SMBIOSBIOSVersion
$SerialNumber = $objWMIService.SerialNumber
$BiosDate = $objWMIService.ConvertToDateTime($objWMIService.ReleaseDate)
$objWMIService = Invoke-Expression -Command (& objWMIQueryString -computerName $ComputerName -Query '"Select * from Win32_NetworkAdapterConfiguration where IPEnabled = True"')
$IPAddresses = $objWMIService.IPAddress
if ( $objWMIService -ne $null )
{
$Prop=@{
'ComputerName' = $ComputerName
'IP Addresses' = [String]$IPAddresses
'Username' = "$env:USERDOMAIN\$env:USERNAME"
'Computer Model'= $ComputerModel
'Bios Version'= $BiosVer
'Bios Date'= $BiosDate
'Serial Number'=$SerialNumber
'UptimeText'=$uptimeText
}
$obj=New-Object -TypeName PSObject -Property $Prop
Write-Output $obj
}
else
{ "The Script was unable to connect to $computername" }
}
foreach ($Computer in $ComputerName)
{
RunProgram($Computer)
}
}

End{
#"The script is done"
}
VBScript Code:
dim objWMIService, objWScript, colOperatingSystems, objOS
dim uptimeSeconds, upDays, upHours, upMinutes
dim textDays, textHours, textMinutes
strComputer = "."

OriginalLocale = GetLocale()
SetLocale(1033)

Function GetUptime(uptimeSeconds)
upDays = Int(uptimeSeconds/86400)
upHours = Int((uptimeSeconds-upDays*86400)/3600)
upMinutes = Int((uptimeSeconds-upDays*86400-upHours*3600)/60)
If upDays = 0 Then
textDays = ""
ElseIf textDays = 1 Then
textDays = "1 day "
Else
textDays = upDays & " days "
End If
If upHours = 0 Then
textHours = ""
ElseIf upHours = 1 Then
textHours = "1 hour "
Else
textHours = upHours & " hours "
End If

If upMinutes = 1 Then
textMinutes = "1 minute"
Else
textMinutes = upMinutes & " minutes"
End If
GetUptime = textDays & textHours & textMinutes
End Function

Function fnDate(utcDate)
strDateSeperator = "/"
strDay = Mid(utcDate, 7, 2)
strMonth = Mid(utcDate, 5, 2)
strYear = Left(utcDate, 4)
fnDate = strDay & strDateSeperator & StrMonth & strDateSeperator & strYear
fnDate = FormatDateTime(fnDate)
End Function

Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & _
Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
13, 2))
End Function

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'On Error Resume Next
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
computerUptime = GetUptime(DateDiff("s",WMIDateStringToDate(dtmBootup),now))
Next
SetLocale(OriginalLocale)

'Found somewhere on the internet, modified by Dani to display serial number in an InputBox (for copy and paste) 03-08-05

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)

For Each objItem in colItems
strModelNumber = objItem.Model
Next

set objItems = nothing
set colItems = nothing

Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS")

For Each objItem in colItems
strManufacturer = objItem.Manufacturer
strSerialNumber = objItem.SerialNumber
strBIOSInfo = objItem.SMBIOSBIOSVersion & " " & fnDate(objItem.ReleaseDate)
Next

set objItems = nothing
set colItems = nothing

Set colAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objAdapter in colAdapters
if objAdapter.IPAddress(0) <> "0.0.0.0" then
arrIPInfo=objAdapter.IPAddress(0) & " | " & arrIPInfo
end if
Next

Set WshNetwork = WScript.CreateObject("WScript.Network")
username = WshNetwork.UserDomain & "\" & WshNetwork.UserName
myComputerName = WshNetwork.ComputerName

InputBox "Computer Name : " & WshNetwork.ComputerName & vbCrlf & "IP Address : " & arrIPInfo & vbCrlf & "Username : " & username & vbCrlf & "Computer Type : " & strManufacturer & " " & strModelNumber & vbCrlf & "Bios Info : " & strBIOSInfo & vbCrlf & "Uptime : " & computerUptime,"My System Information Screen",strSerialNumber

Set WshNetwork = nothing

No comments:

Post a Comment