• 玩转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"

    结果如下图所示:

  • 相关阅读:
    git
    代理上网时git不能直接连接远程库的解决办法
    Python程序练习2--模拟三级菜单
    python os模块常用文件操作方法
    Python程序练习1-模拟用户登录验证
    牛牛牛图片胡乱下载
    oozie 4.1.0与4.2.0版本问题BUG
    hbase 协处理器,实现group by,distinct函数
    Js公共操作类 之 String 扩展方法介绍(一)
    CSS3之伪元素选择器和伪类选择器
  • 原文地址:https://www.cnblogs.com/jackson0714/p/4177395.html
Copyright © 2020-2023  润新知