# replace server name to some server that you control $server = 'SOMESERVER1'
# Get-CimInstance uses WinRM remoting by default Get-CimInstance-ClassName Win32_BIOS -ComputerName$server
# telling Get-CimInstance to use the old DCOM to contact an old system $options = New-CimSessionOption-Protocol Dcom $session = New-CimSession-SessionOption$options-ComputerName$server Get-CimInstance-ClassName Win32_BIOS -CimSession$session Remove-CimSession-CimSession$session
# we are comparing this WMI class (feel free to adjust) $wmiClass = 'Win32_OperatingSystem'
# get information about the WMI class Win32_OperatingSystem with both cmdlets $a = Get-WmiObject-Class$wmiClass | Select-Object-First1 $b = Get-CimInstance-ClassName$wmiClass | Select-Object-First1
# create a calculated property that returns only the basic type name # and omits the namespace $typeName = @{ Name = 'Type' Expression = { $type = $_.TypeNameOfValue.Split('.')[-1].ToLower() switch ($type) { 'boolean' { 'bool' } default { $type } } } }
# ignore the metadata properties which we already know are different $meta = '__CLASS','__DERIVATION','__DYNASTY','__GENUS','__NAMESPACE','__PATH','__PROPERTY_COUNT','__RELPATH','__SERVER','__SUPERCLASS','CimClass','CimInstanceProperties','CimSystemProperties','ClassPath','Container','Options','Properties','Qualifiers','Scope','Site','SystemProperties'
# return the properties and their data type. Add the origin so we later know # which cmdlet emitted them $aDetail = $a.PSObject.Properties | # exclude the metadata we already know is different Where-Object { $_.Name -notin$meta } | # add the origin command as new property "Origin" Select-Object-Property Name, $typeName, @{N='Origin';E={'Get-WmiObject'}} $bDetail = $b.PSObject.Properties | # exclude the metadata we already know is different Where-Object { $_.Name -notin$meta } | # add the origin command as new property "Origin" Select-Object-Property Name, $typeName, @{N='Origin';E={'Get-CimInstance'}}
# compare differences Compare-Object-ReferenceObject$aDetail-DifferenceObject$bDetail-Property Name, Type-PassThru | Select-Object-Property Name, Origin, Type | Sort-Object-Property Name
# we are comparing this WMI class (feel free to adjust) $wmiClass = 'Win32_OperatingSystem'
# get information about the WMI class Win32_OperatingSystem with both cmdlets $a = Get-WmiObject-Class$wmiClass | Select-Object-First1 $b = Get-CimInstance-ClassName$wmiClass | Select-Object-First1
# dump the property names and add the property "Origin" so you know # which property was returned by which command: $aDetail = $a.PSObject.Properties | Select-Object-Property Name, @{N='Origin';E={'Get-WmiObject'}} $bDetail = $b.PSObject.Properties | Select-Object-Property Name, @{N='Origin';E={'Get-CimInstance'}}
# compare the results: Compare-Object-ReferenceObject$aDetail-DifferenceObject$bDetail-Property Name -PassThru | Sort-Object-Property Origin
functionConvert-WordDocument { param ( # accept path strings or items from Get-ChildItem [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] [string] [Alias('FullName')] $Path )
begin { # we are collecting all paths first [Collections.ArrayList]$collector = @() }
process { # find extension $extension = [System.IO.Path]::GetExtension($Path)
# we only process .doc and .dot files if ($extension-eq'.doc'-or$extension-eq'.dot') { # add to list for later processing $null = $collector.Add($Path)
} } end { # pipeline is done, now we can start converting!
# conversion cannot work for read-only docs If (!$doc.ActiveWindow.View.ReadingLayout) { if ($targetConversion-gt0) { $pathOut = [IO.Path]::ChangeExtension($Path, $targetExtension)
PS> explorer (Get-Module-Name PrintManagement -ListAvailable | Select-Object-ExpandProperty Path | Split-Path)
这个快速的演练解释了为什么 PowerShell 没有固定的命令集,以及为什么给定命令在一个系统上可用并且在另一个系统上不可用。新模块可以由操作系统(例如 Windows 10 附带的模块多于 Windows 7)、已安装的软件(例如 SQLServer)、已激活的角色(例如域控制器)引入,并且模块也可以手动安装。
CommandType Name Version Source ---------------------------- FunctionNew-QRCodeGeolocation1.2QRCodeGenerator FunctionNew-QRCodeTwitter1.2QRCodeGenerator FunctionNew-QRCodeVCard1.2QRCodeGenerator FunctionNew-QRCodeWifiAccess1.2QRCodeGenerator
functionFind-WmiClass { # show all properties, not just 4 $oldLimit = $FormatEnumerationLimit $global:FormatEnumerationLimit = -1 # list all WMI classes... Get-WmiObject-Class * -List | # ...with at least 4 properties Where-Object { $_.Properties.Count -gt4 } | # let the user select one Out-GridView-Title'Select a class that seems interesting'-OutputMode Single | ForEach-Object { # query the selected class $Name = $_.name $props = Get-WmiObject-Class$Name | # take the first instance Select-Object-Property * -First1 | ForEach-Object { # turn the object into a hash table, and exclude empty properties $_ | & { $_.PSObject.Properties | Sort-Object-Property Name | Where-Object { $_.Value -ne$null } | ForEach-Object { $hashtable = [Ordered]@{}} { $hashtable[$_.Name] = $_.Value } { $hashtable } } | # show the properties and let the user select Out-GridView-Title"$name : Select all properties you need (hold CTRL)"-PassThru | ForEach-Object { # return the selected property names $_.Name } } # take all selected properties $prop = $props-join', ' # create the command for it: $a = "Get-CimInstance -Class $Name | Select-Object -Property $prop" # place it into the clipboard $a | Set-Clipboard Write-Warning"Command is also available from the clipboard" $a } # reset format limit $global:FormatEnumerationLimit = $oldLimit }