通过交换机访问网页的脚本不起作用?

时间:2015-12-07 02:48:46

标签: powershell if-statement switch-statement conditional-statements

我试图在PowerShell上创建一个脚本,它询问我是否要访问3个网站的列表,然后询问我想使用哪种浏览器,无论是谷歌浏览器还是IE浏览器。

我知道我将使用的脚本,即

[System.Diagnostics.Process]::Start("chrome.exe","http://www.google.com")
IE部分的

iexplore.exe。但它最终给我一个错误,说索引丢失,并有一个意外的令牌。我在这里做错了什么?

以下是我的代码的一部分:

#Now I added menu 2 to the script by using a switch
Switch ($xMenu1){    #Second Menu appears
    1 {
        while ( $xMenu2 -lt 1 -or $xMenu2 -gt 4 ){
            CLS
            # Menu option appears
            Write-Host "`t Select the browser you would like to open this website with" -Fore Cyan
            Write-Host "`t 1. Google Chrome" -Fore Cyan
            Write-Host "`t 2. IE" -Fore Cyan
            Write-Host "`t 3. Go to Main Menu" -Fore Cyan
            [int]$xMenu2 = Read-Host "`t`tEnter Menu Option Number"
            #The start-Sleep indicates how long the error message starts and disappears 
            if( $xMenu2 -lt 1 -or $xMenu2 -gt 4 ){
                Write-Host "'t error, the number you entered was not part of the menu" -Fore Red;start-Sleep -Seconds 3
            }
        }
        Switch ($xMenu2){
            1{  ""[System.Diagnostics.Process]::Start("chrome.exe","http://www.google.com")"" }
            2{  "[System.Diagnostics.Process]::Start("iexplore.exe","http://www.google.com")" }
        }

1 个答案:

答案 0 :(得分:1)

你的引言搞砸了:

Switch ($xMenu2){
    1{  ""[System.Diagnostics.Process]::Start("chrome.exe","http://www.google.com")"" }
    2{  "[System.Diagnostics.Process]::Start("iexplore.exe","http://www.google.com")" }
}

您尝试调用System.Diagnostics.Process类的静态方法,因此您不能首先将表达式放在引号中:

Switch ($xMenu2){
    1{  [System.Diagnostics.Process]::Start("chrome.exe","http://www.google.com") }
    2{  [System.Diagnostics.Process]::Start("iexplore.exe","http://www.google.com") }
}

另外,如果由于某种原因你必须在字符串中使用嵌套的双引号,你必须将它们转义("...`"...")或使用单引号作为外引号('..."...')。

相关问题