在远程服务器上安装软件时调用命令的问题 [英] Issues with Invoke-Command while installing softwares in remote server

查看:20
本文介绍了在远程服务器上安装软件时调用命令的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以安静模式在多个远程服务器上安装一个应用程序.我使用 Powershell v3.0 创建了一个如下所示的脚本 (Installer.ps1):

I need to install an application in several remote servers in quiet mode. I have created a script (Installer.ps1) like below using Powershell v3.0:

param(
[String] $ServerNameFilePath = $(throw "Provide the path of text file which contains the server names"),
[String] $InstallerFolderPath = $(throw "Provide the Installer Folder Path. This should be a network location"),
[String] $UserName = $(throw "Provide the User Name"),
[String] $Password= $(throw "Provide the Password")
)
Function InstallApp
{
    $secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
    $mycreds = New-Object System.Management.Automation.PSCredential ($UserName, $secpasswd)

    $ScrBlock = {param($InstallerFolderPath) $ExePath = Join-Path $InstallerFolderPath "ServerReleaseManager.exe";  & $ExePath /q;}
    Invoke-Command -ComputerName (Get-Content Servers.txt) -Credential $mycreds $ScrBlock -ArgumentList $InstallerFolderPath

}

InstallApp -ServerNameFilePath $ServerNameFilePath -InstallerFolderPath $InstallerFolderPath -UserName $UserName -Password $Password

然后我调用如下脚本(安装程序文件夹路径可以有空格并且可执行 ServerReleaseManager.exe 接受参数):

Then I call the script like below (Installer folder path can have white spaces and the executable ServerReleaseManager.exe accepts argument):

.Installer.ps1 -ServerNameFilePath Servers.txt -InstallerFolderPath "\TestServer01PublicStable ApplicationsServer Release Manager Update 22.7" -UserName "DomainUser" -Password "Test123"

我总是低于 CommandNotFoundException:

The term '\TestServer01PublicStable ApplicationsServer Release Manager Update 22.7ServerReleaseManager.exe' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我尝试了其他选项,例如将 -FilePathInvoke-Command 一起使用,但错误相同.我真的被堵在这里了.你能告诉我为什么显示这个错误吗?如何解决错误?或者有没有更好的方法来解决这个问题.感谢您的帮助.

I have tried other options like using -FilePath with Invoke-Command but same error. I am really blocked here. Can you please let me know why this error has shown? How to resolve the error? Or are there any better ways to deal with this. Thanks for your help.

推荐答案

这听起来像是一个双跳身份验证问题.一旦您远程进入服务器,您就无法访问第三台服务器上的文件共享,因为您无法将基于 kerberos 的身份验证传递给它.

This sounds like a double-hop authentication issue. Once you're remoted into the server, you can't access a file share on a third server because you can't pass your kerberos-based authentication to it.

您可以先尝试从共享复制到远程服务器(这必须在执行脚本的计算机上完成),然后在脚本块中引用(现在是本地)路径.

You could try copying from the share to the remote server, first (this has to be done on the computer executing the script), and then in the scriptblock refer to the (now local) path.

您可以设置 CredSSP,这不是一个好主意.

You could set up CredSSP which isn't a great idea for this purpose.

基本上,您需要避免连接到一台机器,然后通过该连接连接到另一台机器.

Basically, you need to avoid connecting to one machine, then connecting to another through that connection.

param(
[String] $ServerNameFilePath = $(throw "Provide the path of text file which contains the server names"),
[String] $InstallerFolderPath = $(throw "Provide the Installer Folder Path. This should be a network location"),
[String] $UserName = $(throw "Provide the User Name"),
[String] $Password= $(throw "Provide the Password")
)
Function InstallApp
{
    $secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
    $mycreds = New-Object System.Management.Automation.PSCredential ($UserName, $secpasswd)


    $ScrBlock = {param($InstallerFolderPath) $ExePath = Join-Path $InstallerFolderPath "ServerReleaseManager.exe";  & $ExePath /q;}

    Get-Content Servers.txt | ForEach-Item {
        $remoteDest = "\$_c`$some	empfolder"
        $localDest = "C:some	empfolder" | Join-Path -ChildPath ($InstallerFolderPath | Split-Path -Leaf)

        try {
            Copy-Item -Path $InstallerFolderPath -Destination $dest -Force
            Invoke-Command -ComputerName $_ -Credential $mycreds $ScrBlock -ArgumentList $localDest
        finally {
            Remove-Item $remoteDest -Force -ErrorAction Ignore
        }
    }
}

InstallApp -ServerNameFilePath $ServerNameFilePath -InstallerFolderPath $InstallerFolderPath -UserName $UserName -Password $Password

注意事项

  1. 这是未经测试的.
  2. 正如 Swonkie 所提到的,如果这是您要实现的目标,则应该将参数设置为强制参数(在我的代码中未解决).
  3. 您不应传递单独的纯文本用户名和密码参数,然后将它们转换为凭证对象.而是传递单个 [PSCredential] 参数.您可以使用提示的默认值,例如 [PSCredential] $Cred = (Get-Credential).(这在我的代码中也没有解决).
  1. This is untested.
  2. As mentioned by Swonkie, you should set your parameters as mandatory if that's what you're looking to achieve (not addressed in my code).
  3. You shouldn't pass separate plain text user name and password parameters and then convert them to a credential object. Instead pass a single [PSCredential] parameter. You can use a default value that prompts, like [PSCredential] $Cred = (Get-Credential). (this is not addressed in my code either).

这篇关于在远程服务器上安装软件时调用命令的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