1

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.

4
  • I don't know if it's the cause of your problem, but you're using -ComputerName $ComputerName rather than -Session $Session in some of your Invoke-Command calls. Also note that you can place the Remove-Item call after the Start-Process call, so that you don't need to make a separate call later. Commented Aug 18, 2023 at 15:42
  • 1
    tried but no luck. Commented Aug 21, 2023 at 8:47
  • I've managed to figure it out now, thanks. Commented Aug 23, 2023 at 8:00
  • Glad to hear it, @Anthony. Assuming it is of general interest, can you share the solution, so that others can benefit from it, ideally in the form of an answer? Commented Aug 23, 2023 at 12:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.