我将从我刚刚为 SO 问题所做的答案中复制粘贴一些代码。我遇到的问题是为什么Invoke-WmiMethod
在 Windows 8(PS3.0)上工作时在我的 Windows 7(PS2.0)机器上返回错误。这是代码:
#Username/Group to give permissions to
$trustee = ([wmiclass]'Win32_trustee').psbase.CreateInstance()
$trustee.Domain = "domainname"
$trustee.Name = "username"
#Accessmask values
$fullcontrol = 2032127
$change = 1245631
$read = 1179785
#Create access-list
$ace = ([wmiclass]'Win32_ACE').psbase.CreateInstance()
$ace.AccessMask = $fullcontrol
$ace.AceFlags = 3
$ace.AceType = 0
$ace.Trustee = $trustee
#Securitydescriptor containting access
$sd = ([wmiclass]'Win32_SecurityDescriptor').psbase.CreateInstance()
$sd.ControlFlags = 4
$sd.DACL = $ace
$sd.group = $trustee
$sd.owner = $trustee
#Arguments - Arguments: access, description, max. allowed, name, password, path, type(0 = drive/folder share)
Invoke-WmiMethod -Class Win32_Share -Name Create -ArgumentList @($sd, "TestDescription", 100, "TestName$", "", "C:\Test", 0)
Invoke-WmiMethod
在 Windows 7 机器上执行时返回错误,而在 Windows 8 上工作正常。如果我$sd
用 $null 替换(访问参数)它在 win7 上运行良好。错误是:
Invoke-WmiMethod <<<< -Class win32_share -name Create -ArgumentList @($sd, "TestDescription", 100, "TestName$", "", "C:\Test", 0)
+ CategoryInfo : InvalidOperation: (:) [Invoke-WmiMethod], ManagementException + FullyQualifiedErrorId : InvokeWMIManagementException,Microsoft.PowerShell.Commands.InvokeWmiMethod'
这是使用的参数列表Invoke-WmiMethod
:
PS C:\Windows> ([wmiclass]'win32_share').GetMethodParameters('create')
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 7
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
Access :
Description :
MaximumAllowed :
Name :
Password :
Path :
Type :
PSComputerName :
谁能帮我理解为什么Invoke-WmiMethod
在 w8 上工作时不喜欢我在 w7 上的访问参数?如果我运行它,它接受$sd
两者:([wmiclass]'Win32_share').Create(.....)
使用它的定义:
PS C:\Windows> ([wmiclass]'win32_share').Create
OverloadDefinitions
-------------------
System.Management.ManagementBaseObject Create(System.String Path, System.String Name, System.UInt32 Type, System.UInt32 MaximumAllowed, System.String Description, System
.String Password, System.Management.ManagementObject#Win32_SecurityDescriptor Access)