Having scope issues

Hello,
I am trying to add some intelligence to this script by checking to see if the file exists before trying to copy, if it does exist, dont copy, just move on.

$RemoteComputer = Read-Host -Prompt 'Please enter computer name to Install Kace on'
$SessionRemote = New-PSSession $RemoteComputer
If (-not (Test-Path -Path C:\Temp\ampagent-10.1.43-x86.msi){
Copy-Item -path 'C:\Temp\ITScript\KaceInstall\ampagent-10.1.43-x86.msi' -Destination 'c:\Temp' -ToSession $SessionRemote
                    }
                    Invoke-Command -ComputerName $RemoteComputer -ScriptBlock {
                    $Hostname = hostname
                    Start-Process "msiexec.exe" -ArgumentList "/i ""C:\temp\ampagent-10.1.43-x86.msi"" HOST= NOHOOKS=1 /qn /norestart /log ""C:\temp\$Hostname-Kace_install.log"" /quiet" -Wait -NoNewWindow
                    Get-Service -DisplayName "*Kace*"
                    Get-Service -DisplayName "*Kace*" | Out-File -FilePath "C:\Temp\$Hostname-KaceService.txt"
                    Read-Host -Prompt "Press Enter key to continue"
                        }

I understand my problem is the If (-not (Test-Path…) statement is looking on my local system therefore it still tries to copy it everytime.

How can I check the remote computers path?

I tried putting that into its own Invoke-Command but not having much luck :frowning:

You may use an UNC path in the format \\<RemoteComputerName>\C$\temp\<msiFileName>. Check if it’s there. If not copy it there and then you can run the remote command and use it there as local path.

Great, did not think of using UNC. Works well, thanks.