#requires -version 3.0
<#
****************************************************************
* 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 Get-MyCimInstance {
<#
.Synopsis
Create on-the-fly CIMSessions to retrieve WMI data
.Description
The Get-CimInstance cmdlet in PowerShell 3 can be used to retrieve WMI information
from a remote computer using the WSMAN protocol instead of the legacy WMI service
that uses DCOM and RPC. However, the remote computers must be running PowerShell
3 and the latest version of the WSMAN protocol. When querying a remote computer,
Get-CIMInstance setups a temporary CIMSession. However, if the remote computer is
running PowerShell 2.0 this will fail. You have to manually create a CIMSession
with a CIMSessionOption to use the DCOM protocol.
This command does that for you automatically. It is designed to use computernames.
The computer is tested and if it is running PowerShell 2.0 then a temporary session
is created using DCOM. Otherwise a standard CIMSession is created. The remaining
CIM parameters are then passed to Get-CIMInstance.
Get-MyCimInstance is essentially a wrapper around Get-CimInstance to make it easier
to query data from a mix of computers.
.Example
PS C:\> get-content computers.txt | get-myciminstance -class win32_logicaldisk -filter "drivetype=3"
.Notes
Last Updated: April 11, 2013
Version : 1.0
Author : Jeffery Hicks (@JeffHicks)
Read PowerShell:
Learn Windows PowerShell 3 in a Month of Lunches
Learn PowerShell Toolmaking in a Month of Lunches
PowerShell in Depth: An Administrator's Guide
.Link
http://jdhitsolutions.com/blog/2013/04/get-ciminstance-from-powershell-2-0
.Link
Get-CimInstance
New-CimSession
New-CimsessionOption
.Inputs
string
.Outputs
CIMInstance
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter a class name",
ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[string]$Class,
[Parameter(Position=1,ValueFromPipelineByPropertyName,ValueFromPipeline)]
[ValidateNotNullorEmpty()]
[string[]]$Computername=$env:computername,
[Parameter(ValueFromPipelineByPropertyName)]
[string]$Filter,
[Parameter(ValueFromPipelineByPropertyName)]
[string[]]$Property,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[string]$Namespace="root\cimv2",
[switch]$KeyOnly,
[uint32]$OperationTimeoutSec,
[switch]$Shallow,
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
)
Begin {
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
Write-verbose -Message ($PSBoundParameters | out-string)
Function Test-IsWsman3 {
[cmdletbinding()]
Param(
[Parameter(Position=0,ValueFromPipeline)]
[string]$Computername=$env:computername
)
Begin {
#a regular expression pattern to match the ending
[regex]$rx="\d\.\d$"
}
Process {
Try {
$result = Test-WSMan -ComputerName $Computername -ErrorAction Stop
}
Catch {
#Write the error to the pipeline if the computer is offline
#or there is some other issue
write-Error $_.exception.message
}
if ($result) {
$m = $rx.match($result.productversion).value
if ($m -eq '3.0') {
$True
}
else {
$False
}
}
} #process
End {
#not used
}
} #end Test-IsWSMan
} #begin
Process {
foreach ($computer in $computername) {
Write-Verbose "Processing $computer"
#hashtable of parameters for New-CimSession
$sessParam=@{Computername=$computer;ErrorAction='Stop'}
if ($credential) {
Write-Verbose "Adding alternate credential for CIMSession"
$sessParam.Add("Credential",$Credential)
}
Try {
#test if computer is running WSMAN 2
$isWSMAN3 = Test-IsWsman3 -Computername $computer -ErrorAction Stop
if (-NOT $isWSMAN3) {
#create a CIM session using the DCOM protocol
Write-Verbose "Creating a DCOM option"
$opt = New-CimSessionOption -Protocol Dcom
$sessparam.Add("SessionOption",$opt)
}
Else {
Write-Verbose "Confirmed WSMAN 3.0"
}
Try {
$session = New-CimSession @sessParam
}
Catch {
Write-Warning "Failed to create a CIM session to $computer"
Write-Warning $_.Exception.Message
}
#create the parameters to pass to Get-CIMInstance
$paramHash=@{
CimSession= $session
Class = $class
}
$cimParams = "Filter","KeyOnly","Shallow","OperationTimeOutSec","Namespace"
foreach ($param in $cimParams) {
if ($PSBoundParameters.ContainsKey($param)) {
Write-Verbose "Adding $param"
$paramhash.Add($param,$PSBoundParameters.Item($param))
} #if
} #foreach param
#execute the query
Write-Verbose "Querying $class"
Get-CimInstance @paramhash
#remove the temporary cimsession
Remove-CimSession $session
} #Try
Catch {
Write-Warning "Unable to verify WSMAN on $Computer"
}
} #foreach computer
} #process
End {
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end
} #end Get-MyCimInstance