如何以与#34; cmd / c <command />&#34; .execute()相同的方式从groovy运行PowerShell命令?

时间:2015-03-23 21:44:48

标签: windows powershell groovy

在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一起使用的/c cmd的等效项,与结果一起返回脚本。

5 个答案:

答案 0 :(得分:1)

使用-command参数:

powershell -command "dir"

答案 1 :(得分:0)

对于简单命令,例如列表目录:

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...

答案 2 :(得分:0)

使用此:

powershell -command dir

答案 3 :(得分:0)

您可以使用“列表”表单:

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

答案 4 :(得分:0)

我使用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')