forked from jdhitsolutions/ISEScriptingGeek
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCIMScriptMaker.ps1
More file actions
222 lines (180 loc) · 6.45 KB
/
CIMScriptMaker.ps1
File metadata and controls
222 lines (180 loc) · 6.45 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#requires -version 3.0
<#
Version: 3.0
Author : Jeff Hicks
@jeffhicks
http://jdhitsolutions.com/blog
"Those who forget to script are doomed to repeat their work."
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
#>
Function New-CIMCommand {
Param([string]$computername = $env:COMPUTERNAME)
Function Get-Namespace {
#this function will recursively enumerate namespaces
Param(
[string]$Namespace="Root",
[Microsoft.Management.Infrastructure.CimSession]$CimSession
)
$nspaces = $cimsession | Get-CimInstance -Namespace $Namespace -ClassName __Namespace
foreach ($nspace in $nspaces) {
$child = Join-Path -Path $Namespace -ChildPath $nspace.Name
$child
Get-Namespace $child $CimSession
}
}
#create a CIMSession
$cimsess = New-CimSession -ComputerName $computername
#browse namespaces
Write-Host "Enumerating namspaces on $computername....please wait..." -ForegroundColor Cyan
$ns = Get-Namespace -CimSession $cimsess | Sort |
Out-GridView -Title "$($cimsess.Computername): Select a namespace" -OutputMode Single
if ($ns) {
#get classes filtering out system classes
Write-Host "Enumerating classes...please wait..." -ForegroundColor Cyan
$class = $cimsess | Get-CimClass -Namespace $ns |
Where {$_.cimclassname -notmatch "^__" -AND $_.CimClassProperties.Name -notcontains "Antecedent"} |
Sort CimClassName | Select CimClassName,CimClassProperties |
Out-GridView -Title "$NS : Select a class name" -OutputMode Single
}
if ($class) {
#create a VBScript message box
$wshell = New-Object -ComObject "Wscript.Shell"
$r = $wshell.Popup("Do you want to test this class?",-1,$class.CimClassname,32+4)
if ($r -eq 6) {
#Yes
$test = $cimsess | Get-CimInstance -Namespace $ns -ClassName $class.CimClassName
if ($test) {
$test | Out-GridView -Title "$NS\$($Class.cimClassName)" -Wait
$prompt="Do you want to continue?"
$icon=32+4
}
else {
$prompt="No results were returned. Do you want to continue?"
$icon=16+4
}
$r = $wshell.Popup($prompt,-1,$class.CimClassname,$icon)
if ($r -eq 7) {
Write-Host "Exiting. Please try again later." -ForegroundColor Yellow
#bail out
Return
}
} #if r = 6
#define basic command
$cmd = "Get-CimInstance @cimParam"
#create a filter
$filterProperty = $class.CimClassProperties | Select Name,CimType,Flags |
Out-GridView -Title "Select a property to filter on or cancel to not filter." -OutputMode Single
if ($filterProperty) {
$operator = "=","<",">","<>",">=","<=","like" |
Out-GridView -Title "Select an operator. Default if you cancel is =" -OutputMode Single
#create a VBSCript inputbox
Add-Type -AssemblyName "microsoft.visualbasic" -ErrorAction Stop
$Prompt = "Enter a value for your filter. If using a string, wrap the value in ''. If using Like, use % as the wildcard character."
$title= "-filter ""$($filterproperty.Name) $operator ?"""
$value=[microsoft.visualbasic.interaction]::InputBox($Prompt,$Title)
$filter = "-filter ""$($filterproperty.Name) $operator $value"""
$cmd+=" $filter"
} #if filterproperty
#show properties
Write-Host "Getting class properties" -ForegroundColor Cyan
$properties = $class.CimClassProperties | select Name,CimType,Flags |
Out-Gridview -Title "$($class.CimClassName) : Select one or more properties. Cancel will select *" -PassThru
if ($properties) {
$select = $properties.name -join ","
$cmd+= @"
|
Select-Object -property $select,PSComputername
"@
} #if properties
} #if $class
#define a name for the function using the class name
#remove _ from class name
$cname = $class.CimClassName.Replace("_","")
$cmdName = "Get-$cname"
#the auto-generated PowerShell code
$myScript = @"
#Requires -version 3.0
Function $cmdName {
<#
.Synopsis
Get $($Class.CimClassName) information
.Description
This command uses the CIM cmdlets to query a remote computer for information from the $($Class.CimClassName) class in the $NS namespace.
This command requires PowerShell 3.0 or later.
.Parameter Computername
The name of a computer to query. It should be running PowerShell 3.0 or later.
This parameter also supports aliases of CN and Host.
.Parameter CimSession
A previously created CimSession. Works best when you pipe the CimSession
to this command. See examples.
.Example
PS C:\> $cmdName
Run the command defaulting to the local computername.
.Example
PS C:\> Get-CimSession | $cmdName | Out-Gridview -title $cmdName
Get all CIMSessions and pipe them to this command sending results to Out-Gridview.
.Notes
Version : 1.0
Author : $($env:userdomain)\$($env:username)
Last Updated: $((Get-Date).ToShortDateString())
.Inputs
String or CimSession
.Outputs
CIMObject or custom object
.Link
Get-CimInstance
Get-CimSession
#>
[cmdletbinding(DefaultParameterSetName="Computer")]
Param(
[Parameter(Position=0,ValueFromPipelinebyPropertyName=`$True,
ParameterSetName="Computer")]
[ValidateNotNullorEmpty()]
[Alias("CN","Host")]
[string[]]`$Computername=`$env:Computername,
[Parameter(Position=0,ValueFromPipeline=`$True,
ParameterSetName="Session")]
[string[]]`$CimSession
)
Begin {
Write-Verbose "Starting command `$(`$MyInvocation.Mycommand)"
#create a hashtable of parameters to splat against Get-CimInstance
`$cimParam=@{
Namespace = "$NS"
ClassName = "$($Class.CimClassName) "
ErrorAction = "Stop"
}
} #begin
Process {
if (`$computername) {
`$cimParam.Computername=`$computername
Write-Verbose "Processing `$Computername"
}
else {
#must be a cimsession
`$cimParam.CIMSession=`$CimSession
Write-Verbose "Processing `$(`$CimSession.ComputerName)"
}
Try {
$cmd
} #try
Catch {
Write-Warning "Failed to retrieve information. `$(`$_.Exception.Message)"
} #catch
} #Process
End {
Write-Verbose "Ending command `$(`$MyInvocation.Mycommand)"
} #end
} #end function
"@
$myScript | Out-ISETab
#remove the cimsession
$cimsess | Remove-CimSession
} #end function