0

I am looking to keep all the computers on my network up to date with the Defender Anti-Virus definitions by setting up a PowerShell script that will automatically download them from the Microsoft website. The script will then transfer the the .exe file from my computer to all the computers in a text file.

What I want it to do now is to run the .exe on the remote computer but I am not getting it to work somehow. Below is my code.

$File = "https://go.microsoft.com/fwlink/?LinkID=121721&arch=x64"
$Location = "C:\temp\mpam-fe.exe"
$Client = New-Object System.Net.WebClient
$Client.DownloadFile($File, $Location)

Write-Host "Downloading file..."

$Computers = Get-Content "C:\temp\Computers.txt"

foreach ($Computer in $Computers) {
    $Session = New-PSSession -ComputerName $Computer
    Copy-Item -Path C:\temp\mpam-fe.exe C:\temp\ -ToSession $Session -Recurse -Force
    Write-Host "Transferring file to $Computer"
    Enter-PSSession $Computer
    Set-Location "C:\temp\"
    Invoke-Command ./RunAV.ps1
    Exit-PSSession
}

The error message that I receive when it tries to run the script on the remote computer

Invoke-Command : Parameter set cannot be resolved using the specified named
parameters.
At C:\PSScripts\AVDownload.ps1:16 char:9
+         Invoke-Command ./RunAV.ps1
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
1
  • 1
    Enter-PSSession and Exit-PSSession are for interactive use. Don't use them in non-interactive contexts. Commented Jul 25, 2019 at 8:39

1 Answer 1

1

Since the script you want to run appears to be located on the remote computer you should run it like this:

$Session = New-PSSession -ComputerName $Computer
Copy-Item ...
Invoke-Command -Session $Session -Scriptblock {
    Set-Location 'C:\temp'
    & ./RunAV.ps1
}
Sign up to request clarification or add additional context in comments.

Comments

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.