我正在尝试使用WMI为本地/远程计算机创建一个win服务,但是Win32_Service.Create
返回给我21 error codes并且我没有任何想法输入参数有什么问题:
$connection = new-object System.Management.ConnectionOptions
$connection.EnablePrivileges = $true
$connection.Impersonation = "Impersonate"
# at the moment I test creation on local machine
$scope = new-object System.Management.ManagementScope("\\.\root\cimv2", $connection)
$scope.Connect()
$managment = new-object System.Management.ManagementClass(
$scope,
(new-object System.Management.ManagementPath("Win32_Service")),
(new-object System.Management.ObjectGetOptions))
$params = $managment.GetMethodParameters("Create")
$params["Name"] = "Test"
$params["PathName"] = "C:\Users\User\test.exe"
$params["ServiceType"] = 0x10
$params["ErrorControl"] = 1
$params["StartMode"] = "Automatic"
# other params will be used by default
# result is 21
$result = $managment.InvokeMethod("Create", $params)
# resultObj.ReturnValue is 21
$resultObj = $managment.InvokeMethod("Create", $params, $null)
我无法使用New-Service
,因为我希望将安装服务提供给本地计算机和远程服务器。我在远程计算机上没有管理员权限(我在WMI组中),所以我无法使用sc
,但sc
在本地计算机上运行正常
有没有人知道输入参数有什么问题?
答案 0 :(得分:0)
令我感到惊讶的是DisplayName
参数不能empty
必须设置它。
所以参数工作正常:
$params["Name"] = "Test"
$params["DisplayName"] = "Diplay name"
$params["PathName"] = "C:\Users\User\test.exe"
$params["ServiceType"] = 0x10
$params["ErrorControl"] = 1
$params["StartMode"] = "Automatic"
# other params will be used by default