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

Wednesday, September 18, 2013

Deleting Roaming Profiles in Citrix/Terminal Server WITH GUI

Here is a nifty little script that can be used by a helpdesk in a Windows 2008/R2 Citrix environment, that uses roaming profiles. In the case of profile corruption, it will go through each server and delete the local profile, then delete the profile in the central store.
It is based on PowerShell.

Specify your servers on line 11, your domain(s) on line 13 and your roaming profiles store on line 15.
<#

Title: DeleteCitrixUserProfile.ps1
Description: Deletes Citrix User Profiles from Citrix Servers
Comments: None
Author: Dani (http://syscript.blogspot.com)
Original Date: July 5, 2013
Modified Date: September 18, 2013
Version: 1.0
#>
#Computer List of Servers to check
$ComputerList = @("Server1","Server2","Server3")
#Domain List of User profile to specify (Roaming profiles store needs this information)
$DomainList = @("DOMAIN1","DOMAIN2")
#Roaming Profiles Store, include tailing back-slash
$RoamingProfilesStorage = "\\Server\Store\Profiles"

Function Ping($computername)
{
$query = "select * from win32_pingstatus where address = '" + $computername + "'"
$wmi = get-wmiobject -query $query
if ($($wmi.statuscode) -eq $null)
{
$rtnvalue = $($wmi.PrimaryAddressResolutionStatus)
}
else {$rtnvalue = $($wmi.statuscode)}
if ( $rtnvalue -eq 0 ) {$true}
else {$false}
}

Function WMIConnectionTest($computername)
{
$wmiClass = "Win32_OperatingSystem"
if ((Get-WmiObject -ComputerName $computerName win32_operatingsystem -ErrorAction silentlycontinue))
{ return $true }
else { return $false }
}

Function CleanProfile($username,$domain)
{
Foreach ($Computername in $ComputerList)
{
#echo $computername
if (Ping($ComputerName) -eq $true -and if (WMIConnectionTest($ComputerName) -eq $true))
{
if ((Test-Path ("\\$ComputerName\c$\Users\$username")) )
{
Remove-Item -Recurse -Force ("\\$ComputerName\c$\Users\$username")
}
if ((Test-Path ("\\$ComputerName\c$\Users\$username`.$Domain")) )
{
Remove-Item -Recurse -Force ("\\$ComputerName\c$\Users\$username`.$Domain")
}
}

}
if (Test-Path ("$RoamingProfilesStorage\$username`.$domain`.V2"))
{
Remove-Item -Recurse -Force ("$RoamingProfilesStorage\$username`.$domain`.V2")
}

}

#GUI Controls
#Layout is specified as (X,Y)


[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "DeleteCitrixUserProfile.ps1"
$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(300,20)
$objLabel.Text = "Please enter username to clean up:"
$objForm.Controls.Add($objLabel)

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

$objDomainList = New-Object System.Windows.Forms.ComboBox
$objDomainList.Location = New-Object System.Drawing.Size(10,45)
$objDomainList.Size = New-Object System.Drawing.Size(150,30)
foreach($userdomains in $DomainList)
{
$objDomainList.Items.Add($userdomains) > $null
}
$objDomainList.SelectedIndex= 0
$objForm.Controls.Add($objDomainList)

$objForm.Topmost = $True

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

$x = $objTextBox.Text
$y = $objDomainList.SelectedItem.ToString()
if ($x -ne "" -and $x -ne $null)
{
$confirm = [System.Windows.Forms.MessageBox]::Show("Are you sure you would like to remove the Citrix profile for $y\" + $x + "?","Confirmation","YesNo","Warning")

if ($confirm -eq "Yes" -and $x -ne "" -and $x -ne $null)
{
# Do Code Here
#[System.Windows.Forms.MessageBox]::Show("Presed yes, will perform operation on " + $x + ".","Confirmation","OK","Information")
#Executes actual code
CleanProfile -username $x -domain $y
}
else {
[System.Windows.Forms.MessageBox]::Show("Will not perform operation on " + $x + ".","Confirmation","OK","Error")
}
}
[System.Windows.Forms.MessageBox]::Show("The Program has completed")

Thursday, August 15, 2013

First Script - Computer Hardware Inventory Script that helps with SQL Licensing

Here is my first Script!
It is based on PowerShell.

It queries each computer from a text file (one computer line by line) and gathers some hardware information like number of CPUs, physical memory, model etc and logs it to a file. The script will also search for SQL instances and will gather the details on each SQL instance setup on the system as well.

This might come in handy during a licensing true-up...

<#
Title: GetHardwareInfo-forLicensing.ps1
Description: Gathers OS, CPU, Memory and SQL Information from remote systems
Comments: None
Author: Daniel Sarfati
Original Date: August 15, 2013
Modified Date: 
Version: 1.4
#>
Add-type -AssemblyName "System.Windows.Forms"
$HKLM = [UInt32] "0x80000002"
#Custom Ping function which returns 0 if ping fails, 1 if ping passes
Function Ping($computername)
{
 $query = "select * from win32_pingstatus where address = '"   $computername   "'"
 $wmi = get-wmiobject -query $query
 if ($($wmi.statuscode) -eq $null)
  {
  $rtnvalue = $($wmi.PrimaryAddressResolutionStatus)
  }
 else {$rtnvalue = $($wmi.statuscode)}
 if ( $rtnvalue -eq 0 ) {$true}
 else {$false}
 
}
#Custom WMI Test function, just in case ping is working but WMI is down, PS shouldn't throw an error
Function WMIConnectionTest($computername)
{
 $wmiClass = "Win32_OperatingSystem"
 if ((Get-WmiObject -ComputerName $computerName win32_operatingsystem -ErrorAction silentlycontinue)) 
  { return $true } 
 else { return $false } 
}

#Main Code of the program
Function Get-HardwareDetails($ComputerName)
{
 #Create a WMI Connection to the destination computer's Win32_OperatingSystem namespace
 $OSWMI = Get-WmiObject -ComputerName $ComputerName win32_operatingsystem
 #Get some OS Information
 $OperatingSystem = $OSWMI.Caption
 $ServicePack = $OSWMI.ServicePackMajorVersion
 #Close WMI Win32_OperatingSystem connection
 $OSWMI = $null
 #Create a WMI connection to the destination computer's Win32_Processor namespace
 $ProcWMI = Get-WmiObject -ComputerName $ComputerName Win32_Processor
 #Get some Processor information, including CPU architecture x86/x64, number of cores, number of procs, etc
 $Architecture = @($ProcWMI.AddressWidth)[0]
 $CPUDescription = @($ProcWMI.Name)[0]
 $Cores = @($ProcWMI.NumberOfCores)[0]
 $ProcessorCount = @($ProcWMI.NumberOfLogicalProcessors).count
 #Close WMI Win32_OperatingSystem connection
 $ProcWMI = $null
 #Create a WMI Connection to the destination computer's Win32_OperatingSystem namespace
 $ComputerWMI = Get-WmiObject -ComputerName $ComputerName Win32_ComputerSystem
 #Get some memory and computer model information
 $TotalMemory = $ComputerWMI.TotalPhysicalMemory
 $Manufacturer = $ComputerWMI.Manufacturer
 $Model = $ComputerWMI.Model
 $ComputerWMI = $null
 #$ComputerIPs = @(([System.Net.Dns]::GetHostAddresses($ComputerName)))
 #This only works with Windows 2003 and higher
 #$SQLInstances = @(Get-WmiObject -ComputerName $computername -query "select * from win32_service where Name LIKE 'MSSQL%' and Description LIKE '%transaction%'").Count
 #Get a list of all services, for Windows 2000 Compatibility, WQL doesn't have the like operand
 $AllServices = Get-WmiObject -ComputerName $computername -query "select * from win32_service"
 #Filter for SQL Transaction service instances
 $SQLInstances = @($AllServices | where-object {$_.name -like "*MSSQL*" -and $_.description -like "*transaction*"}).count 
 #if any SQL Transaction services exists
 if ($SQLInstances -gt 0)
 {
  #Create an array of SQL service instances to investigate
  $SQLRoot = @($AllServices | where-object {$_.name -like "*MSSQL*" -and $_.description -like "*transaction*"}) | Select-Object pathname
  foreach ($SQL in $SQLRoot)
  {
   $binPath=$SQL.pathname.ToString()
   #Get the path of the binn folder from the service path, replace the : with $ and remove the quotes from the path string
   $LocalSqlInstPath=$binPath.Substring(0,$binPath.IndexOf("Binn")) -replace ":", "$" -replace "`"","" #"
   #Build the folder path, as we will connect to the remote server through UNC
   $sqlInstPath = '\\'   $ComputerName  '\'   $LocalSqlInstPath
   #Get the information and save it to the array
   $thisInstance = GetSQLInformation $sqlInstPath
   $Script:AllSQLInstances =@($thisInstance)
  }
 }
 #Close the services query
 $AllServices = $null
 #Build the object we will send back for easier CSV exports and return it
 $CompOSObj = New-Object PSObject
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name ComputerName -Value $ComputerName
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name Connection -Value $true
 #Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name ComputerIP -Value $ComputerIP
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name OperatingSystem -Value $OperatingSystem
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name ServicePack -Value $ServicePack
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name Architecture -Value $Architecture
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name CPUDescription -Value $CPUDescription
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name NumberOfCores -Value $Cores
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name NumberOfProcessors -Value $ProcessorCount
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name TotalMemory -Value $TotalMemory
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name ComputerManufacturer -Value $Manufacturer
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name ComputerModel -Value $Model
 Add-Member -InputObject $CompOSObj -MemberType NoteProperty -Name SQLInstances -Value $SQLInstances
 Return $CompOSObj
}

Function GetSQLInformation($sqlInstPath)
{
 #Build the sql log path
 $SqlLog=  $sqlInstPath   'Log\Errorlog'
 #Open the Errorlog file, and find the Edition string
 $SqlLogFile = get-content $sqllog
 $Edition = ($SqlLogFile | select-string Edition).ToString()
 $Edition = $Edition.Trim()
 $Version = $SqlLogFile[0].ToString()
 #With the Errorlog file open, and find Microsoft string, this will tell us what SQL version it is
 $SQLVersion = $Version.Substring($Version.IndexOF("Microsoft"))
 #Build the object we will send back for easier CSV exports and return it
 $SQLInstanceObj = New-Object PSObject
 Add-Member -InputObject $SQLInstanceObj -MemberType NoteProperty -Name ComputerName -Value $ComputerName
 Add-Member -InputObject $SQLInstanceObj -MemberType NoteProperty -Name SQLEdition -Value $Edition
 Add-Member -InputObject $SQLInstanceObj -MemberType NoteProperty -Name SQLVersion -Value $SQLVersion
 $SqlLogFile=$null
 Return $SQLInstanceObj
}

#Open a file dialog box
$fd = new-object System.Windows.Forms.OpenFileDialog
$fd.title = %u201CSelect Input File%u201D
$fd.Filter = %u201CText Files (*.txt) | *.txt%u201D
$fd.ShowHelp = $true
$fd.ShowDialog()

#$InputFile = Read-Host -Prompt "Please enter the name of the file you'd like to scan for" -
$ComputerList = Get-Content -Path $fd.FileName
forEach ($ComputerName in $ComputerList)
{
 #If the system is pingable and passes WMI tests, connect to it run the main function in the program, save that output to array a
 if (Ping($ComputerName) -eq $true -and if (WMIConnectionTest($ComputerName) -eq $true))
 {
  $a =@(Get-HardwareDetails $ComputerName)
 }
 #if the system can't connect, build an object and save it to array a
 Else
 {
  $CantConnectObj = New-Object PSObject 
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name ComputerName -Value $ComputerName
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name Connection -Value $false
  #Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name ComputerIP -Value $ComputerIP
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name OperatingSystem -Value $false
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name ServicePack -Value $false
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name Architecture -Value $false
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name CPUDescription -Value $false
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name NumberOfCores -Value $false
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name NumberOfProcessors -Value $false
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name TotalMemory -Value $false
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name ComputerManufacturer -Value $false
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name ComputerModel -Value $false
  Add-Member -InputObject $CantConnectObj -MemberType NoteProperty -Name SQLInstances -Value $false
  $a =@($CantConnectObj)
 }
}

$a | Export-Csv -Path ".\Export-List-HardwareDetails.csv" -notypeinformation
$Script:AllSQLInstances | Export-Csv -Path ".\Export-List-SQLInstanceDeatils.csv" -notypeinformation

Tuesday, August 13, 2013

Welcome

Welcome to my Blog!

I write a lot of Windows focused system administration scripts at work, and decided I should probably start a blog about it.

My strongest language is VBScript, although I have done some coding with PHP, ASP, ASP.NET, Java. My latest passion is with PowerShell, so I hope to build a nice little script repository between PowerShell and VBScript on this blog.

I'm very passionate about IT, and am well versed with MS Exchange, Citrix XenApp, Windows Servers, Active Directory, and VMware. In addition I have a working knowledge of Linux (my latest distro I'm using is CentOS, have used Ubuntu, Red Hat and FreeBSD in the past), though my shell scripting skills aren't quite there yet. I also love Android devices and am a very active member on a number of android related forums.