0

I am attempting to create a generic script that will process registry files on a list of remote servers. The script will take the input of the path/filename to process and a list of servers to process it on. Copies the .reg file to the remote server and then attempts to process the .reg file on the remote server. In order to make it generic for use with any .reg file I want to pass the path\filename to process in a variable in the script block. I have seen examples of passing named variables and positional parameters but that doesn't seem to meet my requirements. I tried creating the script block contents as a Scriptblock type and calling it but it does not work. Most of the errors I have been getting are related to invalid parsing, ambiguous parameter, etc. I have seen a couple of working examples in which the .reg file path/filename is hard-coded but that defeats the purpose of what I am attempting to accomplish. I have tried the Invoke-Command with a session and with the -ComputerName variable in order to try the :using:" command with no success. Is there any way to pass a variable that is basically the command line values (switch params & filepath/name) for an executable within the scriptblock or am I going about this in the wrong manner? In code below I am crossing domains so having to open session for those servers in order to create directory if it doesn't exist.

    while ($true)
{
    $regFile = Read-Host -Prompt "Enter the path & name of registry file to be processed"
    If(Test-Path -Path $regFile) { break }

    Write-Host "You must enter valid path & filename of a registry file to process."    
}

$servers = Get-Content D:\MLB\Scripts\servers.txt
$fileNm = [System.IO.Path]::GetFileName($regFile)

$pass = ConvertTo-SecureString "blahblah" -AsPlainText -Force
$Creds = new-object -typename System.Management.Automation.PSCredential( "domain\username", $pass)

foreach ($server in $servers)
{
    $dirPath = ''
    $newfile = '\\' + $server + '\d$\MLB\RegFiles\' + $fileNm
    
    if($server.ToLower().Contains("web"))
    {
        $Session = New-PSSession -ComputerName $server -Credential $Creds
 
        Invoke-Command -Session $Session -ScriptBlock { New-Item -ErrorAction SilentlyContinue -ItemType directory -Path D:\MLB\RegFiles }

        $newfile = "d:\MLB\RegFiles\" + $fileNm
        Copy-Item $regFile -Destination $newfile -ToSession $Session -Force
        Remove-PSSession -Session $Session
    }
    else
    {
        $dirPath = "\\$server\d`$\MLB\RegFiles"
        New-Item -ErrorAction SilentlyContinue -ItemType directory -Path $dirPath 

        $newfile = "\\$server\d`$\MLB\RegFiles\$fileNm"

        Copy-Item $regFile -Destination $newfile -Force

     }

     Invoke-Command -ComputerName $server -Credential $Creds -ScriptBlock {
         $args = "s/ $newfile"
         Start-Process -filepath "C:\Windows\regedit.exe" -Argumentlist $args
     }
 
  • 1
    `$scriptblock = { $ArgList = '/s',$using:newfile; & 'c:\windows\regedit.exe' $ArgList }` may work. – AdminOfThings Aug 12 '21 at 19:06
  • Thank you AdminOfThings but that did not work. Tried it initially almost the same way you have it typed above but Included the Start-Process command and it errored on the "$using" command can only be used with Invoke-Commnad. – user1757986 Aug 13 '21 at 18:42
  • If the goal is to update a remote registry with a remote file on the same system, then my example works. I just tested it. Don't use `Start-Process`. Since you are using `/s` with your `regedit.exe`, you are foregoing the use of a GUI, which will make `Start-Process` not useful UNLESS you want to execute asynchronously. You should not define `$args` as a custom variable because that is an automatic variable in PowerShell. If `$using:` errors, then you must be using PowerShell v2 or lower and it would be best to tag the question with the PowerShell version. – AdminOfThings Aug 13 '21 at 19:45
  • If `$using:` is not working due to a version issue, perhaps you can do `Invoke-Command -ComputerName $server -Credential $Creds -ScriptBlock { & "C:\Windows\regedit.exe" $args } -ArgumentList '/s',$newfile` – AdminOfThings Aug 13 '21 at 21:43
  • Thank you @AdminOfThings. you da man. That did work successfully. Sorry for misunderstanding your comment previously. This is exactly what I was looking for, – user1757986 Aug 16 '21 at 13:55

0 Answers0