Powershell如果ELSE不会运行ELSE语句

时间:2015-02-09 08:12:49

标签: powershell

我需要使用以下代码在标准和高对比度之间切换Windows 7主题。 IF运行良好但不会运行ELSE条件,有人可以指出我的方式错误吗?

IF     ((Get-ItemProperty -path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes").CurrentTheme = "%SystemRoot%\resources\Ease of Access Themes\hcblack.theme") 
       {
       C:\scripts\Themetool.exe changetheme "c:\Windows\resources\Themes\MyTheme.theme"
       } 
ELSE   {
       C:\scripts\Themetool.exe changetheme "C:\Windows\resources\Ease of Access Themes\hcblack.theme"
       }

1 个答案:

答案 0 :(得分:5)

您正在注册表中分配当前主题。

您需要使用-eq进行相等性比较。 =是powershell中的assign运算符。

List of powershell operators

详细情况是,您的代码首先将值.../hcblack.theme分配给CurrentTheme,然后将此值用于if语句中的布尔条件。 PowerShell将非空字符串视为$true。您可以自己尝试:!!"" -eq $false。这就是if部分匹配的原因。

你在做什么可以写成:

$prop = Get-ItemProperty -path HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes"
$prop.CurrentTheme = "%SystemRoot%\resources\Ease of Access Themes\hcblack.theme"
if ($prop.CurrentTheme) { ... }

你应该做什么:

if ((Get-ItemProperty -path "<path>").CurrentTheme -eq "<value>") { ... } 
相关问题