【发布时间】:2021-09-17 22:57:35
【问题描述】:
我想仅使用 set-service 命令并使用 SwitchServiceAccount 参数更改服务的 Windows 服务帐户,但出现错误“找不到与“SwitchServiceAccount”匹配的参数。什么是正确的 switchservice命令?我使用的是 PowerShell 5.1 版本。
【问题讨论】:
标签: powershell
我想仅使用 set-service 命令并使用 SwitchServiceAccount 参数更改服务的 Windows 服务帐户,但出现错误“找不到与“SwitchServiceAccount”匹配的参数。什么是正确的 switchservice命令?我使用的是 PowerShell 5.1 版本。
【问题讨论】:
标签: powershell
sc.exe config "ServiceName" obj="Localsystem"
【讨论】:
根据docs,Set-Service命令没有这样的参数。 (您也可以使用Get-Help Set-Service -Full 来查看可用的参数以及解释和示例。)
在 PowerShell Core (v6+) 中,您可以使用 -Credentials 参数设置服务凭据:
Set-Service -Name Schedule -Credential $credential
在 PowerShell 5.1 中尝试来自this SO 帖子的解决方案:
$service = Get-WmiObject win32_service -Filter "name='YourService'"
$service.Change($null,$null,$null,$null,$null,$null,"user","password")
或this:
Get-CimInstance win32_service -filter “name=’some_service_name'” | Invoke-CimMethod -Name Change -Arguments @{StartName=”LocalSystem”}
【讨论】:
我使用这种方法将大约 40 台机器的服务登录更改为本地系统。您的服务器列表文件可以是 .txt、.csv 或其他一些 powershell 获取函数。
$svcobj.change 依赖于下面这个序列中的这些值。此处的每个值对应于服务配置的特定部分。有 11 个值,$svcobj.change 需要全部配置才能正常工作。确保将它们按正确的顺序放置,否则可能会损坏服务配置。要更改为 LocalSystem,应将 StartName 更改为“LocalSystem”,所有其他值保持为 $null。
<# WMI Win32_Service values
uint32 Change(
[in] string DisplayName,
[in] string PathName,
[in] uint32 ServiceType,
[in] uint32 ErrorControl,
[in] string StartMode,
[in] boolean DesktopInteract,
[in] string StartName,
[in] string StartPassword,
[in] string LoadOrderGroup,
[in] string LoadOrderGroupDependencies[],
[in] string ServiceDependencies[]
);
#>
#Configure Runas LocalSystem
$svcname = "WindowsService"
foreach ($source in get-content "Some_Path_to_Server_List") {
$source
$svcobj = Get-WmiObject -ComputerName $source -Class Win32_Service -Filter "Name='$svcname'"
$svcobj.StopService | out-null
$svcobj.Change($null,$null,$null,$null,$null,$null,"LocalSystem",$null,$null,$null,$null) | out-null
$svcobj.StartService | out-null
Get-WmiObject -ComputerName $source -Class Win32_Service -Filter "Name='$svcname'"
}
或者您可以使用下面的代码以具有用户名和密码的特定用户身份运行。 $svccred 提示输入用户名和密码并将其加密存储在内存中。
#Configure Runas AD User
$svccred = Get-Credential -Message "Enter Service Account Credentials"
$svcname = "WindowsService"
foreach ($source in get-content "Some_Path_to_Server_List") {
$source
$svcobj = Get-WmiObject -ComputerName $source -Class Win32_Service -Filter "Name='$svcname'"
$svcobj.StopService | out-null
$svcobj.Change($null,$null,$null,$null,$null,$null,$svccred.Username,$svccred.Password,$null,$null,$null) | out-null
$svcobj.StartService | out-null
Get-WmiObject -ComputerName $source -Class Win32_Service -Filter "Name='$svcname'"
}
【讨论】:
@Yogesh
这行不通:
Get-CimInstance win32_service -filter "name='DIAHostService'" | Invoke-CimMethod `
-Name Change -Arguments @{StartName="LocalSystem"}
因为Change方法需要设置启动密码,即使没有启动密码(如果定义了StartName),所以这个调用应该成功:
Get-CimInstance win32_service -filter "name='DIAHostService'" | Invoke-CimMethod `
-Name Change -Arguments @{StartName="LocalSystem";StartPassword=""}
【讨论】: