Home > Powershell > Migrate DHCP Server Settings Using Powershell

Migrate DHCP Server Settings Using Powershell

During my preparation to teach the “Updating Your Skills to Server 2008 R2” class, I saw lot’s of potential to use Powershell to automate some of the mundane tasks, such as creating files and assigning permissions (Module 2) or restoring an AD Computer Object (Module 4) to the more advanced such as using the new Server Migration Tools to migrate the DHCP service from DC1 to SVR1 (Module 1).

I  tried to use functions to perform these tasks which I then had the students dot source (. ./name.ps1) to import then into the current Powershell console.   The functions included Help and Examples which could be copied\pasted for ease of use.   Most of the code was “on point” but there were a few syntax errors I missed along the way, which have now been resolved.

Below is the “Server Migration” script.

Function Migrate-ServerSettings {

<#

.SYNOPSIS

The Migrate-ServerSettings function was created to export\import the DHCP service\configurations using the

“Microsoft Windows Server Migration Tools”.  It queries the existence of several required components and

installs then if needed.  It performs the following

            1.  Queries Get-Command for “Get-Windowsfeature” and if not found runs “Import-Module ServerManager”

            2.  Queries Get-Windowsfeature for “Migration” and if not found runs “Add-WindowsFeature Migration”

            3.  Queries Get-Pssnapin for “Microsoft.Windows.Servermanager.Migration” and if not found runs “Add-PSSnapinMicrosoft.Windows.Servermanager.Migration”

            4.  Queries the DHCPServer service and stops the service if running

            5.  Evaluates $Migrate variable and if value is export runs “Export-SmigServerSetting”.  If value is import runs “Import-SmigServerSetting”

I’ve included some Write-Debug lines to troubleshoot any issue with the $service variable as the paramater value you will use is

DHCP, but the service name is DHCPServer.  The script would have to be adjusted to accomodate other services.

.EXAMPLE

Migrate-ServerSettings -Service DHCP -Path “\\NYC-DC1.contoso.com\export” -Migrate Export

.EXAMPLE

Migrate-ServerSettings -Service DHCP -Path “\\NYC-DC1.contoso.com\export” -Migrate Import

#>

Param(

    [string]$Service,

    [string]$Path,

    [string]$migrate

    )

if(!(get-command * | where{$_.name -eq “get-windowsfeature”})) {

    write-host “`nServerManager Module being installed`n” -Fore Yellow

    import-module servermanager} else {

        write-host “`nServerManager Module already installed`n” -fore Green

        }

if(!(get-windowsfeature -name Migration).installed -eq “False”) {

    write-host “Installing Feature ‘Windows Server Migration Tools`n” -Fore Yellow

    add-windowsfeature Migration} else {

        write-host “Windows Server Migration Tools Already Installed`n” -fore Green

        }

if(!(get-pssnapin | where{$_.name -like “*Migration*”})) {

    write-host “Importing Microsoft.WIndows.ServerManager.Migration Snapin`n” -fore Yellow

    Add-PSSnapin Microsoft.Windows.ServerManager.Migration} else {

        write-host “Microsoft.WIndows.ServerManager.Migration Snapin Already Imported`n” -fore Green

        }

$svc = $service + “server”

if(get-service | where{$_.name -like “$svc”}) {

    if ((get-service $svc).status -eq “Running”) {stop-service $svc -force}

    }

write-debug “‘$service is $service”

if($migrate -eq “export”) { Export-SmigServerSetting -featureID $Service -path $path -Verbose

    } else { Import-SmigServerSetting -featureID $Service -path $path -Verbose }

     } #end function

Advertisements
Categories: Powershell Tags:
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: