2013-07-14 47 views
0

一個基本問題 - 但爲什麼在PowerShell腳本中Get-Service不會將列表打印到輸出中?PowerShell腳本中的獲取服務

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 
+0

你想做什麼?正如你在你的問題中提到的那樣,它工作正常,問題是什麼? –

+0

基本上在腳本中它不會打印任何東西,但是如果我打開一個手動的powershell提示符,它可以正常工作。 – PnP

+0

這兩行代碼沒有問題。它們在腳本內部或外部產生輸出。這是別的東西,你沒有顯示,這是造成問題。請發佈您的腳本,並請張貼如何調用它。 –

回答

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用於處理結果。寫 - 輸出在成功管道中寫入東西,而powershell則展開陣列。然後,我猜測輸出系統無法應對來自get-service和select-object的輸出具有不同類型的事實(並且由於解包它們最終位於同一陣列中)。

如果你改變你的腳本的最後一行

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

它會奏效。但是請注意,如果您要傳達腳本的結果,則最好在顯示之前將其格式化爲最後的格式(英尺)。基本上,一旦你將它傳輸到ft,除了輸出(顯示或文件)外,幾乎沒有什麼理由將它傳遞給其他任何地方。

更新2

This article解釋說,這PowerShell使用的第一個對象的數據流中,以確定如何格式化所有的人。這解釋了我們正在觀察的行爲。

+0

我已經添加了完整的腳本,但我看不到其他任何會影響該命令的東西,它在它自己的空間中 – PnP

相關問題