The following script works in PS5_ISE & CMD.
#Just incase Event has been previously registered
Try {
Unregister-Event -SourceIdentifier 'disk' -Force -ErrorAction Stop
}
Catch {}
$REArgs = @{Query = "Select * from __InstanceCreationEvent within 1 where targetinstance isa 'win32_logicaldisk'"
SourceIdentifier = "disk"
Timeout = 1000
}
Register-WmiEvent @REArgs
However when I attempt to run it in PS7.2.6 I get this:
PSv7>..\test\set-wmidisklistener.ps1
Register-WmiEvent: G:\BEKDocs\Scripts\test\Set-WMIDiskListener.ps1:11
Line |
11 | Register-WmiEvent @REArgs
| ~~~~~~~~~~~~~~~~~
| The term 'Register-WmiEvent' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of
| the name, or if a path was included, verify that the path is correct and try again.
Yet...
PSv7>get-command Register*
CommandType Name Version Source
----------- ---- ------- ------
Function Register-ClusteredScheduledTask 1.0.0.0 ScheduledTasks
Function Register-DnsClient 1.0.0.0 DnsClient
Function Register-IscsiSession 1.0.0.0 iSCSI
Function Register-PSRepository 2.2.5 PowerShellGet
Function Register-PSRepository 2.2.5 PowerShellGet
Function Register-PSRepository 1.0.0.1 PowerShellGet
Function Register-ScheduledTask 1.0.0.0 ScheduledTasks
Function Register-StorageSubsystem 2.0.0.0 Storage
Cmdlet Register-ArgumentCompleter 7.2.6.500 Microsoft.PowerShell.Core
Cmdlet Register-CimIndicationEvent 7.0.0.0 CimCmdlets
Cmdlet Register-EngineEvent 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Register-ObjectEvent 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Register-PackageSource 1.4.7 PackageManagement
Cmdlet Register-PSSessionConfiguration 7.2.6.500 Microsoft.PowerShell.Core
Cmdlet Register-ScheduledJob 1.1.0.0 PSScheduledJob
Cmdlet Register-WmiEvent 3.1.0.0 Microsoft.PowerShell.Management
ExternalScript RegisterManifest.ps1 C:\Program Files\PowerShell\7\RegisterManifest.ps1
Application Register-CimProvider.exe 10.0.1904… C:\Windows\system32\Register-CimProvider.exe
CodePudding user response:
The standard advice applies:
The CIM cmdlets (e.g., Get-CimInstance
) superseded the WMI cmdlets (e.g., Get-WmiObject
) in Windows PowerShell v3 (released in September 2012).
Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) 7 , where all future effort will go, doesn't even have them anymore. Note that WMI still underlies the CIM cmdlets, however. For more information, see this answer.
The CIM cmdlets are contained in the CimCmdlets
module. To list them all, run Get-Command -Module CimCmdlets
While WMI still underlies the CIM cmdlets, there are differences between the Windows PowerShell-only WMI cmdlets and their CIM successors that go beyond different names, notably the need to use Invoke-CimMethod
to invoke methods.
Judging simply by its name, Register-CimIndicationEvent
seems to be the successor to Register-WmiEvent
.
As for why you saw Register-WmiEvent
in the output from get-command Register*
in PowerShell (Core):
By default, you would not see this.
You would only see it if you had explicitly chosen to load the Windows PowerShell version of the
Microsoft.PowerShell.Management
module - which contains the WMI cmdlets - using the Windows PowerShell compatibility feature
(Import-Module -UseWindowsPowerShell Microsoft.PowerShell.Management
)- However, this is ill-advised, given the inherent limitations of this feature (also, said module contains many of PowerShell's core cmdlets, but no proxies are created for those that also exist natively in PowerShell (Core)) and the native availability of the CIM cmdlets.