Hey everyone,
I'm having a tough time with my script and could really use your help. Here's the deal: I'm trying to uninstall Citrix VDA, create a "completed" folder to indicate the process is done, then reboot the machine. After the reboot, I want the script to check for the completed folder, clean up some directories, and copy logs to a server share (though I haven't worked on the last part yet).
The first run goes smoothly, and I get a "Completed" output. However, on the second run, it's trying to redo everything and errors out since the folders are already there. The if statement at the start, which checks for the existence of the completed folder, isn't working right. When I uncomment the code for cleanup, it tells me the folder doesn't exist, even though when I check on the remote machine, it does.
I've attempted to add a return but it stops the whole script. Can anyone point out what I'm missing? Thanks in advance! Here's a look at my code:
$computers = @("computername") #This will eventually pull from a CSV
$localPaths = @("C:citrix", "C:program filescitrix", "C:completed")
foreach ($computer in $computers) {
Write-Host "Connecting to $computer..."
$session = New-PSSession -ComputerName $computer -Credential domain\username -ErrorAction Stop
if (-not(test-path -LiteralPath "c:completed")) {
Invoke-Command -Session $session -ScriptBlock {
# Code for uninstallation and folder creation
}
} else {
# Code for cleanup
}
}
3 Answers
You could simplify your script by removing extra duplicates. Right now, it has convoluted logic that could be streamlined. Check the folder existence, and if not, perform the uninstall and create your completed folder. You might also find cleaning up a single log caller can help keep everything organized.
I noticed you mentioned using nearly 100 machines. It's smart to set the script up to create a "completed" folder on each machine for tracking. Just make sure your logic flow accurately checks for those folders after each reboot. Also, consider using Citrix’s built-in uninstaller for a cleaner removal—it can save you some hassle with leftover folders and registry issues!
Looks like your main issue is that you’re checking a local folder when you should be checking the one on the remote machine after you’ve established the session. Move the folder existence check inside the `Invoke-Command` block so it checks from the perspective of the remote machine.
Also, it’s a good idea to define your path variables inside the script block. Remember, if you declare a variable outside of that block, it won’t carry over to the remote context unless you explicitly pass it.
Lastly, keep in mind that `Invoke-Command` runs all the commands at once for all machines in the list and it might not behave as expected with larger groups. Test on just a few computers first! Good luck!
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String