-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample6.ps1
99 lines (73 loc) · 3.02 KB
/
sample6.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
Return "Haven't you figured out this is a walkthrough by now?!"
Function Get-OS {
[cmdletbinding()]
Param(
[Parameter(Position = 0,ValueFromPipeline)]
[Alias("cn")]
[ValidateNotNullorEmpty()]
[string[]]$Computername = $env:computername
)
Begin {
#save current verbose color value
$saved = $host.privatedata.VerboseForegroundColor
#set color for begin block
$host.privatedata.VerboseForegroundColor = "green"
Write-Verbose "$(Get-Date -Format "hh:mm:ss:ffff") [BEGIN ] Starting $($MyInvocation.Mycommand)"
#define some execution metadata
$meta = "Execution context = User:$($env:userdomain)\$($env:username) Computer:$($env:computername) PSVersion:$($PSVersionTable.PSVersion)"
Write-Verbose "$(Get-Date -Format "hh:mm:ss:ffff") [BEGIN ] $meta"
#define hashtable of parameters to splat to Get-CimInstance
$paramHash = @{
ClassName = 'Win32_OperatingSystem' ErrorAction = 'Stop' }
#set color for process block
$host.privatedata.VerboseForegroundColor = "yellow"
} #begin
Process {
Foreach ($computer in $Computername) {
Write-Verbose "$(Get-Date -Format "hh:mm:ss:ffff") [PROCESS] Connecting to $Computer"
$paramHash.Computername = $Computer
Try {
Write-Verbose "$(Get-Date -Format "hh:mm:ss:ffff") [PROCESS] Running Get-CimInstance"
$data = Get-CimInstance @paramHash
Write-Verbose "$(Get-Date -Format "hh:mm:ss:ffff") [PROCESS] Creating output"
Switch ($data.producttype) {
1 { $prodType = "Desktop"}
2 { $prodType = "DomainController"}
3 { $prodType = "Server"}
default {
#this should never happen but just in case
$prodType = $data.ProductType
}
} #close Switch
[pscustomobject]@{
Computername = $data.CSName
Name = $data.caption
Version = $data.version
Build = $data.buildnumber
InstallDate = $data.installDate
Age = (Get-Date) - $data.installdate
OSArch = $data.OSArchitecture
Type = $ProdType
}
#INSERTING A RANDOM SLEEP FOR THE SAKE OF DEMONSTRATION
Start-Sleep -Seconds (Get-Random -Maximum 5 -Minimum 1)
} #try
Catch {
Write-Warning "Failed to get OS data for $($Computer.toUpper()). $($_.Exception.Message)"
} #catch
} #Foreach
} #process
End {
#set color for end block
$host.privatedata.VerboseForegroundColor = "cyan"
Write-Verbose "$(Get-Date -Format "hh:mm:ss:ffff") [END ] Ending $($MyInvocation.Mycommand)"
#restore color
$host.privatedata.VerboseForegroundColor = $saved
} #end
} #close Get-OS function
Get-OS
Get-OS -Verbose
$r = "localhost",$env:computername,"foo",$(hostname) | Get-OS -Verbose
$r