[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载代码] [PowerShell每日技巧]远程执行代码(20131203)

In a domain environment, PowerShell remoting is working almost out of the box. All you might have to do is enable Remoting on target machines (beginning with Server 2012, PowerShell remoting is enabled by default for Administrators).

In PowerShell 3.0, to enable remoting manually, that's all (Administrator privileges required):

PS> Enable-PSRemoting -SkipNetworkProfileCheck -Force


You do not need to configure anything on the client side (the machine that is going to send commands).

Next, any Administrator can send PowerShell code to the enabled machine and have it execute. This example would list all the PowerShell-related processes from the target machine:
  1. $code =
  2. {
  3.     Get-Process -Name powershell*, wsmprovhost -ErrorAction SilentlyContinue
  4. }
  5. $list = 'server1', 'w2k12-niki', 'pc11box'
  6. Invoke-Command -ScriptBlock $code #-ComputerName $list
复制代码
When you run the code as-is, Invoke-Command runs the script block stored in $code on your own machine.

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    323      25    36392      36980   201     0.45   2060 powershell
    270      22    26208      35512   184     0.41   5520 powershell


It reports all running instances of the PowerShell console, the ISE PowerShell editor, and any hidden PowerShell remoting sessions initiated from someone else on your machine.

Once you uncomment the -ComputerName parameter, the code will run on all of the machines listed in $list variable. Make sure they exist and have remoting enabled. When you receive data from remote computers, PowerShell automatically adds a "PSComputerName" property with the name of the computer that sent back the information.

http://powershell.com/cs/blogs/tips/archive/2013/12/03/executing-code-remotely.aspx

返回列表