2015-02-09 172 views
0

我有以下代碼,我需要使用它來在標準和高對比度之間切換Windows 7主題。 IF運行良好,但不會運行ELSE條件,有人可以指出我的方式錯誤嗎?Powershell IF ELSE不會運行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" 
     } 

回答

5

您正在分配註冊表中的當前主題。

您需要使用-eq進行平等比較。 =是powershell中的賦值運算符。

List of powershell operators

具體會發生什麼事是你的代碼是第一次分配值.../hcblack.themeCurrentTheme,然後在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>") { ... } 
+0

那現在是爲我工作。非常感謝你。 – jemiss 2015-02-11 09:18:54