Search code examples
arrayspowershellstring-interpolation

Basic Programming For Each Loop


I have been breaking my head long enough to do this extremely simple thing but am unable to figure out how. I am trying to work this out in 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 result I get is

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]

However, the format I am keen on is as below:

Incident ID 1: - INC0010164
SystemID 1: - d80fd7e2dbab4300479a7e7dbf96195d

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

Solution

  • 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.