【问题标题】:PowerShell run Command as different User when Credential Parameter is missing缺少凭据参数时,PowerShell 以不同用户身份运行命令
【发布时间】:2022-01-04 09:20:15
【问题描述】:

我需要运行一个通用的 PowerShell 命令来触发远程计算机上工作流之外的组策略更新“gpupdate”。 工作流在系统用户上下文中运行,它没有客户端上的本地管理员权限来强制执行远程“gpupdate”。 因此,我使用“Import-CliXml”导入 PowerShell 凭据安全字符串,以在客户端本地管理员的用户范围内运行该语句。

但是,我要使用的命令不支持本机凭据参数。我需要为远程客户端使用一个参数。 Invoke-GPUpdate -Computer $client -RandomDelayInMinutes 0

我尝试了许多互联网上的方法,但它对我不起作用:

Start-Process powershell.exe -Credential $credentials -ArgumentList $ProcessCommand -WorkingDirectory $env:windir -NoNewWindow -PassThru

Start-Process powershell.exe -wait -Credential $credentials -ArgumentList "-command &{Start-Process Powershell.exe -argumentlist '$($cmnd)' -verb runas -wait}"

如果我测试从远程客户端上的本地管理员用户启动的 PowerShell 控制台发送远程 gpupdate,它可以工作。

有人有解决这个问题的方法吗? 非常感谢!

【问题讨论】:

    标签: powershell parameters scope credentials group-policy


    【解决方案1】:

    当我使用 PowerShell 连接到远程计算机以在这些计算机上执行命令时,我通常会运行以下命令。我留下了我的代码示例供您用来执行 Invoke-GPUpdate

    #Local Host Computer 
    #$RequestingServer = $env:COMPUTERNAME
    
    #Server List From Text File
    #$ServerList = Get-Content 'C:\temp\servicetest\servers.txt'
    
    #Server List In Script 
    $ServerList = 'Computer1','Computer2','Computer3','Computer4'
    
    #Domain Admin Account 
    [STRING]$DomainAccountName = (whoami)
    [STRING]$DomainAccountName = $DomainAccountName.Split("\")[1]
    [STRING]$DomainAccountPassword = "Password01" #Obviously Change Password    
    $DomainAccountSecurePassword = $DomainAccountPassword | ConvertTo-SecureString -AsPlainText -Force
    $DomainCredentials = New-Object System.Management.Automation.PSCredential -ArgumentList $DomainAccountName, $DomainAccountSecurePassword
                    
    #Local Server Admin Account
    [STRING] $LocalUser = "Administrator" #Obviously Change Account
    [STRING] $LocalPassword = "Password01" #Obviously Change Password
    $LocalSecurePassword = $LocalPassword | ConvertTo-SecureString -AsPlainText -Force
    $LocalCredentials = New-Object System.Management.Automation.PSCredential -ArgumentList $LocalUser, $LocalSecurePassword
    
    #If running on multiple computers / servers etc. - - See Lines 5 and 8
    ForEach($ComputerName in $ServerList) {
    
        #Update Windows Something Locally - See Line 2
        #$DomainSession = New-PSSession -Computername $RequestingServer -Credential $DomainCredentials
        
        #Update Windows Something Remotely - See Lines 5 and 8
        $DomainSession = New-PSSession -Computername $ComputerName -Credential $DomainCredentials
            Invoke-Command -Session $DomainSession -ScriptBlock {   
            
            #Some commands need the computername currently using localhost...
            $GPUpdateServer = $Using:ComputerName
            #$GPUpdateServer = $Using:RequestingServer
            
            # enter code of what you plan to do... 
            Invoke-GPUpdate -Computer $GPUpdateServer -RandomDelayInMinutes 0
            }
    } End of ForEach Statement
    
    #If running on multiple computers / servers etc. - - See Lines 5 and 8
    ForEach($ComputerName in $ServerList) {
        
        #Update Windows Something Locally - See Line 2
        #$LocalSession = New-PSSession -Computername $RequestingServer -Credential $LocalCredentials
        
        #Update Windows Something Remotely - See Lines 5 and 8
        $LocalSession = New-PSSession -Computername $ComputerName -Credential $LocalCredentials
            Invoke-Command -Session $LocalSession -ScriptBlock {
            
            #Some commands need the computername currently using localhost...
            $GPUpdateServer = $Using:ComputerName
            #$GPUpdateServer = $Using:RequestingServer
            
            # enter code of what you plan to do... 
            Invoke-GPUpdate -Computer $GPUpdateServer -RandomDelayInMinutes 0
            }
            
    } End of ForEach Statement
    
    

    【讨论】:

    • 您好 Neo,我将尝试这种方法,但这需要在所有客户端上启用 Windows 远程管理,以便使用 New-PS-Session 处理每个客户端的 PowerShell 远程处理。不完全是我首先想通过 GPO 部署的。
    【解决方案2】:

    更详细地面对这个问题,我用远程 PowerShell 会话测试了上述方法。这需要在域中进行更多准备,以便将所有必要的 GPO 设置部署到所有客户端以使 WinRM 正常工作。

    远程 PowerShell 方法有效,但我发现 Invoke-GPUpdate 命令仅在安装了 RSAT 的客户端上可用。所以只适用于 IT 部门的少数客户。

    $Session = New-PSSession -Computername $clientname -Credential $domainAccountWithLocalAdminRights
    
    Invoke-Command -Session $Session -ScriptBlock { Invoke-GPUpdate -Computer $env:ComputerName -RandomDelayInMinutes 0 }
    
    $Session | Remove-PSSession
    

    我切换到了另一种方法,该方法对我有用,而无需使用远程 PS 会话。在客户端完全静默,您只会在 Windows 事件查看器中找到触发的 gpupdates。

    Invoke-Command -ComputerName $clientname -ScriptBlock { gpupdate } -Credential $domainAccountWithLocalAdminRights
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2016-02-04
      • 2019-02-24
      相关资源
      最近更新 更多