I have tried creating a batch file using PsExec to update registry settings on remote computers without success so I am now trying to use Powershell. I have compiled the following script by using Google and playing about with scripts available on different forums.
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
if ($myWindowsPrincipal.IsInRole($adminRole))
{
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
$Host.UI.RawUI.BackgroundColor = "Darkred"
clear-host
}
else
{
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
$newProcess.Verb = "runas";
[System.Diagnostics.Process]::Start($newProcess);
exit
}
winrm quickconfig
$servers = Get-Content c:\temp\servers.txt
$HostedRegFile = "temp\OfficeDocumentfix.reg"
foreach ($server in $servers)
{
$newfile = "\\$servers\c`$\Downloads\RegistryFiles\"
New-Item -ErrorAction SilentlyContinue -ItemType directory -Path \\$servers\C$\Downloads\RegistryFiles\
Copy-Item $HostedRegFile -Destination $newfile
Invoke-Command -ComputerName $server -ScriptBlock {
Start-Process -filepath "C:\windows\regedit.exe" -argumentlist "/s C:\Downloads\RegistryFiles\test.reg"
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$returncode = $?+":"+$lastexitcode;
$codearr = $returncode.split(":");
write-host $codearr[0];
write-host $codearr[1];
#echo Registry_updated_successfully
#:Failed
#echo Registry_update_failed
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
Now for my Problem, running the script, I get the following error
WinRM already is set up to receive requests on this machine.
WinRM already is set up for remote management on this machine.
Copy-item : Cannot find path 'C:\OfficeDocumentfix.reg' because it does not exist.
But the path is correct, is there something blatantly obviously wrong with the script I am using, this is my 2nd push into Powershell so any help would be appreciated
Best Answer
delete this
winrm quickconfig
out of your script. it wants to configure your WinRM service but it's already set up, so there's no need for this. WinRM lets you access Remote Computer via its Service, it's needed for e.ginvoke-command {}
.the reason for this is, that your
$newfile
variable uses$servers
instead of$server
as it should (because it's inside theforeach()
block) so$servers
is$null
. that's the cause of the error.if the Registry File you're using writes to HKCU:, you don't need to elevate the script, if it's writing to HKLM: you have to. Only Admins can write to HKLM. The elevation is the whole part from the beginning up to
winrm quickconfig
.this should give you the following end-product (I left the elevation in there):
please do also take a look at this line:
it's not dynamic yet. it will always read in
test.reg
instead of your desired reg-file.instead of
start-process
you could also simply useregedit /s $regfile /f
PowerShell can execute batch-like commands (but that's detail. if it works like this, leave it like it is).