栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java程序中执行PowerShell命令

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java程序中执行PowerShell命令

您应该编写一个这样的Java程序,这是一个基于Nirman的技术博客的示例,其基本思想是执行如下调用PowerShell进程的命令:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class PowerShellCommand { public static void main(String[] args) throws IOException {  //String command = "powershell.exe  your command";  //Getting the version  String command = "powershell.exe  $PSVersionTable.PSVersion";  // Executing the command  Process powerShellProcess = Runtime.getRuntime().exec(command);  // Getting the results  powerShellProcess.getOutputStream().close();  String line;  System.out.println("Standard Output:");  BufferedReader stdout = new BufferedReader(new InputStreamReader(    powerShellProcess.getInputStream()));  while ((line = stdout.readLine()) != null) {   System.out.println(line);  }  stdout.close();  System.out.println("Standard Error:");  BufferedReader stderr = new BufferedReader(new InputStreamReader(    powerShellProcess.getErrorStream()));  while ((line = stderr.readLine()) != null) {   System.out.println(line);  }  stderr.close();  System.out.println("Done"); }}

为了执行Powershell脚本

String command = "powershell.exe  "C:\Pathtofile\script.ps" ";


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507188.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号