jPowerShell执行PowerShell脚本,环境必须为Windows系统

https://github.com/profesorfalken/jPowerShell 


安装 

要安装jPowerShell,您可以将依赖性添加到软件项目管理工具中:HTTP://mvnrepository.com/artifact/com.profesorfalken/jPowerShell/1.7 

例如,对于Maven的你只需添加到你的pom.xml: 

maven工程依赖导入第三方jar 

  <依赖> 
        <groupId> com.profesorfalken </ groupId> 
        <artifactId> jPowerShell </ artifactId> 
        <version> 1.7 </ version> 
    </ dependency > 

相反,您可以直接下载JAR文件并将其添加到类路径中.https://repo1.maven.org/maven2/com/profesorfalken/jPowerShell/1.7/jPowerShell-1.7 



单个命令执行 

   //在PowerShell的会话中执行命令 
   PowerShellResponse response = PowerShell.executeSingleCommand(“Get-Process”); 

   //打印结果 
   System.out.println(“List Processes:”+ response.getCommandOutput()); 

使用相同的PowerShell会话执行一个或多个命令 

   PowerShell powerShell = null; 
   try { 
       //创建PowerShell会话(我们可以在同一个会话中执行多个命令) 
       powerShell = PowerShell.openSession(); 

       //在PowerShell会话中执行命令 
       PowerShellResponse response = powerShell.executeCommand(“Get-Process”); 

       //打印结果 
       System.out.println(“List Processes:”+ response.getCommandOutput()); 

       //在同一个PowerShell会话中执行另一个命令 
       response = powerShell.executeCommand(“Get-WmiObject Win32_BIOS”); 

       //打印结果 
       System.out.println(“BIOS信息:”+ response.getCommandOutput()); 
   } catch(PowerShellNotAvailableException ex){ 
       //当PowerShell在系统中不可用时处理错误 
       //也许以其他方式       尝试? 
   } finally { 
//始终关闭PowerShell会话以释放资源 
       .if(powerShell!= null) 
         powerShell.close(); 
   } 

配置jPowerShell会议 

我们可以轻松配置jPowerShell会话: 

    通过项目在项目的类路径中创建jpowershell.properties文件,并设置要覆盖的变量。 
    通过调用,使用可以链接到PowerShell中调用的映射。 

例如: 

    //在等待命令终止时设置超时而不是10(默认值) 
    Map <String,String> myConfig = new HashMap <>(); 
    myConfig.put(“MAXWAIT”,“30000”); 
    response = powerShell.configure(myConfig).executeCommand(“Get-WmiObject Win32_BIOS”); 

可以在jPowerShell中配置的三个变量是: 

maxThreads:池中使用的最大线程数.3是最佳和默认值 

waitPause: -个循环池之间的缓冲时间为响应默认。值为10 

maxWait:命令执行的最大等待时间(毫秒)。默认值为10000 

remoteMode:当我们在远程执行命令时,它应该为真。否则执行将在超时结束。 
执行PowerShell脚本 

为了执行PowerShell脚本,建议使用executeScript( )方法而不是executeCommand(): 

   PowerShellResponse response = null; 
   try { 
       //创建PowerShell会话 
       PowerShell powerShell = PowerShell.openSession(); 
       //增加超时以给脚本足够的时间来完成 
       Map <String,String> config = new HashMap <String,String>(); 
       config.put(“MAXWAIT”,“80000”); 

       //执行 
       脚本响应= powerShell.configuration(config).executeScript(“./ myPath / MyScript.ps1”); 

       //如果脚本
       System.out.println(“脚本输出:”+ response.getCommandOutput())打印结果; 
   } catch(PowerShellNotAvailableException ex){ 
       //当PowerShell在系统中不可用时处理错误 
       //也许以其他方式       尝试? 
   } finally { 
//始终关闭PowerShell会话以释放资源 
       .if(powerShell!= null) 
         powerShell.close(); 
   } 


PowerShell中打开一个会话会话,一个会话对象可以执行多个脚本命令,站点验证连通性的办法,直接执行SCMMServer判断返回的IsConnect是否为真 
获取VM,获取主机,GetCluster,GetNetWork 
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值