Windows 如何使用PowerShell脚本在远程计算机上运行另一个PowerShell脚本
我希望通过设置PowerShell脚本,自动从Microsoft网站下载Defender防病毒定义,使我网络上的所有计算机保持最新。然后,脚本将.exe文件从我的计算机传输到文本文件中的所有计算机 我现在想要它做的是在远程计算机上运行.exe,但我无法让它以某种方式工作。下面是我的代码Windows 如何使用PowerShell脚本在远程计算机上运行另一个PowerShell脚本,windows,powershell,Windows,Powershell,我希望通过设置PowerShell脚本,自动从Microsoft网站下载Defender防病毒定义,使我网络上的所有计算机保持最新。然后,脚本将.exe文件从我的计算机传输到文本文件中的所有计算机 我现在想要它做的是在远程计算机上运行.exe,但我无法让它以某种方式工作。下面是我的代码 $File=”https://go.microsoft.com/fwlink/?LinkID=121721&arch=x64" $Location=“C:\temp\mpam fe.exe” $Client=Ne
$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)
写主机“正在下载文件…”
$Computers=获取内容“C:\temp\Computers.txt”
foreach($Computers in$Computers){
$Session=New PSSession-ComputerName$Computer
复制项目-路径C:\temp\mpam-fe.exe C:\temp \-ToSession$Session-Recurse-Force
写入主机“将文件传输到$Computer”
输入PSSession$Computer
设置位置“C:\temp\”
调用命令。/RunAV.ps1
退出会话
}
尝试在远程计算机上运行脚本时收到的错误消息
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
Invoke命令:无法使用指定的名称解析参数集
参数。
在C:\PSScripts\AVDownload.ps1:16 char:9
+调用命令。/RunAV.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:InvalidArgument:(:)[Invoke Command],ParameterBindingException
+FullyQualifiedErrorId:含糊不清的USParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
由于要运行的脚本似乎位于远程计算机上,因此应按如下方式运行:
$Session = New-PSSession -ComputerName $Computer
Copy-Item ...
Invoke-Command -Session $Session -Scriptblock {
Set-Location 'C:\temp'
& ./RunAV.ps1
}
进入PSSession
和退出PSSession
供交互使用。不要在非交互式上下文中使用它们。