繁体   English   中英

如何在远程计算机上运行 PowerShell 脚本?

[英]How to run PowerShell script on a remote computer?

我的目标是在远程计算机上运行用于安装 Google Chrome 的脚本。 本地安装的脚本已经保存在我的电脑上(\localhost\c$\Chrome\Chrome Installer.ps1")。

我想先把脚本复制到远程机器上,然后远程运行脚本。 您能否建议远程运行该脚本的最佳方法? 先感谢您

最好的问候, 斯坦

您可以在下面找到脚本:

$Installer = "$env:temp\chrome_installer.exe"
$url = 'http://dl.google.com/chrome/install/375.126/chrome_installer.exe'
Invoke-WebRequest -Uri $url -OutFile $Installer -UseBasicParsing
Start-Process -FilePath $Installer -Args '/silent /install' -Wait
Remove-Item -Path $Installer
$login = Read-Host "Please enter login id"
$comp = Read-Host "Please enter computer name or IPV4 adress"
Copy-Item -Path "\\localhost\c$\Chrome\Chrome Installer.ps1"  -Dest "\\$($comp)\c$\temp"

我想先把脚本复制到远程机器上,然后远程运行脚本。

您实际上不需要首先将它复制到远程主机 - 您可以让Invoke-Command从远程计算机上的本地文件系统运行脚本,如下所示:

Invoke-Command -ComputerName $comp -FilePath "C:\Chrome\Chrome Installer.ps1"

要传递远程计算机的凭据,请将帐户名称指定为-Credential参数的参数,PowerShell 将提示您输入密码:

$login = Read-Host "Enter the user name for the remote host"
Invoke-Command -ComputerName $comp -FilePath "C:\Chrome\Chrome Installer.ps1" -Credential .\$login

您可以执行enter-pssession以获得本地提示并使用您的凭据粘贴到您的脚本中。 您还可以签invoke-commdand并指定脚本文件和 tagret。

运行: update-help

然后运行:

help invoke-command -examples

您应该看到适合您情况的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM