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 18
Alex GreenProject Systems EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex GreenProject Systems EngineerAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.