$applicationArray = $filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe", $notepad = "C:\Program Files\Notepad \notepad .exe", $cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe", $adobe = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
foreach ($application in $applicationArray)
{
Test-path
if (Test-path)
{
Write-Host "Folder Exists"
}
else
{
Write-Host "Folder Does Not Exist"
}
}
I am trying to write a script that will check if the above programs are installed by useing the "Test-path" command. when I run them in individual "if" statements they all work but I was looking to loop it so I could compress the script to be as eficient as possible. but when I try to run the above I get the below error:
At C:\Users\Nicholas.McKinney\OneDrive - Brandmuscle\Documents\Scripts\Laptop_Setups.ps1:1 char:34
... filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe", $n ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
At C:\Users\Nicholas.McKinney\OneDrive - Brandmuscle\Documents\Scripts\Laptop_Setups.ps1:1 char:100
... a.exe", $notepad = "C:\Program Files\Notepad \notepad .exe", $cisco ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
At C:\Users\Nicholas.McKinney\OneDrive - Brandmuscle\Documents\Scripts\Laptop_Setups.ps1:1 char:153
... ", $cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mob ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : InvalidLeftHandSide
CodePudding user response:
Looks like you are trying to create a hashtable, but the syntax isn't quite right. Let's try it like so,
$apps = @{
filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe"
notepad = "C:\Program Files\Notepad \notepad .exe"
cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
adobe = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
}
To use hashtable values, loop through its keys and access each item by key value like so,
foreach($k in $apps.keys) {
"Testing path for {0} -> {1}: {2}" -f $k, $apps[$k], $(test-path $apps[$k])
}
# output
Testing path for adobe -> C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe: False
Testing path for notepad -> C:\Program Files\Notepad \notepad .exe: True
Testing path for cisco -> C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe: False
Testing path for filezilla -> C:\Program Files\FileZilla FTP Client\filezilla.exe: False
CodePudding user response:
This won't work:
$applicationArray = $filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe", $notepad = "C:\Program Files\Notepad \notepad .exe", $cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe", $adobe = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
You cam do an array of filepaths to verify, e.g.:
$paths = @(
"C:\Program Files\FileZilla FTP Client\filezilla.exe",
"C:\Program Files\Notepad \notepad .exe"
)
Once you did you can loop through the array and run test-path foreach element, e.g.:
foreach ($path in $paths){
If (Test-path -path $path){
write-host "Folder Exists"
}
Else {
Write-Host "Folder Does Not Exist"
}
}
CodePudding user response:
You could use a hashtable to for easier readability and iterate through each one testing the paths.
@{
filezilla = "C:\Program Files\FileZilla FTP Client\filezilla.exe"
notepad = "C:\Program Files\Notepad \notepad .exe"
cisco = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
adobe = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
}.GetEnumerator() | % {
if (Test-Path -LiteralPath $_.Value) {
'{0,-10} - Exists.' -f $_.Key
}
else {
'{0,-10} - Not Found.' -f $_.Key
}
}
# Results
adobe - Not Found.
notepad - Not Found.
cisco - Exists.
filezilla - Not Found.
I liked vonPryz approach, so I updated mine to mimic his/hers. It's essentially the same with just incorporating the conditional statement into the string format value.
'{0,-10} - {1}' -f $_.Key, $(
if (Test-Path -LiteralPath $_.Value) { 'Exists' } else { 'Not Found' }
)