原文地址:http://powershell.com/cs/blogs/tips/archive/2010/06/10/create-remoting-solutions.aspx
原文:
Whenever you use WMI (Get-WMIObject) to retrieve information, it's a snap to turn a local solution into a remotable solution. You can just add the parameter -Computername to Get-WMIObject.
In a previous tip, you learned how to examine physical memory. Here's a wrapper function that makes this code run locally as well as remotely (provided that you have sufficient access privileges):
function Get-Memory($computername = 'localhost') {
$memorytype = "Unknown", "Other", "DRAM", "Synchronous DRAM", "Cache DRAM",
"EDO", "EDRAM", "VRAM", "SRAM", "RAM", "ROM", "Flash", "EEPROM", "FEPROM",
"EPROM", "CDRAM", "3DRAM", "SDRAM", "SGRAM", "RDRAM", "DDR", "DDR-2"
$formfactor = "Unknown", "Other", "SIP", "DIP", "ZIP", "SOJ", "Proprietary",
"SIMM", "DIMM", "TSOP", "PGA", "RIMM", "SODIMM", "SRIMM", "SMD", "SSMP",
"QFP", "TQFP", "SOIC", "LCC", "PLCC", "BGA", "FPBGA", "LGA"
$col1 = @{Name='Size (GB)'; Expression={ $_.Capacity/1GB } }
$col2 = @{Name='Form Factor'; Expression={$formfactor[$_.FormFactor]} }
$col3 = @{Name='Memory Type'; Expression={ $memorytype[$_.MemoryType] } }
$col4 = @{Name='ComputerName'; Expression=[Scriptblock]::Create("'$computername'")}
Get-WmiObject Win32_PhysicalMemory -computername $computername |
Select-Object BankLabel, $col1, $col2, $col3, $col4
}
翻译:
无论什么时候通过WMI(Get-WMIObject)来处理信息,把 本地的方案变成远程执行的方案是很简单的。只需要在Get-WMIObject中加一个参数:-Computername.
在上一篇中介绍了如何查看物理内存信息,以下方法是对其进一步封装,使其能在本地运行远程的代码(需要确定在目标机器上有足够的权限)
function Get-Memory($computername = 'localhost') {
$memorytype = "Unknown", "Other", "DRAM", "Synchronous DRAM", "Cache DRAM",
"EDO", "EDRAM", "VRAM", "SRAM", "RAM", "ROM", "Flash", "EEPROM", "FEPROM",
"EPROM", "CDRAM", "3DRAM", "SDRAM", "SGRAM", "RDRAM", "DDR", "DDR-2"
$formfactor = "Unknown", "Other", "SIP", "DIP", "ZIP", "SOJ", "Proprietary",
"SIMM", "DIMM", "TSOP", "PGA", "RIMM", "SODIMM", "SRIMM", "SMD", "SSMP",
"QFP", "TQFP", "SOIC", "LCC", "PLCC", "BGA", "FPBGA", "LGA"
$col1 = @{Name='Size (GB)'; Expression={ $_.Capacity/1GB } }
$col2 = @{Name='Form Factor'; Expression={$formfactor[$_.FormFactor]} }
$col3 = @{Name='Memory Type'; Expression={ $memorytype[$_.MemoryType] } }
$col4 = @{Name='ComputerName'; Expression=[Scriptblock]::Create("'$computername'")}
Get-WmiObject Win32_PhysicalMemory -computername $computername |
Select-Object BankLabel, $col1, $col2, $col3, $col4
}
笔记:
复习get-wmiobject。
又一种在函数中声明参数的方法。