2

我有当前的 powershell 脚本

$release = read-host "enter release"
$fullPath = "10.0.0.3"
$fullPath = $fullPath + $release
$fullPath = $fullPath + ".bat"
start-process $fullPath

目前,我登录到远程机器以运行此示例 10.0.0.2 的代码。然后代码从另一台远程机器 (10.0.0.3) 中提取文件。我想做的是从我的本地机器(10.0.0.1)执行这个脚本并让它在 .2 上运行。我可以完全访问该框,这样应该会更容易。我该怎么做呢?

4

3 回答 3

8

将脚本放入一个脚本文件中,比如 ReleaseProcessing.ps1 并像这样执行它:

Invoke-Command -ComputerName 10.0.0.2 -FilePath .\ReleaseProcessing.ps1

系统将提示您在本地计算机上“输入发布”,但脚本将传输到远程计算机并在远程计算机上运行。这要求您运行 PowerShell 2.0 或更高版本,并且您已通过在该计算机上运行 Enable-PSRemoting 在 10.0.0.2 上启用远程处理。此外,由于涉及另一台机器,您可能会遇到第二跳凭证问题。如果是这种情况,请考虑使用-Authentication值为 的参数CredSSP

于 2012-06-25T20:42:41.657 回答
2

我在一个项目中使用过一次。我需要在 APP 服务器上创建网站并且必须从 INT 服务器运行。因此,我将站点设置脚本保存在 App 服务器上,并从 INT 服务器远程运行。

 $script = 
 {
 $Filetoexe = $args[0]
 invoke-expression "$Filetoexe"
 }
 Invoke-Command -ComputerName <Computername> -credential <credentials> -ScriptBlock $script -Args "D:\sitesetup.ps1"

保存并运行它。最后一行“Invoke-Command”将被执行,它将访问您提供的远程计算机并执行脚本块。

脚本块接受最后一行中传递的参数并执行脚本块中的文件。

您需要在远程系统上运行的任何功能都必须在脚本块中完成。

希望对您有所帮助....

于 2012-07-01T18:18:19.810 回答
0

如果您将远程服务器设置为通过 powershell 进行远程访问,这将很有效

$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$StrServers = Get-Content -Path "$scriptPath\Environment\$strEnvironment.txt"


$content = [IO.File]::ReadAllText("$scriptPath\environment\test.txt")
foreach ($strServer in $content)
{
Invoke-Command -ComputerName $strServer -FilePath $scriptPath\testScript.ps1 -ArgumentList $StrLog
}
于 2016-08-04T20:07:14.933 回答