Import .reg files remotely with PowerShell and PSSession

Been into PowerShell and Automation for a while now.

#Creating PSCredential object Snippit
$User = “M-toTheI’ke”
$File = “\\Contra2\D$\Powershell\Password\Password.txt”
$MyCredential= New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)

cls
#Build Session
$servers = Get-Content “c:\Powershell\Servers.txt”

Location of Registry file to import. Notice “.reg”
$RegFile = “\\Contra2\D$\Player2.reg”

foreach ($server in $servers)
{

<#Building a session for remote servers based on the content of the servers.txt #>
$Session = New-PSSession -computername $server -credential $MyCredential

<#Taking file from hosted location of file and forcing it to the built session #>
Copy-Item -Path “$RegFile” -Destination “C:\Utils\” -force -ToSession ` $session

#Validate File exists **Code here** NOT DONE

#Command to run the regedit to import the newly copied player2.reg
Invoke-Command -session $session -ScriptBlock {regedit /i /s C:\Utils\player2.reg}
}

 

Many ways to skin this but I can now use this to multiple remote servers as long as the WINRM port and services will accept the session remotely.

This entry was posted in Powershell, Scripting. Bookmark the permalink.