Copy and run registry import via powershell from variables.

Morning all,

So basically this is the thing, I have a list of .REG files which I want to remote execute on 30 machines, each .REG file has different content so I can't push it out to all machines without using variables..

It all goes into the HKLM which is fine.

Right so this is what i'm trying to do.



    

$Source = "C:\temp\cisco\$.User.reg"
import-csv "C:\powershell projects\Cisco\CODE\Cisco.csv" | ForEach-Object{

   $X = gwmi win32_share -ComputerName $_.machinename} | Where-Object {$_.Name -eq "Support$"}
       if($X.Name -eq 'Support$')
       {
           $dest = "Support$\Temp"
       }
       else
       {
          $dest = "C$\Temp"
       }

   Copy-Item $source -Destination \\$_.machinename\$dest\Regkey.reg -Recurse -Force

   }

Open in new window



This is to import the CSV, then take the "machinename" column and then put that in the into the script so I can connect and the $Source is also from the same CSV file which will then copy from a list of reg keys which are named by user

{
if (test-Connection -Cn $_.Machinename
 -quiet) {
C:\Temp\GetRemoteKey\PsExec.exe -d -s -accepteula "\\ $_.Machinename" -c -f "C:\Temp\GetRemoteKey\get.cmd"
} 
else {
"$computer is not online" | Out-file -Append C:\powershell projects\Cisco\CODE\computersnotonline.txt
}
}

Open in new window


This bit then executes the CMD which is just importing the Regkey.reg into the registry of that machine.


Now I know you can do this with powershell natively, unfortunately I can't, security rules block it. But i'm struggling to get this to work properly. Any ideas?

Thanks
Alex

P.S Script in it's entirety

    
import-csv "C:\powershell projects\Cisco\CODE\Cisco.csv" | ForEach-Object{

   $X = gwmi win32_share -ComputerName $_.machinename} | Where-Object {$_.Name -eq "Support$"}
       if($X.Name -eq 'Support$')
       {
           $dest = "Support$\Temp"
       }
       else
       {
          $dest = "C$\Temp"
       }

   Copy-Item $source -Destination \\$_.computername\$dest -Recurse -Force

   }

{
if (test-Connection -Cn $_.machineanme -quiet) {
C:\Temp\GetRemoteKey\PsExec.exe -d -s -accepteula "\\$_.machinename" -c -f "C:\Temp\GetRemoteKey\get.cmd"
} 
else {
"$computer is not online" | Out-file -Append C:\Temp\GetRemoteKey\computersnotonline.txt
}
}

Open in new window

LVL 15
Alex Green3rd Line Server SupportAsked:
Who is Participating?
 
footechConnect With a Mentor Commented:
Without trying to change much, here's a corrected version that should work.  Might as well put the Test-Connection at the start to avoid some timeouts with WMI queries.
Import-Csv "C:\powershell projects\Cisco\CODE\Cisco.csv" | ForEach-Object{
    if (Test-Connection -ComputerName $_.machinename -quiet) {
        $X = gwmi win32_share -ComputerName $_.machinename | Where-Object {$_.Name -eq "Support$"}
        if ($X.Name -eq 'Support$')
        {
            $dest = "Support$\Temp"
        }
        else
        {
            $dest = "C$\Temp"
        }

        Copy-Item $source -Destination "\\$($_.machinename)\$dest" -Recurse -Force

        C:\Temp\GetRemoteKey\PsExec.exe -d -s -accepteula "\\$($_.machinename)" -c -f "C:\Temp\GetRemoteKey\get.cmd"
    } 
    else {
        "$($_.machinename) is not online" | Out-file -Append C:\Temp\GetRemoteKey\computersnotonline.txt
    }
}

Open in new window

0
 
Alex Green3rd Line Server SupportAuthor Commented:
Footech,

Thanks for your input, it's good to know it wasn't too much

$source = "C:\Temp\Cisco\Code\update.cmd"
$regsource = "C:\Temp\Cisco\Code\$($_.user).reg"

Import-Csv "C:\Temp\Cisco\Code\Cisco.csv" | ForEach-Object{
    if (Test-Connection -ComputerName $_.machinename -quiet) {
        $X = gwmi win32_share -ComputerName $_.machinename | Where-Object {$_.Name -eq "Support$"}
        if ($X.Name -eq 'Support$')
        {
            $dest = "Support$\Temp"
        }
        else
        {
            $dest = "C$\Temp"
        }

        Copy-Item $source -Destination "\\$($_.machinename)\$dest" -Recurse -Force
        Copy-Item $regsource -Destination "\\$($_.machinename)\$dest\Cisco.reg" -Recurse -Force
       C:\Temp\Cisco\Code\PsExec.exe -d -s -accepteula "\\$($_.machinename)" -c -f "C:\Temp\update.cmd"
    } 
    else {
        "$($_.machinename) is not online" | Out-file -Append C:\Temp\Cisco\Code\computersnotonline.txt
    }
}

Open in new window


OK so I've changed it slightly to change the locations, however, I'm currently getting the following errors

screenprint

I think this is because my variables are sitting outside of the import-csv maybe?
0
 
footechCommented:
For this -
$regsource = "C:\Temp\Cisco\Code\$($_.user).reg"
to mean anything (specifically the $($_.user) part), it has to be inside the foreach loop after Import-CSV.

The second error is from command you're trying to run with psexec ("C:\Temp\update.cmd" doesn't exist).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.