Wednesday, October 2, 2013

Powershell Snippet - Copy/Distribute a File to Every Exchange Server, With Preference Order as to Which Target Drive

10/02/2013: Here's another handy Powershell script snippet: This time, a chunk of code I use for quickly distributing a new or updated file to all or a subset of Exchange servers using Powershell+Exchange Management Shell

Pretty simple process going on below:
  1. I store a list of drive letters to be tested into an array (in preference order)
  2. Then utilize the EMS get-exchangeserver cmdlet to collect a filtered set of all exchange servers (in this case, using
    get-exchangeserver | where{$_.isHubTransportServer -eq $true} to return only HubTransports anywhere in the Org)
  3. I then loop the exchange servers list against the drives array, testing for a functional path (which indicates a suitable target drive), and using the {break} command to drop out of the loop at the first matching drive in the list. 
  4. Whereuopon the designated file is copied to the matching UNC path

Copy/Distribute file to every exchange server, with preference order  as to which target drive: (1-line below; the command skips tests and copies to the first match)
Here's a Gist with the code:

Static-source follows (for folks in RSS readers, or disabling scripting):
(check the linked Gist/Github links below for _current_ versions).

# 1-line, wrapped at pipes and semi-colons for readability 
$drvs="e","f","c" ; get-exchangeserver |
    where{$_.isHubTransportServer -eq $true} |
            write-host $_ ; 
            foreach ($drv in $drvs) { if (test-path \\$_\$drv`$\scripts\) { break  } } ; 
            copy \\SourceServer\e$\scripts\get-HT-MsgTrk-TopTraffic-LastXMin.ps1 \\$_\$drv`$\scripts\ -whatif ; 
        } ;

No comments:

Post a Comment