【发布时间】:2018-08-30 02:13:56
【问题描述】:
stackoverflow 的新手。多年来一直是潜伏者,但现在需要帮助。我正在为 MSP 编写一个简单的脚本,以检测一组基本应用程序,如果未安装,则为该应用程序运行巧克力安装命令。当我以管理员身份在 ISE 中运行它时,即使它不是真的,它也会报告所有变量值的真值。如果我只运行测试路径并告诉它写入主机它的值是正确的。但是,当全部一起运行时,它总是会返回 True。有人可以帮我调查一下吗?
#Are programs installed?
$a = Test-Path "C:\Program Files\7-Zip" IF ($a = "True") {Write-Host "7-zip is installed"} ELSE {Write-Host "7-zip is not installed, install command sent" choco install 7zip -y}
$b = Test-Path "C:\Program Files\Mozilla Firefox" IF ($b = "True") {Write-Host "Firefox is installed"} ELSE {Write-Host "Firefox is not installed, install command sent"}
$c = Test-Path "C:\Program Files (x86)\Adobe\Acrobat Reader DC" IF ($c = "True") {Write-Host "Adobe Reader is installed"} ELSE {Write-Host "Adobe Reader is not installed, install command sent"}
$d = Test-Path "C:\Program Files (x86)\Google\Chrome" IF ($d = "True") {Write-Host "Chrome is installed"} ELSE {Write-Host "Chrome is not installed, install command sent"}
【问题讨论】:
-
= 是 PowerShell 的赋值运算符。使用 -eq 检查是否相等。
-
另外,$true 是布尔真值,而不是“真”。
标签: powershell variables