We help IT Professionals succeed at work.

Explain Powershell Command

jskfan
jskfan asked
on
Medium Priority
64 Views
Last Modified: 2020-02-20
Explain Powershell Command

I have this Powershell code, I would like to have an Expert explain it. some of it is clear , some of it not sure what it does.
for instance:

 %
$env:temp

Thank you.



import-csv C:\CSVFILE.csv | % { Invoke-Command -ComputerName $($_.COMPUTERNAME) -ScriptBlock { copy "\\server\share\regFile.reg" $env:temp ;  reg.exe import $env:temp\regFile.reg } 

Open in new window

Comment
Watch Question

David Johnson, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
% Is an alias to for-eachobject
$env:temp  reads the environmental variables for the name temp and uses the value
import-csv C:\CSVFILE.csv | ForEach-Object
     { 
       Invoke-Command -ComputerName $($_.COMPUTERNAME) -ScriptBlock 
          { 
          Copy-Item "\\server\share\regFile.reg" $env:temp 
          reg.exe import $env:temp\regFile.reg 
          }
      }
       

Open in new window

Tom CieslikIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
David has right.
You can also study this article, maybe this will give you some more explanation
https://blogs.msdn.microsoft.com/timid/2009/10/11/powershell-for-n00bs-envtemp-vs-temp/
CEO Faru Bonon IT /Top Rated Freelancer on Upwork / Photographer
CERTIFIED EXPERT
Awarded 2018
Distinguished Expert 2018
Commented:
import-csv C:\CSVFILE.csv | % { Invoke-Command -ComputerName $($_.COMPUTERNAME) -ScriptBlock { copy "\\server\share\regFile.reg" $env:temp ;  reg.exe import $env:temp\regFile.reg } 

Open in new window


This script is called a "oneliner", it's a single line with several commands glued with a pipe "|"
The | means what you obtain on the left of it, give it to the cmdlet that is on the right... so example "Get-Mailbox | Get-MailboxStatistics"  will get all the mailboxes on the server and then ask for the stats of each.

So with that in mind, I'll go cmdlet by the cmdlet
Import-csv C:\CSVFILE.csv  (Gets the information of the CSV that contains the "ComputerNames")

%{<whatever>} (for each value on the previous import you're going to run whatever you put inside the curly braces (I mark them as <Whatever>

Invoke-Command execute a command within the local or remote computer, but with the -scriptblock parameter you're giving it an "extra script to run to inside the curly braces"
Example:
Invoke-Command -computer Remote1 -scriptblock {   <Another script to run against the Remote1 host> }

$($_.COMPUTERNAME) (means to use the computername on the CSV for that particular entry and use $( ) to get a result before executing the current line. (the Invoke-command)

Within the same script, you can use ";" and write a lot of Cmdlets within the same line
Example : Cmdlet1; cmdlet2;cmdlet3;
(don't mind the spaces between them) soooo:


copy "\\server\share\regFile.reg" $env:temp ;    //copy the regfIle in the shared \\server\share to the "temporal value" on C:\windows\temp
reg.exe import $env:temp\regFile.reg     //this will run the import to add the registry file (now in C:\windows\temp\regFile.reg) into that particular computer.

Author

Commented:
Sorry Guys for the Delay...was busy...

Can someone elaborate on this part of the code below ?

Copy "\\server\share\regFile.reg" $env:temp ;    //copy the regfIle in the shared \\server\share to the "temporal value" on C:\windows\temp
reg.exe import $env:temp\regFile.reg     //this will run the import to add the registry file (now in C:\windows\temp\regFile.reg) 
into that particular computer.

Open in new window



If the copy was to different folder  instead of windows temp folder , then should we create an environment variable for that folder ?

I also see Reg.exe in the code , what does it do ?  I am familiar with Regedit.exe, but not reg.exe

Thank you

Author

Commented:
Thanks
I will come back to this topic sometime in the future
David Johnson, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
reg /?

REG Operation [Parameter List]

  Operation  [ QUERY   | ADD    | DELETE  | COPY    |
               SAVE    | LOAD   | UNLOAD  | RESTORE |
               COMPARE | EXPORT | IMPORT  | FLAGS ]

Return Code: (Except for REG COMPARE)

  0 - Successful
  1 - Failed

For help on a specific operation type:

  REG Operation /?

Examples:

  REG QUERY /?
  REG ADD /?
  REG DELETE /?
  REG COPY /?
  REG SAVE /?
  REG RESTORE /?
  REG LOAD /?
  REG UNLOAD /?
  REG COMPARE /?
  REG EXPORT /?
  REG IMPORT /?
  REG FLAGS /?

Open in new window

 REG IMPORT AppBkUp.reg
    Imports registry entries from the file AppBkUp.reg

If the copy was to different folder  instead of windows temp folder , then should we create an environment variable for that folder ?
no just provide a full path to the .reg file