添加字符串的子字符串 IP 地址会生成错误但有效
Substring IP-Address with adding a string generates error but works
我有以下命令:
$IP_start = $First.Substring(0, $First.LastIndexOf('.'))+ ".200"
$First
是一个 IP 地址,例如 192.168.0.1
我想把第四个八位字节的1改成200
Write-Output $IP_start
给我正确的 IP 地址 192.168.0.200,但同时我得到以下错误:
Ausnahme beim Aufrufen von "Substring" mit 2 Argument(en): "Die Länge
darf nicht kleiner als 0 (null) sein. Parametername: length" In
*ps1:31 Zeichen:3
+ $IP_start = $First.Substring(0, $First.LastIndexOf('.'))+ ".200"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException
英文翻译
Exception when calling "substring" with 2 arguments: "The length can not be less than zero. Parameter name: length" In * ps1: 31 characters: 3...
我认为一切正常,但错误消息让我很困扰。
//编辑:
有一个 ip.txt,其中每一行都像“192.168.0.1; ABCDEF”
$txt = Get-Content ip.txt
$editline = foreach ($Data in $txt) {
$First, $Second = $Data -split ';' -replace '^\s*|\s*$'
$IP_start = $First.Substring(0, $First.LastIndexOf('.'))+ ".200"
Write-Output "modify ipaddr_first $IP_start"
}
$editline | Out-File "$output"
$first 因此是“192.168.0.1”,$second 是 "ABCDEF".
您可以使用正则表达式
$IP_start = "192.168.1.1"
$IP_start -replace "\d{1,3}$","200"
这会将任何 192.168.1.xxx
更改为 192.168.1.200
$IP_start = "192.168.1.1"
(($IP_start -split "\.")[0..2] -join ".") + ".200"
为 IP 地址对象创建综合方法,这是一种有趣的方式...
$Method = {
Param(
[Int]$Number,
[ValidateSet(1,2,3,4)]
[Int]$Position
)
$OctetPosition = $Position - 1
$CurrentIpAddress = New-Object -TypeName System.Collections.ArrayList
$This.IpAddressToString -Split '\.' | ForEach-Object -Process {
$CurrentIpAddress.Add($_) | Out-Null
}
$ChangedOctet = [Int]($CurrentIpAddress[$OctetPosition]) + $Number
if( $ChangedOctet -gt 255 ){
throw "Resulting octet is $ChangedOctet which is greater than 255"
}
$CurrentIpAddress.Item($OctetPosition) = $ChangedOctet
return ($CurrentIpAddress -join '.')
}
$IPAddress = [System.Net.IpAddress]'192.168.1.1'
$IPAddress | Add-Member -MemberType ScriptMethod -Name Add -Value $Method
#$IPaddress.Add($Number,$OctetPosition)
$IPAddress.Add(200,4)
我有以下命令:
$IP_start = $First.Substring(0, $First.LastIndexOf('.'))+ ".200"
$First
是一个 IP 地址,例如 192.168.0.1
我想把第四个八位字节的1改成200
Write-Output $IP_start
给我正确的 IP 地址 192.168.0.200,但同时我得到以下错误:
Ausnahme beim Aufrufen von "Substring" mit 2 Argument(en): "Die Länge darf nicht kleiner als 0 (null) sein. Parametername: length" In *ps1:31 Zeichen:3 + $IP_start = $First.Substring(0, $First.LastIndexOf('.'))+ ".200" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentOutOfRangeException
英文翻译
Exception when calling "substring" with 2 arguments: "The length can not be less than zero. Parameter name: length" In * ps1: 31 characters: 3...
我认为一切正常,但错误消息让我很困扰。
//编辑:
有一个 ip.txt,其中每一行都像“192.168.0.1; ABCDEF”
$txt = Get-Content ip.txt
$editline = foreach ($Data in $txt) {
$First, $Second = $Data -split ';' -replace '^\s*|\s*$'
$IP_start = $First.Substring(0, $First.LastIndexOf('.'))+ ".200"
Write-Output "modify ipaddr_first $IP_start"
}
$editline | Out-File "$output"
$first 因此是“192.168.0.1”,$second 是 "ABCDEF".
您可以使用正则表达式
$IP_start = "192.168.1.1"
$IP_start -replace "\d{1,3}$","200"
这会将任何 192.168.1.xxx
更改为 192.168.1.200
$IP_start = "192.168.1.1"
(($IP_start -split "\.")[0..2] -join ".") + ".200"
为 IP 地址对象创建综合方法,这是一种有趣的方式...
$Method = {
Param(
[Int]$Number,
[ValidateSet(1,2,3,4)]
[Int]$Position
)
$OctetPosition = $Position - 1
$CurrentIpAddress = New-Object -TypeName System.Collections.ArrayList
$This.IpAddressToString -Split '\.' | ForEach-Object -Process {
$CurrentIpAddress.Add($_) | Out-Null
}
$ChangedOctet = [Int]($CurrentIpAddress[$OctetPosition]) + $Number
if( $ChangedOctet -gt 255 ){
throw "Resulting octet is $ChangedOctet which is greater than 255"
}
$CurrentIpAddress.Item($OctetPosition) = $ChangedOctet
return ($CurrentIpAddress -join '.')
}
$IPAddress = [System.Net.IpAddress]'192.168.1.1'
$IPAddress | Add-Member -MemberType ScriptMethod -Name Add -Value $Method
#$IPaddress.Add($Number,$OctetPosition)
$IPAddress.Add(200,4)