0

一个基本问题 - 但为什么在 Powershell 脚本中 Get-Service 不将列表打印到输出?

    Write-Host "Enumerating installed Services..." -ForegroundColor Green
    Get-Service | Sort-Object status,displayname

现在在脚本中它根本没有输出。

但是,如果我打开一个 powershell 提示符并手动键入它,它就可以正常工作。

完整脚本:

param([string] $server)

$serverSystem = Get-WmiObject -computername $env:computername Win32_ComputerSystem
$serverCPUDetail = Get-WmiObject -ComputerName $env:computername Win32_Processor

switch ($ComputerSystem.DomainRole){
        0 {$ComputerRole = "Standalone Workstation"}
        1 {$ComputerRole = "Member Workstation"}
        2 {$ComputerRole = "Standalone Server"}
        3 {$ComputerRole = "Member Server"}
        4 {$ComputerRole = "Domain Controller"}
        5 {$ComputerRole = "Domain Controller"}
        default {$ComputerRole = "No role available" }
    }


$serverOS = Get-WmiObject -class Win32_OperatingSystem -ComputerName $env:computername

Write-Host "Enumerating  System information..." -ForegroundColor Green
" "
Start-Sleep -s 1
Write-Host "Hostname:" ($env:computername)
Write-Host "Domain:" ($serverSystem.Domain)
Write-Host "Role:" ($ComputerRole)
Write-Host "Operating System:" ($serverOS.Caption)
Write-Host "Service Pack:" ($serverOS.CSDVersion)
$lastBootTime = $serverOS.ConvertToDateTime($serverOS.Lastbootuptime)
Write-Host "Last Boot Time:" $lastBootTime
" "
Write-Host "Enumerating  Hardware information..." -ForegroundColor Green
" "
Start-Sleep -s 1
Write-Host "Model:" ($serverSystem.Model)
Write-Host "Manufacturer:" ($serverSystem.Manufacturer)
Write-Host "CPU - No. of Processors:" ($serverSystem.NumberOfProcessors)
Write-Host "CPU - No. of Cores:" ($serverCPUDetail.NumberofCores)
Write-Host "CPU - No. of Processors, Logical:" ($serverCPUDetail.NumberOfLogicalProcessors)
Write-Host "Memory in GB:" ([math]::round($serverSystem.TotalPhysicalMemory/1024/1024/1024,0))

" "
Write-Host "Enumerating Disk Information..." -ForegroundColor Green
Start-Sleep -s 1
Foreach ($s in $env:computername) {
    Get-WmiObject -Class win32_volume -cn $s |
    Select-Object @{LABEL='Comptuer';EXPRESSION={$s}},DriveLetter, Label,@{LABEL='Free Space (GB)';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}} 
}
" "
Write-Host "Enumerating Network Information..." -ForegroundColor Green
" "
Start-Sleep -s 1 
ForEach($NIC in $env:computername) {
    $intIndex = 1
    $NICInfo = Get-WmiObject -ComputerName $env:computername Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null}
    $caption = $NICInfo.Description 
    $ipAddress = $NICInfo.IPAddress
    $ipSubnet = $NICInfo.IpSubnet 
    $ipGateWay = $NICInfo.DefaultIPGateway 
    $macAddress = $NICInfo.MACAddress 
    Write-Host "Network Card": $intIndex
    Write-Host "Interface Name: $caption"
    Write-Host "IP Addresses: $ipAddress" 
    Write-Host "Subnet Mask: $ipSubnet"
    Write-Host "Default Gateway: $ipGateway"
    Write-Host "MAC: $macAddress"
    $intIndex += 1
}
" "
# Write-Host "Enumerating Software Installations..." -ForegroundColor Green
# "  "
# Get-WmiObject -Class Win32_Product | Select-Object -property Name

Write-Host "Enumerating installed Services..." -ForegroundColor Green
Get-Service | Sort-Object status,displayname
4

1 回答 1

2

您发布的代码很好。您没有展示的其他东西不起作用。

尝试这个:

$script = @"
Write-Host "Enumerating installed Services..." -ForegroundColor Green
Get-Service | Sort-Object status,displayname
"@

$script | Set-Content ./test.ps1
./test.ps1

以上将您的两行写入脚本文件,然后执行脚本。正如您所期望的那样,它会产生所需的输出。

更新 1

看到输入后,我现在可以重现该问题。基本上如果你运行这个:

$a= Get-WmiObject -Class win32_volume -cn $s | 
  Select-Object @{LABEL='Comptuer';EXPRESSION={$s}},DriveLetter, Label,@{LABEL='Free Space (GB)';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}} 
$b = Get-Service | Sort-Object status,displayname
$a
$b

你会发现它不太好用。这里发生的是,默认情况下,Write-Output用于处理结果。Write-Output 在成功管道中写入内容,而 powershell 对数组进行解包。然后,我猜输出系统无法处理 get-service 和 select-object 的输出是不同类型的事实(并且由于展开它们最终在同一个数组中)。

如果您将脚本的最后一行更改为

Get-Service | Sort-Object status,displayname | ft

它会起作用的。但是请注意,如果您要传递脚本的结果,最好在显示之前将其格式化(ft)。基本上,一旦你将它通过管道传输到 ft,除了输出(显示或文件)之外,很少有充分的理由将它传递给其他任何地方。

更新 2

本文解释说,powershell 使用流中的第一个对象来确定如何格式化所有这些对象。这解释了我们正在观察的行为。

于 2013-07-14T22:51:37.697 回答