Reputation: 87
I was trying add to the context menu (for directories) the option "Open Sublime Project" and the icon of Sublime Text Editor 2.
Well, the problem is that if I execute the 2 commands, the execution is perfect, but if I execute the script, only is executed the first command.
Output: No errors
If ( -NOT(
([bool](New-Item -Path "HKLM:\Software\Classes\Directory\shell\Open Sublime Project\command" -Value "`"C:\Program Files\Sublime Text 2\sublime_text.exe`" --new-window %1" -Force)) -OR
([bool](New-ItemProperty -Path "HKLM:\Software\Classes\Directory\shell\Open Sublime Project" -Name "Icon" -PropertyType "String" -Value "`"C:\Program Files\Sublime Text 2\sublime_text.exe`",0" -Force))
)
)
{
Write-Host "Error"
}
Else
{
Write-Host "No errors"
}
Upvotes: 2
Views: 175
Reputation: 5570
get-help about_Logical_Operators
:
The Windows PowerShell logical operators evaluate only the statements required to determine the truth value of the statement. If the left operand in a statement that contains the and operator is FALSE, the right operand is not evaluated. If the left operand in a statement that contains the or statement is TRUE, the right operand is not evaluated. As a result, you can use these statements in the same way that you would use the If statement.
You should separate execution from logical evaluation:
$val1 = New-Item -Path "HKLM:\Software\Classes\Directory\shell\Open Sublime Project\command" -Value "`"C:\Program Files\Sublime Text 2\sublime_text.exe`" --new-window %1" -Force)
$val2 = New-ItemProperty -Path "HKLM:\Software\Classes\Directory\shell\Open Sublime Project" -Name "Icon" -PropertyType "String" -Value "`"C:\Program Files\Sublime Text 2\sublime_text.exe`",0" -Force
If (-NOT($val1 -and $val2))
{
Write-Host "Error"
}
Else
{
Write-Host "No errors"
}
Also, I changed -or
to -and
, because with or, if one errors and the other one is successful, it will return no errors, which doesn't make sense to me
Upvotes: 5