Powershell 如何找到用户的操作系统版本并通过弹出窗口向他们陈述?
我需要告诉用户他的操作系统版本(Windows 10 Home、Windows 7 Home等)。我使用以下代码:Powershell 如何找到用户的操作系统版本并通过弹出窗口向他们陈述?,powershell,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
$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应该是
-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)
}
这有助于记住
-eq
的意思是“相等”。在我写了$OS=(Get-WmiObject-class Win32_OperatingSystem)之后,说明if($OS-eq“Microsoft Windows 10*”){$wshell=New Object-ComObject Wscript.Shell$answer=$wshell.Popup(“这台计算机当前正在运行$OS。您想继续脚本吗?”,0,“Windows 10通知”,0x4)如果($answer-eq 7){exit}
提示用户继续脚本(如果他单击“否”,它将终止),则我继续使用脚本(如果他单击“是”,则应执行该脚本),我以…结束,请参见下一个注释…继续。我以结束{$wshell=New Object-ComObject-Wscript.Shell$wshell.Popup(“您需要一个Windows 10操作系统来运行此程序。您正在使用$OS.”,0,“警报”,16)}
但是我在Windows 7系统上测试了它,但是仍然会出现提示“您想继续使用脚本吗?”的提示。但是它应该只在$OS-ne时才会弹出该消息“Microsoft Windows 10*”
,但那是Windows 7系统。有什么想法吗?当您使用-eq
时,您不能使用通配符*
。现在,所有内容都将通过您的条件测试。请尝试将-Like
与通配符一起使用,并进行测试,以确保其性能符合预期。那么,对于任何版本的Windows 10,我将如何做到这一点?