1

I have created the below script which takes values assigned to an array $PerfList and displays them using Out-Gridview. Once the selection is made it should pass the selection to $Server, but doesn't. I get the following error:

Index operation failed; the array index evaluated to null. At C:\CreateStart.ps1:7 char:21 + foreach { $PerfList[$_.IDX] }) + ~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArrayIndex

My code is as follows:

$PerfList = @("Primary", "SQL", "APE", "Netflow", "AWE")
$IDX = 0
$Server = ($(foreach ($item in $PerfList){
          $item | select @{l='#';e={$IDX}},@{l='Name';e={$PerfList[$IDX]}}
          $IDX++}) | 
          Out-Gridview -Title 'What server is this?' -OutputMode Single |
          foreach { $PerfList[$_.IDX] })

$TaskName = Switch ($Server)
                {
                    'Primary' {'SolarWinds_App'}
                    'SQL' {'SolarWinds_SQL'}
                    'APE' {'SolarWinds_APE'}
                    'Netflow' {'SolarWinds_Netflow'}
                    'AWE' {'SolarWinds_AWE'}
                }

Switch ($Server) {
    'Primary' {
    logman import $TaskName -xml "Primary.xml" -y
    logman start $TaskName
    }

    'SQL' {
    logman import $TaskName -xml "SQL.xml" -y
    logman start $TaskName
    }

    'APE' {
    logman import $TaskName -xml "APE.xml" -y
    logman start $TaskName
    }

    'Netflow' {
    logman import $TaskName -xml "Netflow.xml" -y
    logman start $TaskName
    }

    'AWE' {
    logman import $TaskName -xml "AWE.xml" -y
    logman start $TaskName
    }
}

Any help would be appreciated :)

Dax
  • 35
  • 6

1 Answers1

2

Just goes to show the importance of indentation and keeping things tidy.

First of all $IDX++ is within your Select statement, so that should be erroring.

Secondly you can't pipe from a Foreach (x in y) { } statement.

Thirdly you're not adding a property to the Item called "IDE", it's called "#" so you need to reference by that:

Also, more semantics than anything but I prefer Select when getting a specific property from an object.

This should do the trick:

$PerfList = @("Primary", "SQL", "APE", "Netflow", "AWE")
$IDX = 0
$Server = foreach ($item in $PerfList)
{
    $item | select @{ l='#';e={ $IDX } },@{ l='Name';e={ $PerfList[$IDX] } }
    $IDX++
}

$Server = $Server | Out-Gridview -Title 'What server is this?' -OutputMode Single | 
    Select -ExpandProperty "Name"
Deadly-Bagel
  • 1,612
  • 1
  • 9
  • 21
  • Thank you so much for this, it is definitely passing the selection over now without error, only thing I did was change the Select to take 'Name' and not '#' However, now even though there is no error, after selecting an item and pressing OK, it displays what I selected but doesn't move further into the script. It won't perform the steps below the code you changed – Dax Jun 15 '17 at 10:09
  • Yeeah because the value is being sent to the console instead of the $Server variable you tried to in your original script. Modified the answer. – Deadly-Bagel Jun 15 '17 at 10:19
  • I'm still really new at Powershell (and coding as a whole) so thank you! it's working 100% now – Dax Jun 15 '17 at 10:27
  • No worries, I highly recommend watching the MVA vids on Powershell https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276?l=r54IrOWy_2304984382 It's lengthy but extremely helpful and interesting. – Deadly-Bagel Jun 15 '17 at 10:31