powershell - 如何在远程计算机上运行 PowerShell 脚本?
问题描述
我的目标是在远程计算机上运行用于安装 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
推荐阅读
- python - 仅使用新信息将字典读取到文件覆盖
- z3 - 精益是否增强了证明可测量性?
- c# - 从列表中提取所有数据
- 在 C# 中使用 HTML Agility Pack 标记
- google-cloud-platform - 如何在 GCP 中监控 VM 服务?
- javascript - 我的 svg 贝塞尔曲线的波浪效果这可能吗?
- python - 分区在这个函数中有什么作用?
- java - 一次替换哈希图中的所有相同值
- javascript - 如何在 ReactJS 中使用单个代码库从父应用程序创建子应用程序
- firebase - Firestore 侦听器数据使用情况
- amazon-web-services - 将 Docker 映像上传到 AWS ECR