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"

    结果如下图所示:

  • 相关阅读:
    构建前端第8篇之---Webstom搭建ES6运行环境
    方法重写
    继承的成员变量的访问特定
    继承的理解
    总结与新的开始
    python 小案例demo07
    python 小案例demo06
    python 小案例demo05
    python 小案例demo05 升级版石头剪刀布
    python 小案例demo04
  • 原文地址:https://www.cnblogs.com/jackson0714/p/4177395.html
Copyright © 2011-2022 走看看