使用凭据更改多个服务器上注册表中的值

Change value in registry on multiple servers using credentials

希望在多台远程机器上启用注册码。 尝试 1:

$Servers = Get-Content "C:\PowerShell\TestServers.txt"
$Path = "HKLM:\SYSTEM\CurrentControlSet\Services\"
$Property = "*REG_WORD NAME*"
$Value = "1"

Foreach ($Server in $Servers) 
{
  Set-ItemProperty -Path $Path -Name $Property -Value $Value
}

错误:Set-ItemProperty:不允许请求的注册表访问。 注意:检查有效访问,正在使用的帐户对特定配置单元具有 FULLControl 尝试 2: 创建了一个函数,添加了 get-credential cmdlet

function Set-RemoteRegistryValue {
    param (
        $ComputerName,
        $Path,
        $Name,
        $Value,
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty
    )
    
        $null = Invoke-Command -ComputerName $ComputerName -ScriptBlock {
            Set-ItemProperty -Path $using:Path -Name $using:Name -Value $using:Value
        } -Credential $Credential
}

我现在可以调用该函数并根据需要设置注册表键值,但一次只能在一台机器上使用:

$remoteKeyParams = @{
    ComputerName ='name' 
    Path = "HKLM:\SYSTEM\CurrentControlSet\Services\"
    Name = "*keyname*"
    Value = "1"
}
    Set-RemoteRegistryValue @remoteKeyParams -Credential (Get-Credential)    

我试过将多台机器作为一个字符串和一个文本文件放入:

[string]$ComputerName = "name","name","name"
 ComputerName = c:\temp\testservers.txt

我是不是做错了什么?

确认你每行有一个服务器,然后你应该这样写。

$Servers = Get-Content "C:\PowerShell\TestServers.txt"
$Path = "HKLM:\SYSTEM\CurrentControlSet\Services\"
$Property = "*REG_WORD NAME*"
$Value = "1"

Invoke-Command -ComputerName $servers -ScriptBlock {
            Set-ItemProperty -Path $using:Path -Name $using:Name -Value $using:Value
        } -Credential $Credential

当您将所有服务器名称传递给 Invoke-Command 时,它将 运行 它们全部异步(在 5.1 上默认最多 32 个)