Something strange is happening with my PowerShell script.
- I use Azure DevOps to automate a certain task, using YML pipelines.
- I need to copy files from Azure DevOps repo to a remote machine, which is a VM on Azure (a DTL machine to be precise - shouldn't make a difference though)
- Copying said files is done using the FTP task - it works so we know it's configured correctly.
- After the files are copied over, I need to install an application there. Here's the strange behaviour:
I use the PowerShellOnTargetMachines@3 task to run an inline script, according to which
a - It checks if the machine is connected to the DC (using Test-ComputerSecureChannel) - that works
b - Checks if a specific application is installed (like this: Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like "app*" }) and if it is, uninstalls it (using the uninstall() method) - that also works
c - Installs the app from the directory that was copied over using the FTP task (using Invoke-Command -ScriptBlock { Start-Process -FilePath "msiexec.exe" -ArgumentList "/i C:\<path_to_the_msi>.msi /qn" -Wait -NoNewWindow } ) - this does not work!
But the real interesting thing is this: If I run the same script on the VM locally, it works - meaning a and b works as expected and c also works - it installs the application just fine.
Now I am sure you know how the PowerShellOnTargetMachines works, but in case you do not, it basically uses WinRM to write the inline script (that's in the task itself) into a temporary ps1 file (at $HOME\AppData\Local\Temp) in the VM, and execute it. It is available there only for a brief period because it's deleted soon after, but I even went in there and opened the temp ps1 file copied the contents (which is same as my own script), and ran it AND THAT WORKED!
So the way I see it is that my code works because as per the inline script, tasks a and b do execute. It's just the c has an issue. And yes, I have tried Start-Process (like this: Start-Process -FilePath C:\<pat_to_the_msi>.msi -ArgumentList "/quiet /passive") and that doesn't work either.
FYI: TrustedHost is set to "*".
So long story short, an inline PS script does not work from the pipeline, but does locally, essentially, with some caveats (that the script written into the temp location works and installs the application but doesn't allow PS to execute it).
Why am I using the FTP task to copy instead of AzureFileCopy is for another day - yes I am aware they both (PowerShellOnTargetMachines and AzureFileCopy) use WinRM.
Any help is immensely appreciated!