none
Powershell - find if a value is present RRS feed

  • Question

  • Hi,

    I'm writing a DHCP migration tool in powershel, but struggling with one of the options. Here is the scenario:

    I want to copy over all server option from the source server to the destination server. So I can use these commands

    Get-DhcpServerv4OptionDefinition -ComputerName 

    and

    Add-DhcpServerv4OptionDefinition -ComputerName 

    The issue with this is that if most of the server options are standard and cause an error when I run it like this

    Get-DhcpServerv4OptionDefinition -ComputerName source-servername | Get-DhcpServerv4OptionDefinition -ComputerName dest-servername

    the error is the value already exists.

    So what I am trying to do is for each dhcp option in the source server, check if it exists in the desitnation server, and if it doesn't add it.

    the value of the commands display like this

    Name                 OptionId             Type                 VendorClass          MultiValued        

    ----                 --------             ----                 -----------          -----------        

    Classless Static ... 121                  BinaryData                                False              

    Subnet Mask          1                    IPv4Address                               False              

    Time Offset          2                    DWord                                     False              

    Router               3                    IPv4Address                               True               

    Time Server          4                    IPv4Address                               True               

    Name Servers         5                    IPv4Address                               True               

    DNS Servers          6                    IPv4Address                               True               

    Any ideas on the best way to code this? Im trying with if statements and -contains but not having a great deal of luck.


    Regards,

    Denis Cooper

    MCITP EA - MCT

    Help keep the forums tidy, if this has helped please mark it as an answer

    My Blog

    LinkedIn:

    Monday, January 13, 2014 1:20 PM

Answers

  • Try this:

    Get-DhcpServerv4OptionDefinition -ComputerName source-servername | 
         Add-DhcpServerv4OptionDefinition -ComputerName dest-servername-ea 0


    ¯\_(ツ)_/¯

    • Marked as answer by Denis Cooper Monday, January 13, 2014 3:48 PM
    Monday, January 13, 2014 2:51 PM
  • Hi Denis,

    jrv's solution is a lot more elegant than mine. However, unless I'm rather wrong, it will not notice when there are different definitions on the same OptionId and just silently continue without changing those definitions to the same ones used on your old system. If that's not an issue, it doesn't get any sleeker than jrv's solution.

    If it is an issue, this may help:

    # Grab info on old DHCP
    $OldConfig = Get-DhcpServerv4OptionDefinition -ComputerName $OldDHCP
    
    # Grab the already existing config from new DHCP
    $NewConfig = Get-DhcpServerv4OptionDefinition -ComputerName $NewDHCP
    
    # Go through each of the old definition sets
    Foreach ($Config in $OldConfig)
    {
    	# Test variable
    	$found = $false
    	
    	# Go through each config of the new dhcp and compare it with the config item of the old DHCP we are handling right now
    	foreach ($nConfig in $NewConfig)
    	{
    		# Compare the two config IDs
    		if ($Config.OptionId -eq $nConfig.OptionId)
    		{
    			# If they are the same, mark it found
    			$found = $true
    			
    			# If they share the ID, but have different definitions, overwrite the default settings with the ones you had on the old DHCP
    			if ($Config -ne $nConfig){$config | Set-DhcpServerv4OptionDefinition -ComputerName $NewDHCP}
    		}
    	}
    	
    	# If you didn't find the old config on the new DHCP, create a new definition
    	if (!$found){$config | Add-DhcpServerv4OptionDefinition -ComputerName $NewDHCP}
    }

    Please note that I don't have some test-DHCP servers at hand, this was create from the technet documentation on the functions, so if you want to use this, you may want to run a test on this before using it in a production environment :)

    Seriously though, in most situations the solution jrv posted above is bound to be superior.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Denis Cooper Monday, January 13, 2014 3:48 PM
    Monday, January 13, 2014 3:07 PM

