【问题标题】:How to change the windows account to localsystem account using Powershell command Set-Service SwitchAccount如何使用 Powershell 命令 Set-Service SwitchAccount 将 Windows 帐户更改为本地系统帐户
【发布时间】:2021-09-17 22:57:35
【问题描述】:

我想仅使用 set-service 命令并使用 SwitchServiceAccount 参数更改服务的 Windows 服务帐户,但出现错误“找不到与“SwitchServiceAccount”匹配的参数。什么是正确的 switchservice命令?我使用的是 PowerShell 5.1 版本。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    sc.exe config "ServiceName" obj="Localsystem"

    【讨论】:

    • 请补充一点说明
    【解决方案2】:

    根据docsSet-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”}
    

    【讨论】:

    • 谢谢马尔泽。我想使用 PowerShell 将服务登录从“NT SERVICE\DIAHostService”更改为本地系统本地登录,而不提供密码,因为它在 UI/对话框中工作。 PFA 截图。我可以在这里将用户名和密码设置为 null - $service.Change($null,$null,$null,$null,$null,$null,"user","password") 吗?
    • 谢谢马尔泽。我尝试了第二个选项,我将输出作为返回值 2,并且该帐户没有更改为本地系统。你能在这里帮忙吗? - 获取 CimInstance win32_service -filter "name='DIAHostService'" | Invoke-CimMethod -Name Change -Arguments @{StartName="LocalSystem"} ReturnValue PSComputerName ----------- -------------- 2
    • @YogeshKulkarni 这对我来说都是理论上的。我从来没有这样做过,我的回答完全基于互联网研究。我恳请您自己做一些研究。
    • 谢谢马尔泽。将尝试选项并相应地更新线程。
    • sc.exe config "ServiceName" obj="Localsystem" 为我工作。
    【解决方案3】:

    我使用这种方法将大约 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'"
    }
    
    

    【讨论】:

      【解决方案4】:

      @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=""}
      

      【讨论】:

        猜你喜欢
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-16
        • 2015-01-12
        • 2021-07-17
        • 2013-02-16
        相关资源
        最近更新 更多