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 :)
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"