Reputation: 3185
A basic question - but why in a Powershell Script does Get-Service not print the list to the output?
Write-Host "Enumerating installed Services..." -ForegroundColor Green
Get-Service | Sort-Object status,displayname
Now in the script it gives no output at all.
But if I opened a powershell prompt and typed it manually it works fine.
Full Script:
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
Upvotes: 0
Views: 3592
Reputation: 26349
The code that you posted is fine. It's something else that you are not showing that does not work.
Try this:
$script = @"
Write-Host "Enumerating installed Services..." -ForegroundColor Green
Get-Service | Sort-Object status,displayname
"@
$script | Set-Content ./test.ps1
./test.ps1
The above writes your two lines in a script file and then executes the script. As you would expect it produces desired output.
Update 1
Having seen the input, I can now reproduce the problem. Basically if you run this:
$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
you will see that it does not quite work. What happening here is that by default Write-Output is used to process the results. Write-Output writes stuff in the success pipeline, and powershell does unwrapping of the arrays. Then, I'm guessing the output system can't cope with the fact that output from get-service and select-object are of different types (and because of unwrapping they end up in the same array).
If you change the last line of your script to
Get-Service | Sort-Object status,displayname | ft
it will work. However note, that if you are going to pass around the results of your script, you better format it (ft) at the very end right before displaying. Basically, once you piped it to ft there is rarely a good reason to pass it anywhere else but for output (display or file).
Update 2
This article explains, that powershell uses the first object in the stream to determine how to format all of them. This explains the behaviour that we are observing.
Upvotes: 2