Skip to content

Instantly share code, notes, and snippets.

@cmaahs
Created December 14, 2011 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmaahs/1477976 to your computer and use it in GitHub Desktop.
Save cmaahs/1477976 to your computer and use it in GitHub Desktop.
Useful Powershell Functions
Function Get-OSInfo {
param($RemotePC)
$OSInfo = "" | Select OSName,OSVersion,OSArchitecture,OSFamily,OSType,Status
$OSInfo.Status = $true
$PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$RemotePC'" | Select-Object StatusCode
If ($PingStatus.StatusCode -eq 0) {
$error.Clear()
Try {
$CompSys = Gwmi Win32_ComputerSystem -Comp $RemotePC -ErrorAction Stop
} Catch {
$OSInfo.Status = $false
$OSInfo.OSName = "ERROR"
Return $OSInfo
}
If ( $CompSys.Name -eq $RemotePC ) {
$error.Clear()
Try {
$OSInfo.OSVersion = (Gwmi -Class Win32_OperatingSystem -Computer $RemotePC).Version
} Catch {
$OSInfo.Status = $false
$OSInfo.OSVersion = "UNDETERMINED"
return $OSInfo
}
$Error.Clear()
Try {
$OSInfo.OSName = (Gwmi -Class Win32_OperatingSystem -Computer $RemotePC).Caption
} Catch {
$OSInfo.Status = $false
$OSInfo.OSName = "UNDETERMINED"
return $OSInfo
}
$Error.Clear()
Try {
$OSInfo.OSArchitecture = (Get-WmiObject Win32_Processor -ComputerName $RemotePC -NameSpace root\cimv2 | Where {$_.DeviceID -eq "CPU0"}).AddressWidth
} Catch {
$OSInfo.Status = $false
$OSInfo.OSArchitecture = "UNDETERMINED"
return $OSInfo
}
#}
#1 Work Station
#2 Domain Controller
#3 Server
$Error.Clear()
Try {
$ProductType = (Gwmi -Class Win32_OperatingSystem -Computer $RemotePC).ProductType
} Catch {
$OSInfo.Status = $false
$OSInfo.OSType = "UNDETERMINED"
return $OSInfo
}
Switch ($ProductType) {
1 { $OSInfo.OSType = "Workstation" }
2 { $OSInfo.OSType = "DomainController" }
3 { $OSInfo.OSType = "Server" }
default { $OSInfo.OSType = "Other" }
}
Switch -wildcard ($OSInfo.OSVersion) {
"6.1*" {
If ( $OSInfo.OSType -eq "Workstation" ) { $OSInfo.OSFamily = "WIN7" }
If ( ( $OSInfo.OSType -eq "Server" ) -or ( $OSInfo.OSType -eq "DomainController" ) ) { $OSInfo.OSFamily = "WIN2008R2" }
}
"6.0*" {
If ( $OSInfo.OSType -eq "Workstation" ) { $OSInfo.OSFamily = "WINVISTA" }
If ( ( $OSInfo.OSType -eq "Server" ) -or ( $OSInfo.OSType -eq "DomainController" ) ) { $OSInfo.OSFamily = "WIN2008" }
}
"5.2*" {
If ( $OSInfo.OSType -eq "Workstation" ) { $OSInfo.OSFamily = "WINXP64" }
If ( ( $OSInfo.OSType -eq "Server" ) -or ( $OSInfo.OSType -eq "DomainController" ) ) { $OSInfo.OSFamily = "WIN2003" }
}
"5.1*" {
If ( $OSInfo.OSType -eq "Workstation" ) { $OSInfo.OSFamily = "WINXP" }
}
"5.0*" {
If ( $OSInfo.OSType -eq "Workstation" ) { $OSInfo.OSFamily = "WIN2000WKS" }
If ( ( $OSInfo.OSType -eq "Server" ) -or ( $OSInfo.OSType -eq "DomainController" ) ) { $OSInfo.OSFamily = "WIN2000SRV" }
}
}
return $OSInfo
}
} else { #Cannot ping
$OSInfo.Status = $false
$OSInfo.OSName = "OFFLINE"
Return $OSInfo
}
}
Function Get-WhereLoggedOn {
param(
[parameter(Mandatory=$True)][string]$UserName,
[parameter(Mandatory=$false)][string]$ShareServer
)
If ( -not ( $ShareServer ) ) {
$ShareServer = "PRIMARYSHARESERVER"
#EDIT THIS ^^^^^^^^^^^^^^^^^^
}
$userSessions = Get-WmiObject -Class Win32_ServerConnection -ComputerName $ShareServer | Where-Object { ($_.UserName).ToUpper() -eq "$UserName" } | Select-Object ComputerName | Sort-Object -Property ComputerName | Get-Unique -AsString
If ( $userSessions ) {
Write-Host "$($UserName) is logged onto the follow systems:"
ForEach ( $session in $userSessions ) {
$HostName = ([System.Net.Dns]::GetHostbyAddress($session.ComputerName)).HostName
Write-Host "`t$HostName"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment