每个循环的基本编程

时间:2018-01-12 14:17:13

标签: arrays powershell string-interpolation

我一直在打破这个非常简单的事情,但我无法弄明白。我试图在PowerShell中解决这个问题。

$IncidentID="INC0010164~INC0010165"
$arrIncidentID = $IncidentID -split '~'
$SysID = "d80fd7e2dbab4300479a7e7dbf96195d~7f0f97e2dbab4300479a7e7dbf9619ec"
$arrSystemID = $SysID -split '~'

if ($arrIncidentID.Length -eq $arrSystemID.Length) {
    for ($i=0; $i -lt $arrIncidentID.length; $i++) {
        "The Incident ID is $arrIncidentID[$i] "
        "The URL is $arrSystemID[$i]"
    }
}

我得到的结果是

The Incident ID is INC0010164 INC0010165[0] 
The URL is d80fd7e2dbab4300479a7e7dbf96195d 7f0f97e2dbab4300479a7e7dbf9619ec[0]
The Incident ID is INC0010164 INC0010165[1] 
The URL is d80fd7e2dbab4300479a7e7dbf96195d 7f0f97e2dbab4300479a7e7dbf9619ec[1]

但是,我热衷的格式如下:

Incident ID 1: - INC0010164
SystemID 1: - d80fd7e2dbab4300479a7e7dbf96195d

Incident ID 2: - INC0010165 
SystemID 2: - 7f0f97e2dbab4300479a7e7dbf9619ec

3 个答案:

答案 0 :(得分:1)

This answer is intended to get your code functional and help you understand why it isn't working.

Understanding the error

Using a variable in a string and having it replace by its value is called string interpolation. In PowerShell, interpolating variables themselves is easy. You can also interpolate statements, but you need a little different syntax (which is $()).

$IncidentID="INC0010164~INC0010165"
$arrIncidentID = $IncidentID -split '~'
$SysID = "d80fd7e2dbab4300479a7e7dbf96195d~7f0f97e2dbab4300479a7e7dbf9619ec"
$arrSystemID = $SysID -split '~'

if ($arrIncidentID.Length -eq $arrSystemID.Length) {
    for ($i=0; $i -lt $arrIncidentID.length; $i++) {
        "The Incident ID is $($arrIncidentID[$i]) "
        "The URL is $($arrSystemID[$i])"
    }
}

The new result is:

The Incident ID is INC0010164
The URL is d80fd7e2dbab4300479a7e7dbf96195d
The Incident ID is INC0010165
The URL is 7f0f97e2dbab4300479a7e7dbf9619ec

Better output

So to get the final output you wanted, you could slightly adjust your code to:

$IncidentID="INC0010164~INC0010165"
$arrIncidentID = $IncidentID -split '~'
$SysID = "d80fd7e2dbab4300479a7e7dbf96195d~7f0f97e2dbab4300479a7e7dbf9619ec"
$arrSystemID = $SysID -split '~'

if ($arrIncidentID.Length -eq $arrSystemID.Length) {
    for ($i=0; $i -lt $arrIncidentID.length; $i++) {
        "Incident ID $($i+1): - $($arrIncidentID[$i]) "
        "SystemID $($i+1): - $($arrSystemID[$i])"
    }
}
Incident ID 1: - INC0010164
SystemID 1: - d80fd7e2dbab4300479a7e7dbf96195d

Incident ID 2: - INC0010165 
SystemID 2: - 7f0f97e2dbab4300479a7e7dbf9619ec

The PowerShell Way

You should consider doing this in a more PowerShell way to make it easier to reuse the output:

$IncidentID="INC0010164~INC0010165"
$arrIncidentID = $IncidentID -split '~'
$SysID = "d80fd7e2dbab4300479a7e7dbf96195d~7f0f97e2dbab4300479a7e7dbf9619ec"
$arrSystemID = $SysID -split '~'

if ($arrIncidentID.Length -eq $arrSystemID.Length) {
    for ($i=0; $i -lt $arrIncidentID.length; $i++) {
        New-Object PSObject -Property @{
            IncidentID = $arrIncidentID[$i]
            SystemID = $arrSystemID[$i]
        }
    }
}
IncidentID SystemID
---------- --------
INC0010164 d80fd7e2dbab4300479a7e7dbf96195d
INC0010165 7f0f97e2dbab4300479a7e7dbf9619ec

These are now objects that you can reference by property names.

答案 1 :(得分:0)

$IncidentID="INC0010164~INC0010165"
$arr = $IncidentID.Split("~")
foreach($val in $arr) {echo $val}
INC0010164
INC0010165

像魅力一样工作?

答案 2 :(得分:0)

考虑使用format operator-f):

for ($i=0; $i -lt $arrIncidentID.Length; $i++) {
    "Incident ID {0}: - {1}`nSystemID {0}: - {2}" -f ($i+1), $arrIncidentID[$i], $arrSystemID[$i]
}