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

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

问题描述

我需要在安静模式下的多个远程服务器上安装应用程序.我已经使用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 "\\TestServer01\Public\Stable Applications\Server Release Manager Update 2\2.7" -UserName "Domain\User" -Password "Test123"

我总是低于CommandNotFoundException:

The term '\\TestServer01\Public\Stable Applications\Server Release Manager Update 2\2.7\ServerReleaseManager.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\temp\folder"
        $localDest = "C:\some\temp\folder" | 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).

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

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