【问题标题】:How to use a PowerShell script to run another PowerShell Script on a Remote Computer如何使用 PowerShell 脚本在远程计算机上运行另一个 PowerShell 脚本
【发布时间】:2019-12-03 11:15:50
【问题描述】:

我希望通过设置将自动从 Microsoft 网站下载它们的 PowerShell 脚本,使我网络上的所有计算机与 Defender Anti-Virus 定义保持同步。然后,该脚本会将 .exe 文件从我的计算机传输到文本文件中的所有计算机。

我现在想要它做的是在远程计算机上运行 .exe,但我无法让它以某种方式工作。下面是我的代码。

$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
}

尝试在远程计算机上运行脚本时收到的错误消息

调用命令:无法使用指定的命名解析参数集 参数。 在 C:\PSScripts\AVDownload.ps1:16 char:9 + 调用命令 ./RunAV.ps1 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand

【问题讨论】:

  • Enter-PSSessionExit-PSSession 用于交互使用。不要在非交互式环境中使用它们。

标签: windows powershell


【解决方案1】:

由于您要运行的脚本似乎位于远程计算机上,您应该像这样运行它:

$Session = New-PSSession -ComputerName $Computer
Copy-Item ...
Invoke-Command -Session $Session -Scriptblock {
    Set-Location 'C:\temp'
    & ./RunAV.ps1
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    相关资源
    最近更新 更多