Powershell,WMI方法
将WMI与PowerShell一起使用时,我发现了一些不明白的地方:Powershell,WMI方法,powershell,syntax,wmi,Powershell,Syntax,Wmi,将WMI与PowerShell一起使用时,我发现了一些不明白的地方: # Syntax 1 gwmi -Class Win32_Share | gm -MemberType Method Output: Delete, GetAccessMask, SetShareInfo # Syntax 2 $a = New-Object "System.Management.ManagementClass" "Win32_Share" : $a | gm -MemberType Method Outpu
# Syntax 1
gwmi -Class Win32_Share | gm -MemberType Method
Output: Delete, GetAccessMask, SetShareInfo
# Syntax 2
$a = New-Object "System.Management.ManagementClass" "Win32_Share" :
$a | gm -MemberType Method
Output: Create.....
那么:为什么我不使用语法“1”获取“Create”方法呢?因为它们返回两种不同类型的对象
(gwmi -Class Win32_Share).GetType()
返回System.Array
实例,同时
(New-Object "System.Management.ManagementClass" "Win32_Share").GetType()
返回一个System.Management.ManagementObject
实例
请注意,在已经实例化的对象上调用Create是没有意义的,或者换句话说:为什么您认为您需要它
编辑
你的评论实际上让我重新思考(最后),你的难题是你应该使用-query
而不是-class
。我还没有弄清楚两种调用方法之间的实际区别是什么,但我假设它们是相同的类/实例区别
Get-WmiObject -query "SELECT * FROM meta_class WHERE __class = 'Win32_Share'"
你好,列文,谢谢你的帮助。在这种情况下(可能有点太学术化了……),方法“create”是有意义的,因为它不创建对象本身,而是创建一个新的共享。@Purclot-我已经更新了答案以处理您的评论。fwiw-不鼓励使用WMI commandlets而支持CIM commandlets。