首页 > 解决方案 > 使用 sshpass、bash 和 PsExec 问题从 rhel-server1 在 windows-machine1 上执行 file.ps1

问题描述

如果我有头发,我会再次秃顶。

我有 2 台服务器。

windows01 linux01

windows01机器上,我有一个基于 GUI 的应用程序,我需要从linux01服务器启动。

windows01机器上名为start.ps1的脚本如下所示:(它放置在应用程序和psexec.exe的可执行文件所在的位置)

if((get-process "TmaApplication" -ea SilentlyContinue) -eq $Null){ 
        echo "TmaApplication not running, attempting to start"
        & .\psexec.exe -accepteula TMA.exe /u "Default User" /c y /p 5003
}

else{ 
    echo "Running"
 }

但是,当我从linux01服务器运行以下命令时,我得到以下信息:(由于某种原因,它从 linux 机器上运行,而不是简单地执行位于windows01机器上的 start.ps1 文件。)

[root@linux01]# ./test.sh

TmaApplication not running, attempting to start
.\psexec.exe : The term '.\psexec.exe' is not recognized as the name of a cmdlet, function, script file, or operable pr
ogram. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Program Files (x86)\redacted\redacted\redacted 1.0\redacted Application\start.ps1:3 char:9
+         .\psexec.exe -accepteula TMA.exe /u "Default User" /c y /p 50 ...
+         ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\psexec.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 

test.sh文件的内容是:

#!/bin/bash
STP="redacted"
Server="redacted"
User="redacted"
Password="redacted"
TMAVersion="1.0"
PsExecSourceFile="/opt/tools/psexec.exe"
PsExecFileName="psexec.exe"
Test="test.ps1"


sshpass -p $Password ssh -o stricthostkeychecking=no $User@$Server 'powershell -InputFormat None -File "C:/Program Files (x86)/redacted/redacted/redacted '$TMAVersion'/redacted Application/'$Test'"'

我对此非常陌生,我被迫使用我有复制粘贴的东西和没有的东西。除此之外一切都很好。如果我通过右键单击“使用 powershell 运行”直接从 windows01 机器运行 start.ps1,它完全符合我的要求。但我需要从这台 linux 机器上执行它。

当涉及到操作系统级别时,我对 linux 和 windows 环境具有完全的 root/admin 控制权。但我宁愿限制第三方应用程序。如果不直接从 Microsoft 或 RedHat 发布,恐怕任何非开源都不是前进的选择

标签: linuxpowershellpsexecrhel7invoke-command

解决方案


你能在 linux 机器上安装 Powershell 7 吗?https://github.com/powershell/powershell

然后就可以使用PSSessions通过powershell连接windows机器了。

$session = New-PSSession -HostName UbuntuVM1 -UserName TestUser
Enter-PSSession $session

更多信息: https ://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/ssh-remoting-in-powershell-core?view=powershell-7


推荐阅读