无法索引到Null数组错误

时间:2017-07-23 23:47:00

标签: powershell

我正在尝试使用PowerShell脚本,但我遇到了错误。错误是,

Cannot index into a null array.
At C:\users\cody\desktop\test.ps1:18 char:97
+     $Profiles += (netsh wlan show profiles) | Select-String "\:(.+)$" | Foreach {$_.Matches.Groups[ <<<< 1].Value.Trim()} | Sort-Object
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

我不确定这个问题究竟是什么。

脚本是:

#Create QuestionBox
$title = "Reveal WiFi network password"
$message = "Do you want to reveal all or just one WiFi password stored on this computer?"
$All = New-Object System.Management.Automation.Host.ChoiceDescription "&All", "Reveal all stored WiFi Passwords"
$One = New-Object System.Management.Automation.Host.ChoiceDescription "&One", "Reveal only a selected stored WiFi Password"
$ShowWiFiProfiles = New-Object System.Management.Automation.Host.ChoiceDescription "&Show WiFi Profiles", "Only Display stored Wifi Profiles on this computer"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($All, $One, $ShowWiFiProfiles )
$result = $Host.UI.PromptForChoice($title, $message, $options, 0) 

#Get all WiFi Profiles
$Profiles = @()
$Profiles += (netsh wlan show profiles) | Select-String "\:(.+)$" | Foreach{$_.Matches.Groups[1].Value.Trim()} | Sort-Object

#Read out the user input
switch ($result) {
    0 {
        #The user selected All
        $Profiles | Foreach{$ProfileName = $_; (netsh wlan show profile name="$_" key=clear)} | `
            Select-String "Key Content\W+\:(.+)$" | `
            Foreach{$pass=$_.Matches.Groups[1].Value.Trim(); $_} | `
            Foreach{[PSCustomObject]@{ PROFILE_NAME=$ProfileName;PASSWORD=$pass }} | `
            Format-Table -AutoSize
    }
    1 {
        #The user selected One
        $Wifi2Reveal = Read-Host "Name of the WiFi profile you wish to reveal"
        (netsh wlan show profile name="$Wifi2Reveal" key=clear)| `
            Select-String "Key Content\W+\:(.+)$" | `
            Foreach{$pass=$_.Matches.Groups[1].Value.Trim(); $_} | `
            Foreach{[PSCustomObject]@{ PROFILE_NAME=$Wifi2Reveal;PASSWORD=$pass }} | `
            Format-Table -AutoSize 
    }
    2 {
        #The user selected Only Show Wifi profiles
        $Profiles 
    }
}

0 个答案:

没有答案