0

Trying to set DNS Suffixes that are entered as a parameter into my script. Found that there is a registry key - HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\SearchList and can set them in there but, as the parameters are passed as UK.COM,US.COM,AUS.COM,NL.COM.

The problem is that when I look at the registry key it is removing each comma between the domains.

I found a fix here: Powershell is removing comma from program argument

However, the answer states to put the paramters in quotes - the problem is that the parameter is being read from an external system (which I cannot change) and the line for where it gets this from is this:

<Param><ParamValue>UK.COM,US.COM,NL.COM</ParamValue></Param><Param>

So, how can I keep the comma when this is being passed in?

The input being entered is this (getting rid of the IP's and leaving just the DNS search order):

.\ip_assign.ps1 ip subnet gateway dns_servers dns_domain UK.COM,US.COM,NL.COM pri_wins sec_wins

Script (look at parameter 6)

param (

[Parameter(Mandatory=$true,
            Position = 1)]
            [string]$IP
,
[Parameter(Position = 2)]
[string]$SubnetMask = "none"
,
[Parameter(Position = 3)]
[string]$Gateway = "none" 
,
[Parameter(Position = 4)]
[string[]]$DNSServers
,
[Parameter(Position = 5)]
[string[]]$DNSDomain
,
[Parameter(Position = 6)]
$DNSSuffixs
,
[Parameter(Position = 7)]
[string[]]$PrimaryWINS
,
[Parameter(Position = 8)]
[string[]]$SecondaryWINS
)


$TeamAdaptor = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.Caption -ilike '*Virtual*'}
$TeamAdaptor.EnableStatic($IP,$SubnetMask) | Out-Null
$TeamAdaptor.SetGateways($Gateway) | Out-Null
$TeamAdaptor.SetDNSServerSearchOrder($DNSServers) | Out-Null
$TeamAdaptor.SetDNSDomain($DNSDomain) | Out-Null
$TeamAdaptor.SetWINSServer($PrimaryWINS,$SecondaryWINS) | Out-Null

Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters" -Name "SearchList" -Value $DNSSuffixs
Community
  • 1
  • 1
lara400
  • 4,646
  • 13
  • 46
  • 66

3 Answers3

2

Use single quotes around those items, because otherwise PowerShell interprets commas as separating entries. I added a Write-Host line to debug, and this is what I see

#Write-host "Received $($DNSSuffixs.Count) `$DNSSuffix, which are $DNSSuffixs"

.\ip_assign.ps1 ip subnet gateway dns_servers dns_domain 'UK.COM,US.COM,NL.COM' pri_wins sec_wins

Received 1, which are UK.COM,US.COM,NL.COM

Since you cannot ammend the parameters because of an outside tool

You've mentioned that you have no control over the way an outside tool invokes this script. To still allow you to pass the commas, what we can do it use the -Join operator to rejoin all three items you're passing and make them one item, which happens to have commas in it. Simply add this line to your code:

$DnsSuffixs = $DNSSuffixs -join ','
Write-host "Received items:$($DNSSuffixs.Count) `t value: $DNSSuffixs"

>Received items:1    value: UK.COM,US.COM,NL.COM

This should completely resolve your issue.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • I cannot amend the input paramters - how they are piped in is from a third party tool that I cannot change. I cannot put comma around them - the command I put is me testing the script as to how the third party tool passes them. – lara400 Apr 29 '15 at 13:08
  • 1
    Alright, I added a new approach to my answer. please check it out, I think this one will work for you :) – FoxDeploy Apr 29 '15 at 13:32
  • that worked! thanks for that - I had to use some of @Kayasax answer and yours but finally got there in the end! Many thanks! – lara400 Apr 29 '15 at 14:03
2

Don't write directly to the registry unless you have to. Use the appropriate interfaces:

$DNSSuffixs = 'UK.COM', 'US.COM', 'AUS.COM', 'NL.COM'
Invoke-WmiMethod -Class Win32_NetworkAdapterConfiguration -Name setDNSSuffixSearchOrder `
  -ArgumentList @($DNSSuffixs),$null

Server 2012 even has a shiny new cmdlet:

Set-DnsClientGlobalSetting -SuffixSearchList @($DNSSuffixs)

Note that depending on whether you call the script from PowerShell or from somewhere else (e.g. CMD) the parameter value will be different. If you call it from PowerShell like this:

.\ip_assign.ps1 -DNSSuffixs UK.COM,US.COM,NL.COM

you get an array @('UK.COM', 'US.COM', 'NL.COM'). If you call it from CMD like this:

powershell -File .\ip_assign.ps1 -DNSSuffixs UK.COM,US.COM,NL.COM

you get a string 'UK.COM,US.COM,NL.COM'.

This can be handled for instance by splitting the value at commas if it's not of type array:

if (-not $DNSSuffixs -is [Array]) {
  $DNSSuffixs = $DNSSuffixs -split ','
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

just type $DNSsuffixs as array of strings : [string[]]$DNSSuffixs

function test{
param (

[Parameter(Mandatory=$true,
            Position = 1)]
            [string]$IP
,
[Parameter(Position = 2)]
[string]$SubnetMask = "none"
,
[Parameter(Position = 3)]
[string]$Gateway = "none" 
,
[Parameter(Position = 4)]
[string[]]$DNSServers
,
[Parameter(Position = 5)]
[string[]]$DNSDomain
,
[Parameter(Position = 6)]
[string[]]$DNSSuffixs
    )


$psboundparameters

}

test ip subnet gateway dns_servers dns_domain UK.COM,US.COM,NL.COM 

Outputs :

Key                                                                                    Value                        
---                                                                                    -----                        
IP                                                                                     ip                           
SubnetMask                                                                             subnet                       
Gateway                                                                                gateway                      
DNSServers                                                                             {dns_servers}                
DNSDomain                                                                              {dns_domain}                 
DNSSuffixs                                                                             {UK.COM, US.COM, NL.COM}     
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • did that before but that does not work. Still removes the commas and keeps one long string with spaces between them. – lara400 Apr 29 '15 at 13:06
  • your right...it does show that...but for some reason whenever I try that in my script it does not return it as expected...cannot for the life of me figure out why! The output in the registry is missing the commas... – lara400 Apr 29 '15 at 13:18
  • try to add `-force ` parameter to the last line of your script – Loïc MICHEL Apr 29 '15 at 13:34