玩转PowerShell第二节——【利用PsExec进行远程调用】-技术&分享

简介: 玩转PowerShell第二节——【利用PsExec进行远程调用】-技术&分享


概述

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

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

1.首次运行PowerShell.exe

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

图片.png

2.查看PowerShell帮助

图片.png

图片.png

3.常用参数

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

-u 远程机器的域用户名称Domain\UserName

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

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

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

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

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

4.例子程序

1) 直接调用

\PsExec \\$MachineIP -u $DomainUser -p $Password -i 0 -s -w $WorkPath powershell "$WorkPath\Scripts\test.ps1 $Param"

注意:

1.远程机器磁盘上需要有“$WorkPath\Scripts\test.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 "$WorkPath\Scripts\test.ps1 $Param"
5          }

 

5.实例

test.ps1的路径:D:\01_360Cloud\02_MyBlogs\PowerShell\Source\test.ps1

test.ps1脚本

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

 

PsExec.exe路径:D:\01_360Cloud\02_MyBlogs\PowerShell\Source

键入以下命令

1 $WorkPath = "D:\01_360Cloud\02_MyBlogs\PowerShell\Source"
2 cd $WorkPath
3 .\PsExec -i -s -w $WorkPath powershell ".\test.ps1"

 

结果如下图所示:

图片.png

 




相关文章
|
4月前
|
运维 Linux Windows
[运维技术]PowerShell中实现一个最基本的日志器logger
[运维技术]PowerShell中实现一个最基本的日志器logger
71 1
|
监控 测试技术 Windows
玩转PowerShell第三节——【SCOM Maintenance Mode】-技术&分享
玩转PowerShell第三节——【SCOM Maintenance Mode】-技术&分享
116 0
|
监控 Java
玩转PowerShell第一节——【后台任务处理】-技术&分享
玩转PowerShell第一节——【后台任务处理】-技术&分享
346 0
一起谈.NET技术,Powershell简介及其编程访问
  这个工具可以单独使用,完全可以取代cmd.exe。例如如下:   但它的功能远不止于此,例如我们可以很容易地获取所有的进程名称:   再来看一个,下面这个例子是获取当前正在运行的服务列表。(可以用条件很方便地筛选):   除此之外,Powershell还支持定制,例如微软很多产品都提供了专门的Powershell插件(典型的有:SQL Server,SharePoint Server, Exchange Server等)。
1019 0
|
4天前
|
Windows
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
37 10
|
4月前
|
存储 Ubuntu Linux
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
115 0
|
10月前
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
79 0
|
JavaScript Windows
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
|
Windows
使用PowerShell获取Windows当前锁屏壁纸
使用PowerShell获取Windows当前锁屏壁纸 如果原始图片丢了,用这段代码就可以提取当前锁屏壁纸了!
159 0