Power Shell Loop

How to write, read and look super cool with a PowerShell loop. Below are two different methods to accomplish the same task; basically get a list of server names, and run a command against all of the names. To run the command this way we need to loop through a list of a servers names, and we need to prepend the server name to some tesxt.

Quick and dirty

Get-ClientAccessServer
|
%{$_.Name} |
%{Set-OWAVirtualDirectory
$_\(Default Web Site)”
-ChangePasswordEnabled:$False}

Or clean and with style

[String[]]$Machines =
Get-ClientAccessServer
|
%{$_.Name} |
Sort

foreach ($Machine
in
$Machines) {

       [String]$DefaulWebSite =
“{0}\(Default Web Site)”
-format
$Machine

       Set-OWAVirtualDirectory
$DefaulWebSite
-ChangePasswordEnabled:$False

}

 

Now we are going to go over each piece of the above operations word by word and explain what is going on. Starting with the Quick and Dirty method:

  1. Get-clientaccesserver |   – extract all of the results of this command and send it to the pipe
  2.  % – shortcut for foreach – Meaning we are going to loop line by line through the stuff we took in via the pipe from the PS command run before
  3. {$_.Name}
    – all we care about out of the stuff in the pipe is the name. Toss all of the rest of the stuff out of memory and shove the name through the pipe to the next command
  4. % – shortcut for foreach – Meaning we are going to loop line by line through the stuff we took in via the pipe from the PS command run before
  5. {Set-OWAVirtualDirectory “$_\(Default Web Site)” -ChangePasswordEnabled:$False} – We are going to run the command Set-QwaVirturalDirecty for each name in the pipe. We are also prepending the server name in the pipe ($_} to the words \(Default Web Site) to make something like ServerName\(Default Web Site) finally we are setting ChangePasswordEnabled to $ false

 

Now we are going to go over each piece of the above operations word by word and explain what is going on. Next for the clean and with style method:

  1. [String[]]$Machines
    – we are declaring $machines as a sting variable we can stick stuff in
  2. Get-clientaccesserver |   – extract all of the results of this command and send it to the pipe
  3.  % – shortcut for foreach – Meaning we are going to loop line by line through the stuff we took in via the pipe from the PS command run before
  4. {$_.Name} |
    – all we care about out of the stuff in the pipe is the name. Toss all of the rest of the stuff out of memory and shove the name through the pipe to the next command
  5. Sort – we are putting all of the names in alphabetical order; I said this was the pretty method
  6. foreach ($Machine in $Machines)
    – loop through all of the stuff in the $Machines variable one at a time. As you take a line stick the stuff in the $Machine variable
  7. {       [String]$DefaulWebSite “{0}\(Default Web Site)” -format $Machine – Declare a string $DefaultWebsite and stick the value $machine + \(Default Web Site) in it to make something like ServerName\(Default Web Site)
  8. Set-OWAVirtualDirectory $DefaulWebSite -ChangePasswordEnabled:$False }
    – Run the command Set-OwaVirturalDirecty ServerName\(Default Web Site) and set the ChangePasswordEnabled variable to $False
Related Posts with Thumbnails

About Kevinm