windows 如何从groovy中以与“cmd /c“.execute()相同的方式运行PowerShell命令< command>?

x8goxv8g  于 2023-03-24  发布在  Windows
关注(0)|答案(6)|浏览(215)

在Groovy中,我可以直接运行Windows cmd shell,并像这样读取结果:

def proc = "cmd /c dir".execute()
proc.wait()
println "stdout: ${proc.in.text}"

但是,如果我尝试使用PowerShell,它会阻塞并且不会返回:

def proc = "powershell dir".execute()

我试过了

def proc = "powershell -NonInteractive dir".execute()

等等,但是它们都阻塞了,我不得不终止Groovy脚本。
与PowerShell中用于获取返回脚本的结果的cmd的/c开关等效的是什么?

eeq64g8w

eeq64g8w1#

使用-command参数:
powershell -command "dir"

pokxtpni

pokxtpni2#

您可以使用列表表单:

['powershell', '-command', 'dir'].execute()
baubqpgj

baubqpgj3#

我使用了一个Groovy类来执行一个PowerShell脚本。这比仅仅执行一个命令要复杂一点,但我认为这可能会有帮助。

import groovy.util.logging.Log4j
import org.springframework.stereotype.Component

@Component
@Log4j
class PowerShellUtil {

    def exec(debug, command, args){

        def powerShellCommand = ".\\${command} ${args}"
        def shellCommand = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile  -Command \"${powerShellCommand}\""

        if (debug) log.debug powerShellCommand

        def process = shellCommand.execute()
        def out = new StringBuffer()
        def err = new StringBuffer()
        process.consumeProcessOutput(out, err)
        process.waitFor()
        if(out.size() > 0 && debug) log.debug out
        if(err.size() > 0) log.error err
    }
}

然后我可以执行脚本:

PowerShellUtil psUtil = new PowerShellUtil()
psUtil.exec(true, 'script.ps1','script-args')
8tntrjer

8tntrjer4#

使用此命令:

powershell -command dir
omvjsjqw

omvjsjqw5#

对于类似列表目录的简单命令

powershell -command ls

要在cmd中运行PowerShell脚本,应使用编码命令,如:

$script = {Get-EventLog -LogName System -Newest 10 | where { $_.Index -ge 5071811 } | sort Index}

然后:

[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes( $script))
RwBlAHQALQBFAHYAZQBuAHQATABvAGcAIAAtAEwAbwBnAE4AYQBtAGUAIABTAHkAcwB0AGUAbQAgAC0ATgBlAHcAZQBzAHQAIAAxADAAIAB8ACAAdwBoAGU
AcgBlACAAewAgACQAXwAuAEkAbgBkAGUAeAAgAC0AZwBlACAANQAwADcAMQA4ADEAMQAgAH0AIAB8ACAAcwBvAHIAdAAgAEkAbg

我的结果:

C:\Users\soheil>powershell -encodedcommand RwBlAHQALQBFAHYAZQBuAHQATABvAGcAIAAtA
EwAbwBnAE4AYQBtAGUAIABTAHkAcwB0AGUAbQAgAC0ATgBlAHcAZQBzAHQAIAAxADAAIAB8ACAAdwBoA
GUAcgBlACAAewAgACQAXwAuAEkAbgBkAGUAeAAgAC0AZwBlACAANQAwADcAMQA4ADEAMQAgAH0AIAB8A
CAAcwBvAHIAdAAgAEkAbgBkAGUAeAA=

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
 5071812 Mar 10 22:39  Information Service Control M...   1073748860 The Mul...
 5071813 Mar 10 22:40  Information Service Control M...   1073748860 The App...
 5071814 Mar 10 22:45  Information Service Control M...   1073748860 The Mul...
 5071815 Mar 10 22:48  Information Service Control M...   1073748860 The Dia...
 5071816 Mar 10 22:55  Information Service Control M...   1073748860 The Mul...
 5071817 Mar 10 22:58  Information Service Control M...   1073748860 The Mul...
 5071818 Mar 10 22:59  Information Service Control M...   1073748860 The Goo...
 5071819 Mar 10 22:59  Information Service Control M...   1073748860 The Goo...
 5071820 Mar 10 23:14  Information Service Control M...   1073748860 The Mul...
 5071821 Mar 10 23:30  Information Service Control M...   1073748860 The Mul...
ffx8fchx

ffx8fchx6#

下面是我使用的方法:

// powershell.groovy file
def executePowershellCommand(String command, boolean returnStdout = false) {
    def result
    def wrappedCommand = """
    @echo off
    for /f "tokens=*" %%a in ('powershell -noProfile -executionPolicy bypass -command "& { ${command} }"') do (
        echo %%a
    )
    """
    result = bat script: wrappedCommand, returnStdout: returnStdout

    if (returnStdout) result = result.trim()

    return result
}

return this

相关问题