简体   繁体   English

在需要交互/重定向的远程 Powershell 会话中执行批处理

[英]Execute Batch in Remote Powershell Session that Requires Interactive/Redirection

My company uses an Oracle based product called "Hyperion".我的公司使用名为“Hyperion”的基于 Oracle 的产品。 I believe Oracle hired some high school students to write the batch files to start and stop the services.我相信 Oracle 聘请了一些高中生来编写批处理文件来启动和停止服务。 The problem I have is that I would like to use Powershell Remoting to execute a batch file called "Stop_Hyperion.bat" which is provided by Oracle for the purposes of stopping several processes and services in a controlled manner.我遇到的问题是,我想使用 Powershell Remoting 来执行一个名为“Stop_Hyperion.bat”的批处理文件,该文件由 Oracle 提供,用于以受控方式停止多个进程和服务。 Unfortunately for me this doesn't work:不幸的是,这对我来说不起作用:

$Session = New-PSSession -ComputerName HyperionServer1 -Credential $PSCredential
Invoke-Command -Session $Session -ScriptBlock {C:\Scripts\Stop_Hyperion.bat}

It executes the batch file, but the batch file contains a command called "Timeout" which generates the following error:它执行批处理文件,但批处理文件包含一个名为“超时”的命令,该命令会生成以下错误:

ERROR: Input redirection is not supported, exiting the process immediately.
    + CategoryInfo          : NotSpecified: (ERROR: Input re...ss immediately.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
    + PSComputerName        : HYPERIONSERVER1

If I use good old PSExec, I get the same error unless I use the -i option for a desktop interactive session.如果我使用旧的 PSExec,除非我对桌面交互会话使用 -i 选项,否则我会收到相同的错误。 Is there some way to accomplish that kind of interactive session with PSSession?有没有办法用 PSSession 完成那种交互式会话?

I would have modified the batch file or just stopped the services using Powershell commands, but the batch files contain call to other batch files (which also use Timeout), which contain even more calls.我会修改批处理文件或只是使用 Powershell 命令停止服务,但批处理文件包含对其他批处理文件(也使用超时)的调用,其中包含更多调用。 There are so many nested calls that it would take quite awhile to unravel exactly what the batch files are doing.有如此多的嵌套调用,需要很长时间才能准确地解开批处理文件在做什么。

You can look at the batch files, and create a native PowerShell implementation as replacement. 您可以查看批处理文件,并创建一个本机PowerShell实现作为替换。

'Stop-Service' and 'Start-Service' are standard cmdlets available on Windows. “停止服务”和“启动服务”是Windows上可用的标准cmdlet。

This may ignore some of the complexity in the supplied batch files, but if it gets the job done, it is sufficient. 这可能会忽略提供的批处理文件中的某些复杂性,但是如果完成工作,就足够了。 This is the same as stopping and starting a service from the services manager. 这与从服务管理器停止和启动服务相同。

Using Enter-PSSession localhost , I can't get a BAT file to use timeout .使用Enter-PSSession localhost ,我无法获得 BAT 文件以使用timeout File a.bat :文件a.bat

timeout 5 >NUL
echo %errorlevel% > C:\Users\my_user\abc.txt

But, by running Start-Process .\\a.bat -wait then I can see timeout correctly waits, and returns exit code 0. With a little more .NET you should be able to get the stdout if needed.但是,通过运行Start-Process .\\a.bat -wait然后我可以看到timeout正确等待,并返回退出代码 0。如果需要更多的 .NET,您应该能够获得标准输出。

I think this works by spawning the CMD process with its own new STDIN as if it was in a new conhost window (instead of reusing the powershell process STDIN which cannot be redirected).我认为这是通过使用自己的新 STDIN 生成 CMD 进程来工作的,就好像它在一个新的主机窗口中一样(而不是重用无法重定向的 powershell 进程 STDIN)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM