如何找到用户的操作系统版本并通过弹出窗口向他们说明?

时间:2017-10-09 18:39:29

标签: powershell

我需要告诉用户他的操作系统版本(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 个答案:

答案 0 :(得分:1)

应该是-eq [-EQ]而不是-eg

但是,如果你需要的只是向用户发送弹出窗口,如果它不是Win 10 home,那么这就是你需要做的所有脚本:

$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)
}