即時編寫腳本以從本地和遠程機器獲取服務。我必須拆分本地和遠程機器的wmi調用(遠程機器需要不同的憑據)。我想輸出它們作爲System.Object。我如何爲system.object的輸出創建一個函數?Powershell:我如何爲system.object的輸出創建一個函數?
繼承人的代碼我迄今爲止:
$objServicecol = @()
# how do i get AddService object back
Function AddServiceObjects
{
ForEach ($Service in $Services)
{
$objService = New-Object System.Object
$objService | Add-Member -MemberType NoteProperty -Name SystemName -Value $Services.SystemName
$objService | Add-Member -MemberType NoteProperty -Name Name -Value $Services.Name
$objService | Add-Member -MemberType NoteProperty -Name StartMode -Value $Services.StartMode
$objService | Add-Member -MemberType NoteProperty -Name StartName -Value $Services.StartName
$objService | Add-Member -MemberType NoteProperty -Name Status -Value $Services.Status
#$objServiceCol += $objService
AddServiceObjects += $objService
}
}
# Executes local WMI
If ($Servers -contains "localhost")
{
$Services = Get-WMIObject Win32_Service -ComputerName "localhost" | Select-Object SystemName, Name, StartMode, StartName, Status
AddServiceObjects $Services
}
...#execute remote wmi...
感謝下面的建議後代碼更清潔:-) – resolver101 2012-04-06 20:31:48