在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脚本。
什么是相当于CMD的/ C开关,你用PowerShell的结果返回到脚本
使用-command参数:
powershell -command "dir"
用于简单的命令如列表目录
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...
使用这个“powershell -command dir”
你可以使用列表形式: ['powershell', '-command', 'dir'].execute()