2016-09-26 48 views
1

我創建與下面的代碼遠程計算機上的註冊表項中創建註冊表:如何將多個遠程機器

$basePath="C:\Users\<User>\Desktop\Script\" 
$remoteMachineName = $basePath + "server.txt" 
$arrServer=(Get-Content $remoteMachineName) 

$remoteUserPassword = Get-Content "C:\Users\<UserName>\Desktop\Script\pass.txt" 
ConvertTo-SecureString -AsPlainText -Force -String $password 
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "UserName" 

    Enter-PSSession -ComputerName $server -Credential $credentials 
New-ItemProperty -Name "myReg" -Value "ABC" -PropertyType "String" -Path "HKLM:\SOFTWARE\Usertest" 

但我的本地機器上創建註冊表項,但我想它下面遠程機器給錯誤:

New-ItemProperty : The property already exists. 
At C:\Users\<User>\Desktop\Script\Untitled4.ps1:14 char:1 
+ New-ItemProperty -Name "myReg" -Value "ABC" -PropertyType " ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : ResourceExists: (HKLM:\SOFTWARE\Usertest\:String) [New-ItemProperty], IOException 
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.NewItemPropertyCommand 

但是當我運行命令Enter-PSSessionNew-ItemProperty分開它的工作原理,但我想同時運行的命令。

請幫我在遠程機器上創建註冊表項。

回答

1

只需使用Invoke-Command的cmdlet:

# ... 
Invoke-Command -cn $server -cred $credentials { 
    New-ItemProperty -Name "myReg" -Value "ABC" -PropertyType "String" -Path "HKLM:\SOFTWARE\Usertest" -Force 
} 
+0

我'-force'嘗試很好,但它不工作只是創建我的本地機器,而不是遠程計算機上的註冊表項。 – VIVEK