2016-06-16 38 views
1

我製作了一個PowerShell腳本來查找網絡密碼,但是它在打開後立即關閉。我嘗試使用Read-Host命令,但它不起作用。這裏是我的腳本:腳本在立即運行後關閉,並且Read-Host -Prompt不起作用

$Output = netsh wlan show profiles name="enter network here" key=clear 
$SSIDSearchResults = $Output | Select-String -Pattern 'SSID Name' 
$ProfileName = ($SSIDSearchResults -split ":")[-1].Trim() -replace '"' 
$PwSearchResult = $Output | Select-String 'Key Content' 
$PW = ($PwSearchResult -split ":")[-1].Trim() -replace '"' 
[pscustomobject] @{ 
     WifiProfileName = $ProfileName 
     Password = $PW 

} 
+0

嗨,你使用ISE編寫PowerShell代碼?點擊**開始**,輸入** ise **並點擊**輸入**。 – sodawillow

+0

是的,我使用PowerShell ISE。我的所有問題都已得到解決 –

+0

將問題標記爲已回答的方式是使用可幫助您的答案旁邊的複選標記。 – Matt

回答

1

如果你打電話從其他位置PowerShell中您可以使用-noexit保持它至少關閉調試。

僅略微無關你的問題,這裏有一個簡單的方法,你可以把它變成一個功能

function Show-WifiPassword ($network) { 
    $Output = netsh wlan show profiles name="$network" key=clear 
    $SSIDSearchResults = $Output | Select-String -Pattern 'SSID Name' 
    $ProfileName = ($SSIDSearchResults -split ":")[-1].Trim() -replace '"' 
    $PwSearchResult = $Output | Select-String 'Key Content' 
    $PW = ($PwSearchResult -split ':')[-1].Trim() -replace '"' 
    [pscustomobject] @{ 
     WifiProfileName = $ProfileName 
     Password = $PW 
    } 
} 

,這裏是我使用的一個。誠然,它可以更具可讀性,但我喜歡它是一個班輪

function Show-WifiPasswords { 
    netsh wlan show profiles | ? {$_ -match ' : '} | % {$_.split(':')[1].trim()} | % {$n = $_; netsh wlan show profile name="$_" key=clear} | ? {$_ -match 'key content'} | select @{n='Network';e={$n}}, @{n='Key';e={$_.split(':')[1].trim()}} 
} 
+0

謝謝你使用作品的版本,但是上面的版本不能。反正我更喜歡單線班車 –

+0

我遇到了一個小問題。看起來,對於11個字符以上的網絡密鑰,它不顯示完整的密鑰,有沒有辦法繞過這個? –

+0

您可能必須自行運行netsh命令才能看到它是否是該程序的限制 –