![](/img/trans.png)
[英]How to use a PowerShell script to run another PowerShell Script on a Remote Computer
[英]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.