Below is the code which I'm trying to run which installs software from machine A to B and then deletes the file from remote machine, which the installation works but cannot pass over the next piecce of code which is to delete the file, which causes the script not to exit.
I have found out the reason which is because of the child processes that are awaiting to be exited. So my question is how can I do this with the below code. Thanks everyone.
# Define the remote computer name and Software installation package path
$ComputerName = "user1"
$DestFolderPath = "C:\Temp\Test.exe"
$FilePath ="C:\temp\test.exe"
# Create a new PowerShell session to the remote computer
$Session = New-PSSession -ComputerName $ComputerName
#Create the destination folder if not already exist
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
param($DestFolderPath)
if (!(Test-Path $DestFolderPath)) {
}
} -ArgumentList $DestFolderPath
# Copy the software installation package to the remote computer
Copy-Item $FilePath -Destination $DestFolderPath -ToSession $Session
# Install the Software silently on the remote computer
Invoke-Command -Session $Session -ScriptBlock {
param($DestFolderPath)
Start-Process -FilePath $DestFolderPath -ArgumentList "/silent" -wait} -ArgumentList $DestFolderPath
# Remove the installation package from the remote computer
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
param($DestFolderPath)
Remove-Item $DestFolderPath -Force -Recurse } -ArgumentList $DestFolderPath
# Close the PowerShell session to the remote computer
Remove-PSSession $Session
I have did google research but wasn't able to acquire any further resolution.
-ComputerName $ComputerNamerather than-Session $Sessionin some of yourInvoke-Commandcalls. Also note that you can place theRemove-Itemcall after theStart-Processcall, so that you don't need to make a separate call later.