如何使用 PsCustomObject 获取计算机名称?
How to get computer name with PsCustomObject?
我目前有一个脚本可以 ping 服务器列表并检查每个服务器上的服务 运行 的状态。我想写信给 log.csv.
我想显示哪些计算机处于离线状态并显示哪些服务处于停止状态。
如何使用 PSCustumObject 获取计算机或机器名称? CSV 输出只有一行显示离线,但没有在其前面列出计算机名称。
$serviceList = Get-Content C:\services.txt
$results = Get-Content C:\servers.txt | ForEach-Object {
if (Test-Connection -ComputerName $_ -BufferSize 16 -Count 1 -EA 0 -Quiet) {
foreach ($service in $serviceList) {
if ($s=get-service -computer $_ -name $service -ErrorAction SilentlyContinue)
{
$s | select MachineName, ServiceName, Status, StartType
} else {
# "$_ - Service '$service' does not exist."
}
}
} else {
$status = Write-Output "Offline"
}
[pscustomobject][ordered]@{
Machine = $_
Status = $status
}
}
$results | Export-CSV C:\log.csv -notypeinformation -Append
当您从 Get-Service
输出结果时,您选择 MachineName
:
$s | select MachineName, ServiceName, Status, StartType
然后对于离线计算机,您正在使用 Machine
:
构建一个对象
[pscustomobject][ordered]@{
Machine = $_
Status = $status
}
您需要将自定义对象更新为 MachineName
,以便它与您在上面选择的属性相匹配。
它也需要移动到else{}
(替换$status = Write-Output "Offline"
)所以它只在计算机离线时调用。
然后您将得到您期望的输出:
MachineName ServiceName Status StartType
----------- ----------- ------ ---------
localhost spooler Running Automatic
localhost DusmSvc Running Automatic
localhost DeviceInstall Stopped Manual
noname Offline
更新代码,对不存在的服务进行奖励更新:
$serviceList = Get-Content C:\services.txt
$results = Get-Content C:\servers.txt| ForEach-Object {
if (Test-Connection -ComputerName $_ -BufferSize 16 -Count 1 -EA 0 -Quiet) {
foreach ($service in $serviceList) {
if ($s = get-service -computer $_ -name $service -ErrorAction SilentlyContinue) {
$s | select MachineName, ServiceName, Status, StartType
}
else {
[pscustomobject][ordered]@{
MachineName = $_
ServiceName = $service
Status = "NotFound"
}
}
}
}
else {
[pscustomobject][ordered]@{
MachineName = $_
Status = "Offline"
}
}
}
$results | Export-CSV C:\log.csv -notypeinformation -Append
我目前有一个脚本可以 ping 服务器列表并检查每个服务器上的服务 运行 的状态。我想写信给 log.csv.
我想显示哪些计算机处于离线状态并显示哪些服务处于停止状态。
如何使用 PSCustumObject 获取计算机或机器名称? CSV 输出只有一行显示离线,但没有在其前面列出计算机名称。
$serviceList = Get-Content C:\services.txt
$results = Get-Content C:\servers.txt | ForEach-Object {
if (Test-Connection -ComputerName $_ -BufferSize 16 -Count 1 -EA 0 -Quiet) {
foreach ($service in $serviceList) {
if ($s=get-service -computer $_ -name $service -ErrorAction SilentlyContinue)
{
$s | select MachineName, ServiceName, Status, StartType
} else {
# "$_ - Service '$service' does not exist."
}
}
} else {
$status = Write-Output "Offline"
}
[pscustomobject][ordered]@{
Machine = $_
Status = $status
}
}
$results | Export-CSV C:\log.csv -notypeinformation -Append
当您从 Get-Service
输出结果时,您选择 MachineName
:
$s | select MachineName, ServiceName, Status, StartType
然后对于离线计算机,您正在使用 Machine
:
[pscustomobject][ordered]@{
Machine = $_
Status = $status
}
您需要将自定义对象更新为 MachineName
,以便它与您在上面选择的属性相匹配。
它也需要移动到else{}
(替换$status = Write-Output "Offline"
)所以它只在计算机离线时调用。
然后您将得到您期望的输出:
MachineName ServiceName Status StartType ----------- ----------- ------ --------- localhost spooler Running Automatic localhost DusmSvc Running Automatic localhost DeviceInstall Stopped Manual noname Offline
更新代码,对不存在的服务进行奖励更新:
$serviceList = Get-Content C:\services.txt
$results = Get-Content C:\servers.txt| ForEach-Object {
if (Test-Connection -ComputerName $_ -BufferSize 16 -Count 1 -EA 0 -Quiet) {
foreach ($service in $serviceList) {
if ($s = get-service -computer $_ -name $service -ErrorAction SilentlyContinue) {
$s | select MachineName, ServiceName, Status, StartType
}
else {
[pscustomobject][ordered]@{
MachineName = $_
ServiceName = $service
Status = "NotFound"
}
}
}
}
else {
[pscustomobject][ordered]@{
MachineName = $_
Status = "Offline"
}
}
}
$results | Export-CSV C:\log.csv -notypeinformation -Append