登录时使用 VBScript 确定是否安装了 powershell

登录时使用 VBScript 确定是否安装了 powershell

我的网络上有 Win7 和 XP 机器。每个用户都使用基于 VBS 的登录脚本登录,对于支持该脚本的客户端,我想显示信息弹出窗口如下图所示

如何使用 VBScript 检测 Powershell 是否安装?

答案1

您可以使用类似下面的代码。它读取 PowerShell 的注册表项。如果读取成功(返回代码 0)或失败,您将收到相应的消息框,您可以将其切换为您需要执行的其他逻辑——例如,如果未检测到 PowerShell,则安装它。有关更多信息,请参阅下面的源链接。

Option Explicit
Dim oShell
Dim value

'If the key isn't there when we try to read it, an error will be generated
'that we will later test for, so we want to automatically resume execution.
On Error Resume Next

'#Try reading the registry value
Set oShell = CreateObject("WScript.Shell")
value = oShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\")

'Catch the error
If Err.Number = 0 Then
    'Error code 0 indicates success
    MsgBox(Err.Number & "PowerShell is installed.")
Else
    'Any other error code indicates failure
    MsgBox(Err.Number & "PowerShell is NOT installed.")
End If

VBScript 检查应用程序的注册表(例如 .NET): https://stackoverflow.com/questions/4394607/vbscript-to-check-if-net-2-0-is-installed

要检查 PowerShell 的注册表项: http://blogs.msdn.com/b/powershell/archive/2009/06/25/detection-logic-poweshell-installation.aspx

相关内容