简体   繁体   English

我如何分割参数

[英]how do I split a parameter

Writing a script to put DNS servers that are inputted as parameter to the primary network card. 编写脚本以将作为参数输入的DNS服务器放置到主网卡中。

The servers are inputted as: 1.1.1.1,2.2.2.2,3.3.3.3 (sometimes there are not three servers but more/less). 服务器输入为: 1.1.1.1,2.2.2.2,3.3.3.3 (有时没有三台服务器,但更多/更少)。

I want to split on the comma "," - how can I do this? 我想用逗号“,”分开-我该怎么做?

I tried this but Powershell is complaining: 我尝试了这个,但是Powershell抱怨:

[Parameter(Position = 4)]
$a = $DNSServers
$a.Split(',')

When the script is run the forth parameter is DNS Servers which are split via a comma. 运行脚本时,第四个参数是DNS服务器,它们通过逗号分开。

UPDATE UPDATE

I will try Matt's suggestion...more infoL 我会尝试马特的建议...更多infoL

didn't think I would get this much response. 没想到我会得到这么多回应。 Apologies, due to security restrictions at my place I cannot copy/paste the code that is on a server (so pain the backside in typing it all again). 抱歉,由于我所处的安全限制,我无法复制/粘贴服务器上的代码(因此,在重新键入所有内容时会感到痛苦)。 So, decided to copy only the bit that I needed - I thought that would be enough (obviously not!). 因此,决定只复制我需要的部分-我认为就足够了(显然不行!)。

The script would be run as: 该脚本将运行为:

script.ps1 IP_ADDRESS SUBNET_MASK GATEWAY DNS_SERVER script.ps1 IP_ADDRESS SUBNET_MASK网关DNS_SERVER

ie: script.ps1 10.1.1.1 255.255.255.0 10.1.1.254 1.1.1.1,2.2.2.2,3.3.3.3 即:script.ps1 10.1.1.1 255.255.255.0 10.1.1.254 1.1.1.1,2.2.2.2,3.3.3.3

So the forth parameter that will be called is DNS Server (there can be multiple DNS Servers). 因此,将要调用的第四个参数是DNS服务器(可以有多个DNS服务器)。 The parameter is pulled in from an external web client that is used where the person enters the DNS Servers - but is is generally of 3 IP addresses. 该参数是从外部Web客户端提取的,该客户端在此人输入DNS服务器时使用该外部Web客户端-但通常具有3个IP地址。

oh, and the error is this - I am not too sure where it is missing the ')' 哦,这是错误的-我不太确定它在哪里缺少')'

PS C:\Temp> C:\Temp\ip_assign.ps1
At C:\Temp\ip_assign.ps1:14 char:17
+ $a = $DNSServers
+                 ~
Missing ')' in function parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInFunctionParameterList

Managed to get the main parts then of what I am doing - hope this is enough: 设法掌握了我正在做的事情的主要部分-希望这足够了:

param (

[Parameter(Mandatory=$true,
            Position = 1)]
            [string]$IP
,
[Parameter(Position = 2)]
[string]$SubnetMask = "none"
,
[Parameter(Position = 3)]
[string]$Gateway = "none" 
,
[Parameter(Position = 4)]
$a = $DNSServers
$a.Split(',')


$TeamAdaptor = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.Caption -ilike '*Virtual*'}
$TeamAdaptor.EnableStatic($IP,$SubnetMask)
$TeamAdaptor.SetGateways($Gateway)
$TeamAdaptor.SetDNSServerSearchOrder("$DNSServers")

I think the issue here is that you are not passing a string to the parameter like you think you are. 我认为这里的问题是您没有像您认为的那样将字符串传递给参数。 Consider the following function. 考虑以下功能。

function Get-Bagel{
param(
    $DnsServers
)
    $DnsServers.GetType().FullName
    $DnsServers
}

And then the function call 然后函数调用

Get-Bagel 1.1.1.1,2.2.2.2,3.3.3.3

This will net the following output. 这将得到以下输出。

System.Object[]
1.1.1.1
2.2.2.2
3.3.3.3

Since we used array notation for inputting the variable and you didn't set a cast in the declaration $DnsServers is actually a string array. 由于我们使用数组符号来输入变量,并且您未在声明$DnsServers设置$DnsServers类型转换实际上是一个字符串数组。 This might be what you wanted in the first place so there might not be a need to use .split() . 首先可能就是您想要的,所以可能不需要使用.split()

Your Funny Error 您的有趣错误

You are missing a bracket for param() 您缺少param()的括号

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
)

Cast to [string[]]$DNSServers and then there is no need for splits. 强制转换为[string[]]$DNSServers ,然后无需拆分。

You need to have an array variable, and then use the split function on the input string to split the contents into the array: 您需要具有一个数组变量,然后对输入字符串使用split函数将内容拆分为数组:

$a = "1.1.1.1,2.2.2.2,3.3.3.3" [array]$DnsServers = $a -split(",")

This will give you an array, $DnsServers, containing the ip addresses. 这将为您提供一个包含IP地址的数组$ DnsServers。 In this particular case, $DnsServers[0] is 1.1.1.1, $DnsServers[1] is 2.2.2.2, and $DnsServers[2] is 3.3.3.3. 在这种情况下,$ DnsServers [0]为1.1.1.1,$ DnsServers [1]为2.2.2.2,$ DnsServers [2]为3.3.3.3。

I think Matt gave a solid answer, but to be a little more concise here: when you specify a comma separated list of parameters, like so: 我认为Matt给出了一个可靠的答案,但在这里要更加简洁:当您指定以逗号分隔的参数列表时,如下所示:

Get-Bagel Aaaaa,Bbbb,Cccc

PowerShell interprets this as three separate items, Aaaa,Bbbb and Cccc. PowerShell将其解释为三个单独的项目,即Aaaa,Bbbb和Cccc。 There's no need to separate on the comma, as PowerShell automatically does it for you 无需在逗号上分开,因为PowerShell会自动为您完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM