Skip to content

Instantly share code, notes, and snippets.

@macostag
Created October 26, 2018 01:59
Show Gist options
  • Save macostag/9b835f8ad8d48fa4f7f8dcb61a23c136 to your computer and use it in GitHub Desktop.
Save macostag/9b835f8ad8d48fa4f7f8dcb61a23c136 to your computer and use it in GitHub Desktop.
Powershell module example.
Function Get-PSHComputerSystemData{
[cmdletBinding()]
param(
[Parameter( Mandatory=$True,
ValueFromPipeline=$True)]
[string []] $ComputerName,
[string] $ErrorLog = 'C:\Users\Public\errLab.txt',
[switch] $LogError
)
BEGIN{
Write-Verbose "Starting Get-PSHComputerSystemData"
}#BEGIN
PROCESS{
foreach ($computer in $computerName){
Write-Verbose "Querying $computer."
try{
$everything_ok = $True
Write-Verbose "Querying WMI Win32_operatingsystem."
$os = Get-WmiObject -Class Win32_operatingsystem -ComputerName $computer -ErrorAction Stop
}catch{
$everything_ok = $False
Write-Warning "$computer failed - $_.Exception.Message"
if($LogError){
#create an error message
$msg = "$computer : $_.Exception.Message"
$msg | Out-File $Errorlog -Append
Write-Warning "Logged to $Errorlog."
}
}#Try/catch
If($everything_ok){
Write-Verbose "Querying WMI Win32_ComputerSystem."
$comp = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer
#decode the admin password status
$adminPasswordStatus = "N/A"
Switch ($comp.AdminPasswordStatus) {
0 {
$adminPasswordStatus = "Disabled";Break
}
1 {
$adminPasswordStatus = "Enabled";Break
}
2 {
$adminPasswordStatus = "Not Implemented";Break
}
3 {
$adminPasswordStatus = "Unknown ";Break
}
}#Switch
#Define a hashtable to be used for property names and values
$props = @{
'computerName' = $computer;
'domain' = $comp.Domain;
'Manufacturer' = $comp.Manufacturer;
'Model' = $comp.Model;
'Version' = $os.Version;
'Service Pack' = $os.ServicePackMajorVersion;
'AdminPasswordStatus' = $adminPasswordStatus
}
#create a custom object from the hash table
$obj = New-Object -TypeName PSObject -Property $props
#add a type name to the custom object
$obj.PSObject.TypeNames.Insert(0,'PSH.ComputerSystemData')
Write-Output $obj
}#If
}#foreach
}#PROCESS
END{
Write-Verbose "Ending Get-PSHComputerSystemData"
}#END
}
Function Get-PSHComputerDiskData {
[cmdletBinding()]
param(
[Parameter( Mandatory=$True,
ValueFromPipeline=$True)]
[string []] $computerName,
[string] $ErrorLog = 'C:\Users\Public\errLab.txt',
[switch] $LogError
)
BEGIN{
Write-Verbose "Starting Get-PSHComputerDiskData."
}
PROCESS{
foreach ($computer in $computerName){
Write-Verbose "Querying $computer."
Try{
Write-Verbose "Querying WMI Win32_Volume."
$disks = Get-WmiObject -Class Win32_Volume -ComputerName $computer -Filter "drivetype=3" -ErrorAction Stop
foreach ($disk in $disks){
Write-Verbose "Querying $disk."
$computerName = $disk.SystemName
$DeviceID = $disk.DeviceID
$VolumeName = $disk.Caption
$freeSpace = $disk.FreeSpace/1GB
$Size= $disk.capacity/1GB
$props = @{
'computerName' = $computer;
'DeviceID' = $DeviceID;
'Caption' = $VolumeName;
'freeSpace(GB)' = "{0:N2}" -f $freeSpace;
'Size(GB)' = "{0:N2}" -f $Size
}
$obj = New-Object -TypeName PSObject -Property $props
$obj.PSObject.TypeNames.Insert(0,'PSH.ComputerDiskData')
Write-Output $obj
Remove-Variable -Name $obj
}
}catch{
$msg = "$computer failed - $_.Exception.Message"
Write-Warning $msg
if($LogError){
$msg | Out-File $Errorlog -Append
Write-Warning "Logged to $Errorlog."
}
}
}
}
END{
Write-Verbose "Ending Get-PSHComputerDiskData."
}
}
Function Get-PSHComputerServiceData {
[cmdletBinding()]
param(
[Parameter( Mandatory=$True,
ValueFromPipeline=$True)]
[string []] $computerName,
[string] $ErrorLog = 'C:\Users\Public\errLab.txt',
[switch] $LogError
)
BEGIN{
Write-Verbose "Starting Get-PSHComputerServiceData."
}
PROCESS{
foreach ($computer in $computerName){
Write-Verbose "Querying $computer."
Try{
$everything_ok = $True
Write-Verbose "Querying Win32_Service."
$services = Get-WmiObject -Class Win32_Service -filter "State='Running'" -computername $computer -ErrorAction Stop
} catch {
$everything_ok = $False
$msg = "$computer failed - $_.Exception.Message"
Write-Warning $msg
if($LogError){
$msg | Out-File $Errorlog -Append
Write-Warning "Logged to $Errorlog"
}
}
If($everything_ok){
foreach ($service in $services){
Write-Verbose "Querying $service."
Write-Verbose "Querying Win32_Process."
$id = $service.ProcessId
$process = Get-WmiObject -Class Win32_Process -Filter "ProcessId=$id" -computername $computer
$hash = @{
'PSComputerName' = $process.PSComputerName;
'ThreadCount' = $process.ThreadCount;
'ProcessName' = $process.ProcessName;
'Name' = $process.Name;
'Description' = $process.Description;
'VM' = $process.VM;
}
$obj = New-Object -TypeName PSObject -Property $hash
$obj.PSObject.TypeNames.Insert(0,'PSH.ComputerServiceData')
Write-Output $obj
}
}
}
}
END{
Write-Verbose "Ending Get-PSHComputerServiceData."
}
}
Function Set-PSHComputerState{
[cmdletBinding(SupportsShouldProcess=$True,
ConfirmImpact='High')]
param(
[Parameter( Mandatory=$True,
ValueFromPipeline=$True)]
[string []] $ComputerName,
[ValidateSet('LogOff','Restart','ShutDown','PowerOff')]
[string] $Action,
[switch] $Force
)
BEGIN{}
PROCESS{
ForEach ($computer in $ComputerName){
$os = Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem
Switch ($Action) {
'LogOff' {
$actionValue = 0;Break
}
'Restart'{
$actionValue = 1;Break
}
'ShutDown' {
$actionValue = 2;Break
}
'PowerOff' {
$actionValue = 3;Break
}
}#Switch
If ($Force){
$actionForce = 4
}
If ($PSCmdlet.ShouldProcess("$os on $computer")){
$os.Win32Shutdown($actionValue + $actionForce) | Out-Null
}
}
}
END{}
}
#Define some aliases for the functions
New-Alias -Name gcsd -Value Get-PSHComputerSystemData
New-Alias -Name gcdd -Value Get-PSHComputerDiskData
New-Alias -Name gcpd -Value Get-PSHComputerServiceData
#Export the functions and aliases
Export-ModuleMember -Function * -Alias *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment