Hi All,
I was writing script to install and uninstall applications with winget. I have created hashtable to enter applications details so user can change applications as per requirments now i wanted to use switch statement for user selection but I am not able to retrive user selection into switch statement
here is my script
$apps= @{}
$apps.Add(1,@('Chrome','Google.Chrome'))
$apps.Add(2,@('Anydesk','AnyDeskSoftwareGmbH.AnyDesk'))
Clear-Host
$ex=0
while($ex -ne 20)
{
Write-Host "---------------Menu---------------------" -ForegroundColor Cyan
Write-Host "1.Install Apps" -ForegroundColor Cyan
Write-Host "2.Uninstall Apps" -ForegroundColor Cyan
Write-Host "3.Update All Apps" -ForegroundColor Cyan
Write-Host "20.Exit" -ForegroundColor Cyan
Write-Host "------------------------------------------" -ForegroundColor Cyan
switch($ex=Read-Host "Select a menu item"){
1{
$exsub=0
while ($exsub -ne 20)
{
Write-Host "---------------SubMenu---------------------"
for($i=1;$i -le $apps.Count; $i++)
{
Write-Host "$i Install "$apps[$i][0]
}
Write-Host "20.Exit"
Write-Host "------------------------------------------" -ForegroundColor DarkGray
switch($exsub=Read-Host "Select a sub-menu item"){
$exsub{
if($exsub -eq 20)
{
Write-Host "Ending" -ForegroundColor DarkGray
}
else {
Write-host "User Selected"$exsub
Write-Host $apps[1][1]
Write-Host "user choice was " $apps[$exsub][1]
}
}
default {Write-Host "Invalid entry" -ForegroundColor DarkGray}
}
}
}
}
}
And here error
Select a sub-menu item: 20
---------------Menu---------------------
1.Install Apps
2.Uninstall Apps
3.Update All Apps
20.Exit
------------------------------------------
Select a menu item: 1
---------------SubMenu---------------------
1 Install Chrome
2 Install Anydesk
20.Exit
------------------------------------------
Select a sub-menu item: 1
User Selected 1
Google.Chrome
InvalidOperation: C:\Users\Ramakant\Desktop\Ramakant\powershell\temp2.ps1:36:29
Line |
36 | … Write-Host "user choice was " $apps[$exsub][1]
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot index into a null array.
---------------SubMenu---------------------
1 Install Chrome
2 Install Anydesk
20.Exit
------------------------------------------
Select a sub-menu item: 20
Ending
Thank You in Advance.