Reputation: 3
new to stackoverflow. Have been a lurker for years but now need help. I am writing a simple script for an MSP to detect for a base set of applications and if not installed to run the chocolatey install command for that app. When I run this in ISE as an admin it reports back true for all the variable values even if it is not true. If I run just the test-path and tell it to write-host the value it is correct. But when ran all together it always comes back True. Can someone help investigate for me?
#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"}
Upvotes: 0
Views: 188
Reputation: 3624
Your code has a few issues,
IF ($a = "True")
You cant use an =
equals, you must use a -eq
. Also, "True" is a String
containing 4 characters, the boolean
values for True and False are $true
and $false
, so your code becomes:
IF ($a -eq $true)
But you can simplify this further:
IF ($a)
Below is how i would write this logic:
if(-not (Test-Path -Path "C:\Program Files\7-Zip")){
Write-Host "Installing 7-Zip...";
# do stuff ...
}else{
Write-Host "7-Zip already installed";
}
(Like in Mathematics you start from the innermost parentheses and work outwards, in this case Test-Path -Path "C:\Program Files\7-Zip"
first - which in my case returns $true
. The -not()
negates the result of the expression in the parentheses (
)
changing it to $false
).
Given how you plan to use it, you should probably wrap it up inside a function so you can write the logic once and re-use it without duplicating the code such as:
#Simple function
function InstallIfNessersary([String] $installDir, [String] $chocoPackage){
if(-not (Test-Path -Path $installDir)){
Write-Host "Installing Package $chocoPackage...";
#Do stuff with $chocoPackage
}else{
Write-Host "7-Zip already installed";
}
}
#Call the function for each app
InstallIfNessersary -installDir "C:\Program Files\7-Zip" -chocoPackage 7zip
InstallIfNessersary -installDir "C:\Program Files\X" -chocoPackage X
InstallIfNessersary -installDir "C:\Program Files\Y" -chocoPackage Y
Which on my PC outputs:
7-Zip already installed
Installing Package X...
Installing Package Y...
Upvotes: 1
Reputation: 3
I was setting my variable to true and not checking for the value. Changed = to -eq for if statement and it runs fine now.
Upvotes: 0
Reputation: 4379
Did you enter into the console the code exactly like it is in the question? If so, then it isn't correct syntax. The if
statements need to be on a new line, or have ;
separating them from the Test-path
statement:
$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"}
When trying to figure out when something is not working, proper formatting can help, especially when copy pasting code into a window. If everything still is reporting 'True' check to see if your console has those variables set to true
already
Upvotes: 0