我正在编写一个脚本来从本地和远程机器获取服务。我不得不将 wmi 调用拆分为本地和远程机器(远程机器需要不同的凭据)。我想将它们输出为 System.Object。如何为 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...