All replies

  • Hi,

    I'm writing a DHCP migration tool in powershel, but struggling with one of the options. Here is the scenario:

    I want to copy over all server option from the source server to the destination server. So I can use these commands

    Get-DhcpServerv4OptionDefinition -ComputerName 

    and

    Add-DhcpServerv4OptionDefinition -ComputerName 

    The issue with this is that if most of the server options are standard and cause an error when I run it like this

    Get-DhcpServerv4OptionDefinition -ComputerName source-servername | Get-DhcpServerv4OptionDefinition -ComputerName dest-servername

    the error is the value already exists.

    So what I am trying to do is for each dhcp option in the source server, check if it exists in the desitnation server, and if it doesn't add it.

    the value of the commands display like this

    Name                 OptionId             Type                 VendorClass          MultiValued        

    ----                 --------             ----                 -----------          -----------        

    Classless Static ... 121                  BinaryData                                False              

    Subnet Mask          1                    IPv4Address                               False              

    Time Offset          2                    DWord                                     False              

    Router               3                    IPv4Address                               True               

    Time Server          4                    IPv4Address                               True               

    Name Servers         5                    IPv4Address                               True               

    DNS Servers          6                    IPv4Address                               True               

    Any ideas on the best way to code this? Im trying with if statements and -contains but not having a great deal of luck.

     


    Regards,

    Denis Cooper

    MCITP EA - MCT

    Help keep the forums tidy, if this has helped please mark it as an answer

    My Blog

    LinkedIn:

    Monday, January 13, 2014 1:18 PM
  • Try this:

    Get-DhcpServerv4OptionDefinition -ComputerName source-servername | 
         Add-DhcpServerv4OptionDefinition -ComputerName dest-servername-ea 0


    ¯\_(ツ)_/¯

    • Marked as answer by Denis Cooper Monday, January 13, 2014 3:48 PM
    Monday, January 13, 2014 2:51 PM
  • This is a duplicate question. Please try no to ask the same question multiple ways.  It only makes it harder.

    ¯\_(ツ)_/¯

    Monday, January 13, 2014 2:52 PM
  • Hi Denis,

    jrv's solution is a lot more elegant than mine. However, unless I'm rather wrong, it will not notice when there are different definitions on the same OptionId and just silently continue without changing those definitions to the same ones used on your old system. If that's not an issue, it doesn't get any sleeker than jrv's solution.

    If it is an issue, this may help:

    # Grab info on old DHCP
    $OldConfig = Get-DhcpServerv4OptionDefinition -ComputerName $OldDHCP
    
    # Grab the already existing config from new DHCP
    $NewConfig = Get-DhcpServerv4OptionDefinition -ComputerName $NewDHCP
    
    # Go through each of the old definition sets
    Foreach ($Config in $OldConfig)
    {
    	# Test variable
    	$found = $false
    	
    	# Go through each config of the new dhcp and compare it with the config item of the old DHCP we are handling right now
    	foreach ($nConfig in $NewConfig)
    	{
    		# Compare the two config IDs
    		if ($Config.OptionId -eq $nConfig.OptionId)
    		{
    			# If they are the same, mark it found
    			$found = $true
    			
    			# If they share the ID, but have different definitions, overwrite the default settings with the ones you had on the old DHCP
    			if ($Config -ne $nConfig){$config | Set-DhcpServerv4OptionDefinition -ComputerName $NewDHCP}
    		}
    	}
    	
    	# If you didn't find the old config on the new DHCP, create a new definition
    	if (!$found){$config | Add-DhcpServerv4OptionDefinition -ComputerName $NewDHCP}
    }

    Please note that I don't have some test-DHCP servers at hand, this was create from the technet documentation on the functions, so if you want to use this, you may want to run a test on this before using it in a production environment :)

    Seriously though, in most situations the solution jrv posted above is bound to be superior.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Denis Cooper Monday, January 13, 2014 3:48 PM
    Monday, January 13, 2014 3:07 PM
  • Actually the simplest method is to export at one and import into the second but the exact purpose of this exercise  would determine how to go forwards.

    Ignoring errors on duplicates is all that is needed to just update a second server.  Errors on duplicates will not cause issues as the dupe will just be ignored.

    This is a one-off, one-time task so just ignore the errors.


    ¯\_(ツ)_/¯

    Monday, January 13, 2014 3:11 PM
  • yes - sorry about that - it winds me up too when people ask twice....I think something went wrong when I first submitted and it didn't go through properly and converted all my post to html - so I deleted it - but it looks as if it didn't delete and ended up posting correctly.

    Regards,

    Denis Cooper

    MCITP EA - MCT

    Help keep the forums tidy, if this has helped please mark it as an answer

    My Blog

    LinkedIn:

    Monday, January 13, 2014 3:14 PM
  • great - thanks - will give both options a go.

    so just for my understanding the -ea 0 sets the error reporting to continue on error?


    Regards,

    Denis Cooper

    MCITP EA - MCT

    Help keep the forums tidy, if this has helped please mark it as an answer

    My Blog

    LinkedIn:

    Monday, January 13, 2014 3:17 PM
  • Hi,

    For information about -ea (which is alias for -ErrorAction), see the PowerShell help:


    PS C:\> help about_CommonParameters

    Bill

    Monday, January 13, 2014 3:20 PM
    Moderator