2017-10-09 43 views
1

我需要告訴用戶他的操作系統版本(Windows 10 Home,Windows 7 Home等)。我用這個代碼:如何找到用戶的操作系統版本並通過彈出窗口向用戶說明?

$WIN7H = "Microsoft Windows 7 Home" 
$WIN7U = "Microsoft Windows 7 Ultimate" 
$WIN7P = "Microsoft Windows 7 Professional" 
$WIN7E = "Microsoft Windows 7 Enterpise" 
$WIN10H = "Microsoft Windows 10 Home" 

If ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H) { 

$wshell = New-Object -ComObject Wscript.Shell 
$wshell.Popup("This is Windows 10 Home",0,"Windows 10",0) 

}else if ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H) { 

$wshell = New-Object -ComObject Wscript.Shell 
$wshell.Popup("This is Windows 7 Home",0,"Windows 7",0) 

} 

找到並說明用戶的操作系統版本,但我得到以下錯誤在PowerShell中:

At line:7 char:60 
+ ... If ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H 
... 
+                ~~~ 
Unexpected token '-eg' in expression or statement. 
At line:7 char:64 
+ ... ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H) { 
+                ~~~~~~~ 
Unexpected token '$WIN10H' in expression or statement. 
At line:7 char:64 
+ ... ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H) { 
+                ~~~~~~~ 
Missing closing ')' after expression in 'If' statement. 
At line:7 char:71 
+ ... ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN10H) { 
+                  ~ 
Unexpected token ')' in expression or statement. 
At line:12 char:2 
+ }else if ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $W ... 
+ ~~~~ 
Unexpected token 'else' in expression or statement. 
At line:12 char:64 
+ ... if ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H) 
... 
+                ~~~ 
Unexpected token '-eg' in expression or statement. 
At line:12 char:68 
+ ... f ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H) { 
+                 ~~~~~~ 
Unexpected token '$WIN7H' in expression or statement. 
At line:12 char:68 
+ ... f ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H) { 
+                 ~~~~~~ 
Missing closing ')' after expression in 'if' statement. 
At line:12 char:74 
+ ... f ((Get-WmiObject -class Win32_OperatingSystem).Caption -eg $WIN7H) { 
+                  ~ 
Unexpected token ')' in expression or statement. 
+ CategoryInfo   : ParserError: (:) [], 
ParentContainsErrorRecordException 
+ FullyQualifiedErrorId : UnexpectedToken 

基本上,所有我需要的是一個腳本能夠找到用戶的操作系統版本。更詳細的,應該是這樣的:

if ("The user is running windows 10") { 

    ....something here.... 

}else if("He is running windows 7"){ 

    Then show a popup that "You are running Windows 7, you need Windows 10" 
    (or something like that...) 
} 

你可以在我的笑,因爲它是壞的編程技巧寫的,但我只是在PowerShell中一個初學者。對不起:)

任何幫助將不勝感激! 謝謝!

- MS

回答

1

應當-eq [-EQ]而不是-eg

但是,如果你需要的是一個彈出發送給用戶如果沒有贏得10個主場,這都是你的腳本需要做的事情:

$OS = (Get-WmiObject -class Win32_OperatingSystem).caption 
if ($OS -ne "Microsoft Windows 10 Home") 
{ 
    $wshell = New-Object -ComObject Wscript.Shell 
    $wshell.Popup("This is $OS. You need Windows 10 Home",0,"Windows 10 Notification",0) 
} 
+0

它有助於記住它是'-eq'手段「等於。」 – Windos

+0

好了,經過我寫了'$ OS =(GET-WmiObject可以-class Win32_OperatingSystem).caption 如果($ OS當量 「的Microsoft Windows 10 *」) {$ = wshell新對象-ComObject Wscript.Shell $ answer = $ wshell.Popup(「此計算機當前正在運行$ OS。是否繼續使用該腳本?」,0,「Windows 10 Notification」,0x4) if($ answer -eq 7){exit }'提示用戶繼續腳本(如果他點擊「否」,它將終止),我繼續執行腳本(如果他點擊「是」,應執行該腳本),然後我完成...看下一個評論 –

+0

...續。我用'else完成了$ wshell = New-Object -ComObject Wscript.Shell $ wshell.Popup(「你需要一個Windows 10操作系統來運行這個程序,你使用的是$ OS。」,0,「警報「,16) }'但我在Windows 7系統上測試過它,但是提示」您想繼續使用腳本嗎?「仍然出現。但是,如果'$ OS -ne「Microsoft Windows 10 *」'只能彈出該消息,但那是Windows 7系統。有任何想法嗎? –

相關問題