Remote PSSession and Scope Modifiers

I had a task to deploy a simple setup of DHCP service on a few branch office servers. I took it as a great opportunity to put my PowerShell scripting skills to test and add get some more experience with remote sessions and working with remote variables by using Scope Modifier.

The requirements where that each server had to be authorized in AD, have a small IP range scope, configure the clients to have a local DNS server as their preferable DNS as well as have two datacenters DNS servers configured (in the code sample they are 10.0.0.1 and 10.0.0.2) and the DNS domain name as a suffix

So we start with a CSV with 6 headers (Name, ScopeName, StartRange, EndRange, Description, DNS)

Name, name of server to configure
ScopeName, name of the scope to configure. This could be the branch or office name/code
StartRange, start of the IP range. Such as 192.168.1.51
EndRange, end of the IP range. Such as 192.168.1.60
Description, description for the scope. Such as “Subnet 1 in Office 1”
DNS, IP address of the local DNS server to configure the clients

The interesting thing when doing remote PSSession and variables is that you cannot take your local variables with you into your remote sessions.  You will have to call them using a method called Scope Modifier which is simply putting $using:variable1 to use a variable called $variable1, you have setup earlier.  There are other methods of getting local variables into your remote session but the Scope Modifier is in my opinion by far the most simple to use.  It however only works with PS v3 and above.

You can see this in action in the code bellow.

#Import your servers (Name, ScopeName, StartRange, EndRange, Description, DNS)
$Servers = Import-CSV "C:\CSVs\DHCP-Servers.csv"

foreach ($Server in $servers){

#As each server will have different variables they are loaded for each server
        $ServerName = $Server.name
        $ScopeName = $Server.ScopeName
        $StartRange = $Server.StartRange
        $EndRange = $Server.EndRange
        $Description = $Server.Description        
        $DNS = $Server.dns
        
        
        #This creates a remote PSSession to each server at a time
        $Session = New-PSSession -ComputerName $Servername
        
        Invoke-Command -Session $Session -ScriptBlock {
        
        Add-Windowsfeature -name DHCP -IncludeManagementTools
        
        Add-DHCPserverInDC -confirm
        
        Add-DhcpServerv4Scope -Name $Using:scopename -StartRange $Using:StartRange -EndRange $Using:EndRange -SubnetMask 255.255.255.0 -Description $Using:Description
        
        Set-DhcpServerv4OptionValue -Optionid 6 $Using:DNS, 10.0.0.1, 10.0.0.2
        
        Set-DhcpServerv4OptionValue -Optionid 15 mydomain.local}        

        }
Advertisements
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s