notepad
$Notepad = Get-CimInstance -ClassName Win32_Process -Filter "Name = 'notepad.exe'"
Invoke-CimMethod -InputObject $Notepad -MethodName Terminate
Run Code Online (Sandbox Code Playgroud)
这些语句工作正常,但是当我Get-CimInstance -ClassName Win32_Process -Filter "Name = 'notepad.exe'" | get-member -force用来查找该Terminate方法时,它似乎不存在。
该调用-CimMethod cmdlet的调用通用信息模型(CIM)类或方法CIM实例...
该GET-CimInstance cmdlet获取公共信息模型(CIM)实例一类从CIM服务器...
不要混淆Powershell和CimClass 对象的方法:
[Microsoft.PowerShell.Commands.MemberDefinition][Microsoft.Management.Infrastructure.CimClass]你正在寻找类似的东西
PS D:\PShell> (Get-CimClass -ClassName Win32_Process).CimClassMethods |
Format-Table -AutoSize -Wrap
Name ReturnType Parameters
---- ---------- ----------
Create UInt32 {CommandLine, CurrentDirectory,
ProcessStartupInformation, ProcessId}
Terminate UInt32 {Reason}
GetOwner UInt32 {Domain, User}
GetOwnerSid UInt32 {Sid}
SetPriority UInt32 {Priority}
AttachDebugger UInt32 {}
GetAvailableVirtualSize UInt32 {AvailableVirtualSize}
Run Code Online (Sandbox Code Playgroud)
JosefZ 的回答很有帮助,并提供了有效的解决方案;让我用不同的方式来解释这个问题:
tl;博士:
要通过 返回的对象发现 CIM 实例支持的方法Get-CimInstance,请访问其.CimClass.CimClassMethods属性:
PS> (Get-CimInstance Win32_Process -Filter "Name = 'notepad.exe'").CimClass.CimClassMethods
Name ReturnType Parameters Qualifiers
---- ---------- ---------- ----------
Create UInt32 {CommandLine, CurrentDirectory, ProcessStartupInformation, ProcessId} {Constructor, Implemented, MappingStrings, Privileges...}
Terminate UInt32 {Reason} {Destructor, Implemented, MappingStrings, Privileges...}
GetOwner UInt32 {Domain, User} {Implemented, MappingStrings, ValueMap}
GetOwnerSid UInt32 {Sid} {Implemented, MappingStrings, ValueMap}
SetPriority UInt32 {Priority} {Implemented, MappingStrings, ValueMap}
AttachDebugger UInt32 {} {Implemented, ValueMap}
GetAvailableVirtualSize UInt32 {AvailableVirtualSize}
Run Code Online (Sandbox Code Playgroud)
要将上述限制为实例方法:
(Get-CimInstance Win32_Process -Filter "Name = 'notepad.exe'").CimClass.CimClassMethods |
Where-Object { -not $_.Qualifiers['Static'] }
Run Code Online (Sandbox Code Playgroud)
或者,将 CIM类的名称传递给Get-CimClass -ClassName,如 JosefZ 的回答。
期望从 cmdlet 返回的对象在通过管道传输时显示其所有成员是合理的Get-Member- 这就是它在 PowerShell 中的通常工作方式。
但是,处理CIM实体(类和实例)是一种特殊情况:
一方面PowerShell及其基于 .NET 的类型(类)系统,另一方面是平台中立的 CIM 标准,建模(通常)远程计算资源,是不同的世界,有自己的实体,不完美地映射到每个其他。
CIM 实例部分映射到类型[Microsoft.Management.Infrastructure.CimInstance]为 的PowerShell 对象,由 返回Get-CimInstance:
这些 PowerShell 对象实际上是底层 CIM 实例的部分只读快照。
State属性Win32_Service),但在实践中似乎忽略了分配给这样的属性。您不能直接修改通过当地CIM实例[Microsoft.Management.Infrastructure.CimInstance]作品-专用的cmdlet需要修改CIM实例:Invoke-CimMethod要调用方法,并Set-CimInstance以一组属性。
同样,您无法通过检查[Microsoft.Management.Infrastructure.CimInstance]带有 的对象来直接发现 CIM 类/实例的所有成员Get-Member,因为它们只是其底层 CIM 实例的部分表示。
如顶部所述,您有两种选择:
[Microsoft.Management.Infrastructure.CimInstance]对象的CimClass属性。Get-CimClass -ClassName <CimClassName>。[Microsoft.Management.Infrastructure.CimClass]实例,该实例在 CIM 类的属性和方法等方面对其进行描述,但请注意,此内省信息基于CIM规则。| 归档时间: |
|
| 查看次数: |
847 次 |
| 最近记录: |