【问题标题】:Powershell Invoke-Command executing on local, rather than remote serverPowershell Invoke-Command 在本地而不是远程服务器上执行
【发布时间】:2012-08-07 23:37:37
【问题描述】:

我有一个脚本,它将另一个脚本复制到远程服务器,然后执行第二个脚本。上周工作正常,但今天第一个脚本的执行失败,说找不到第二个脚本。作为故障排除的一部分,我在本地服务器上创建了第二个脚本的简单版本(仅包含 Write-Host "Wrong Server!)。现在,当我运行第一个脚本时,虚拟的第二个脚本在本地服务器上执行!

我已经通过下面的测试工具脚本粘贴了:

$DeploymentFolderOnTargetServer = "c:\biztalkdeployment"

$TargetServer = "d-vasbiz01"                
$Username = "TFS_Service"
$Password = "x"
$SecPass = ConvertTo-SecureString $Password -AsPlainText -Force
$Cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $Username,$SecPass
$Session = New-PSSession -ComputerName $TargetServer -Authentication CredSSP -Credential $Cred
$Environment = "Dev"
$ExecuteScriptFilePath = join-path "$DeploymentFolderOnTargetServer" "ExecuteBizTalkAppMSI.ps1"
$MSI = "bin\debug\x.int.mis-3.0.0.msi"
$InstallFolderOnTargetServer = "C:\Program Files (x86)\x.Int.MIS for BizTalk 2010\3.0"

Write-Host "Session = $Session"
Write-Host "ExecuteScriptFilePath = $ExecuteScriptFilePath"
Write-Host "MSI = $MSI"
Write-Host "InstallFolderOnTargetServer = $InstallFolderOnTargetServer"
Write-Host "Environment = $Environment"
Write-Host "DeploymentFolderOnTargetServer = $DeploymentFolderOnTargetServer"


Invoke-Command -Session $Session  -FilePath $ExecuteScriptFilePath -argumentlist $MSI, $InstallFolderOnTargetServer, $Environment, $DeploymentFolderOnTargetServer             

测试的输出如下:

Session = System.Management.Automation.Runspaces.PSSession
ExecuteScriptFilePath = c:\biztalkdeployment\ExecuteBizTalkAppMSI.ps1
MSI = bin\debug\x.int.mis-3.0.0.msi
InstallFolderOnTargetServer = C:\Program Files (x86)\Vasanta.Int.MIS for BizTalk 2010\3.0
Environment = Dev
DeploymentFolderOnTargetServer = c:\biztalkdeployment
Wrong Server!

如果我运行 get-session,则会话的计算机名称正确指向第二台服务器。

有什么想法吗?

【问题讨论】:

  • 而不是使用Write-Host "wrong server" 进行故障排除尝试Write-Host (hostname.exe) 以确保第二个脚本在哪台机器上执行。此外,Invoke-Command-FilePath 参数指定了脚本的 本地 路径,而不是目标计算机上的路径。 Invoke-Command 将负责将本地脚本文件传送到目标计算机,以便它可以在那里执行。
  • 感谢基思的回答。我会在早上试试这个。我可以确认,-FilePath 应该指向本地文件吗?这与我的理解背道而驰,所以可能是我出错的地方。
  • 是的,-FilePath 应该指向本地文件或本地计算机可以访问的位置(即您可以使用网络共享)。查看有关 Invoke-Command 的帮助主题以获取更多信息。 technet.microsoft.com/library/hh849719.aspx
  • 就是基思!一旦您突出显示 Invoke-Command 会将脚本复制到目标计算机,那么一切就到位了。感谢您的帮助。
  • 没问题。我将相关评论复制到该问题的建议答案中以供将来参考。

标签: powershell


【解决方案1】:

Invoke-Command-FilePath 参数指定脚本的本地 路径,而不是目标计算机上的路径。 Invoke-Command 将负责将本地脚本文件传送到目标计算机,以便它可以在那里执行。

【讨论】:

    猜你喜欢
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多