zoukankan      html  css  js  c++  java
  • 玩转PowerShell第二节——【利用PsExec进行远程调用】-技术&分享

    概述

      PowerShell用的最多的地方就是远程调用,在远程机器上执行脚本,监控远程机器的状态,如NLB状态,EventLog,SqlServer DataBase状态等。

      本篇将讲到用PsExec.exe这个工具进行远程调用。

    1.首次运行PowerShell.exe

    双击PowerShell.exe,同意licence条款。

     

    2.查看PowerShell帮助

     

    3.常用参数

    computer   计算机名称,如果省略,则在本机执行,如果是\*,则在域里面的所有的机器上执行脚本

    -u 远程机器的域用户名称DomainUserName

    -p 远程机器的域用户密码Password

    -i 指定远程机器运行某程序所在的桌面会话session,如果未指定,则在控制台会话中执行

    -s 指定在系统账户中运行程序

    -w 指定运行程序的工作路径

    program 指定运行的程序,如PowerShell,cmd

    4.例子程序

    1) 直接调用

    1 .PsExec \$MachineIP -u $DomainUser -p $Password -i 0 -s -w $WorkPath powershell "$WorkPathScripts	est.ps1 $Param"

    注意:

    1.远程机器磁盘上需要有“$WorkPathScripts est.ps1”这个文件;

    2.远程机器上不需要有PsExec.exe文件。

    2)在Job中调用

    1 $Job = Start-Job -ArgumentList $WorkPath,$MachineIP,$DomainUser,$Password,$WorkPath,$Param -ScriptBlock {
    2          param($WorkPath,$MachineIP,$DomainUser, $Password,$WorkPath,$Param)
    3          Set-Location $WorkPath
    4          .PsExec \$MachineIP -u $DomainUser -p $Password -h -i 0 -s -w $WorkPath powershell "$WorkPathScripts	est.ps1 $Param"
    5          }

    5.实例

    test.ps1的路径:D:1_360Cloud2_MyBlogsPowerShellSource est.ps1

    test.ps1脚本

    1 Write-Host "abc"
    2 Read-Host a

    PsExec.exe路径:D:1_360Cloud2_MyBlogsPowerShellSource

    键入以下命令

    1 $WorkPath = "D:1_360Cloud2_MyBlogsPowerShellSource"
    2 cd $WorkPath
    3 .PsExec -i -s -w $WorkPath powershell ".	est.ps1"

    结果如下图所示:

  • 相关阅读:
    POJ 1611 The Suspects(并查集)
    POJ 2485 Highways(最小生成树Prim算法)
    POJ 1062 昂贵的聘礼(最短路径Dijkstr的变形)
    SDUT 2374 || HDU 1803Cylinder(计算几何求体积)
    HDU 1804 || SDUT 2375 Deli Deli(简单题)
    POJ 3041 Asteroids(二分图的最大匹配)
    HDU 1802 || SDUT 2373 Black and white painting(规律)
    POJ 1035 Spell checker(字符串简单匹配)
    POJ 3080 Blue Jeans(KMP模式匹配)
    js中反斜杠\的一些研究
  • 原文地址:https://www.cnblogs.com/jackson0714/p/4177395.html
Copyright © 2011-2022 走看看