这个查询gwmi -Class Win32_PerfFormattedData_NETFramework_NETCLRMemory给出了更less的结果,如果我在32位PowerShell控制台中运行它比64位之一。 它看起来像后台进程,如服务,只是显示在64位。 这并不是PowerShell所独有的,我在C#和F#中都得到了不一致的结果。 我也遇到了一些我使用的监视工具的问题。
这里发生了什么? 如何使32位模式正常工作?
从StackOverflow的这个答案中采用了JPBlanc的解决scheme
# Setup the context information $mContext = New-Object System.Management.ManagementNamedValueCollection $mContext.Add( "__ProviderArchitecture", 64) $mContext.Add( "__RequiredArchitecture", $true) # Setup the Authrntification object $ConOptions = New-Object System.Management.ConnectionOptions #$ConOptions.Username = "computername\administrateur" # Should be used for remote access #$ConOptions.Password = "toto" $ConOptions.EnablePrivileges = $true $ConOptions.Impersonation = "Impersonate" $ConOptions.Authentication = "Default" $ConOptions.Context = $mContext # Setup the management scope (change with the computer name for remote access) $mScope = New-Object System.Management.ManagementScope( ` "\\localhost\root\cimV2", $ConOptions) $mScope.Connect() # Query $queryString = "SELECT * From Win32_PerfFormattedData_NETFramework_NETCLRMemory" $oQuery = New-Object System.Management.ObjectQuery ($queryString) $oSearcher = New-Object System.Management.ManagementObjectSearcher ($mScope, $oQuery) $oResult = $oSearcher.Get(); $oResult.Name # only for simple check that current code snippet gives # the same results from both 32 and 64 -bit version of PowerShell
在64位平台上请求WMI数据
默认情况下,应用程序或脚本在存在两个版本的提供程序时从相应的提供程序接收数据。 32位提供程序将数据返回到32位应用程序(包括所有脚本),并且64位提供程序将数据返回到64位已编译的应用程序。 但是,应用程序或脚本可以通过在方法调用上通过标志来通知WMI,从非默认提供程序(如果存在)请求数据。
上下文标志
__ProviderArchitecture和__RequiredArchitecturestring标志具有一组由WMI处理的值,但未在SDK头或types库文件中定义。 这些值被放置在一个上下文参数中,用来向WMI发出信号,告知它应该向非默认提供者请求数据。
以下列出了标志及其可能的值。
__ProviderArchitecture整数值,32或64,指定32位或64位版本。__RequiredArchitecture布尔值强制加载指定的提供程序版本。 如果版本不可用,则WMI返回错误0x80041013,对于Visual Basic,WBEM_E_PROVIDER_LOAD_FAILURE对于C ++,返回WBEM_E_PROVIDER_LOAD_FAILURE。 未指定时,此标志的默认值为FALSE。在具有并行版本的提供程序的64位系统上,32位应用程序或脚本自动从32位提供程序接收数据,除非提供了这些标志,并指示64位提供程序数据应该被退回。