powershell - how can I keep commas? -
trying set dns suffixes entered parameter script. found there registry key - hklm:\system\currentcontrolset\services\tcpip\parameters\searchlist
, can set them in there but, parameters passed uk.com,us.com,aus.com,nl.com
.
the problem when @ registry key removing each comma between domains.
i found fix here: powershell removing comma program argument
however, answer states put paramters in quotes - problem parameter being read external system (which cannot change) , line gets this:
<param><paramvalue>uk.com,us.com,nl.com</paramvalue></param><param>
so, how can keep comma when being passed in?
the input being entered (getting rid of ip's , leaving 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 @ 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
use single quotes around items, because otherwise powershell interprets commas separating entries. added write-host line debug, , see
#write-host "received $($dnssuffixs.count) `$dnssuffix, $dnssuffixs" .\ip_assign.ps1 ip subnet gateway dns_servers dns_domain 'uk.com,us.com,nl.com' pri_wins sec_wins received 1, uk.com,us.com,nl.com
since cannot ammend parameters because of outside tool
you've mentioned have no control on way outside tool invokes script. still allow pass commas, can use -join operator rejoin 3 items you're passing , make them 1 item, happens have commas in it. add line 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 resolve issue.
Comments
Post a Comment