2013-01-08 38 views
1

對於Powershell的V2日誌文件輸出沒有捕捉所有的數據

我有下面的代碼(它只是一團代碼,我一直在某款)。整個事情都有寫到主機的地方,我被問到是否可以把它放到日誌文件中。使用這個鏈接我按照步驟,並嘗試其他的東西負載,但我不能讓所有的輸出工作:

Create Log File in Powershell

很多的事情是不會出現在日誌文件 - 它只是空白的日誌文件,但顯示了絲網即輸出繼電器:

  1. 任何WMI調用輸出的 - 即

    @(GET-WmiObject可以-Class Win32_OperatingSystem |選擇鈣 -f($ .FreePhysicalMemory/1mb)}},@編號, BuildNumber,CountryCode,CSDVersion,CSName,InstallDate, @ {Name =「Physical Memory Free」; Expression = {「{0:N1} GB」 {Name =「Free Param Files」; Expression = {「{0:N1} GB」-f($ .FreeSpaceInPagingFiles/1mb)}}, @ {Name =「Free Virtual Memory」; Expression = {「 {0:N1} GB「 -f($ _。FreeVirtualMemory/1mb)}} |格式列表)

  2. 那些剛剛運行的任何命令:即net localgroup administrators

有很多的還有很多,但現在看來似乎是大多都WMI調用。

這裏是有問題的代碼部分:

網絡部分:

$forward = nslookup $computername 


$reverse = [System.Net.Dns]::GetHostByName($computername) | select -Expa AddressList | select -Expa ipaddresstostring | % { nslookup $_ } 

LogWrite "Doing forward lookup: " 
$forward 
LogWrite `r`n 
LogWrite "Doing reverse lookup: " 
$reverse 

#$computername = gc env:computername 
#$serverName = SV180515 

$NicConfig = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $computername 
$myCol = @() 
ForEach ($Nic in $NicConfig) 
{ 
    If ($Nic.IPAddress -ne $null) 
    { 
     $myObj = "" | Select-Object Description, DHCPEnabled, IPAddress, IPSubnet, DefaultIPGateway, DNSServers, WINSServers, NICModel, SpeedDuplex 
     $myObj.Description = $Nic.Description 
     $myObj.DHCPEnabled = $Nic.DHCPEnabled 
     $myObj.IPAddress = $Nic.IPAddress 
     $myObj.IPSubnet = $Nic.IPSubnet 
     $myObj.DefaultIPGateway = $Nic.DefaultIPGateway 
     $myObj.DNSServers = $Nic.DNSServerSearchOrder 
     $myObj.WINSServers = $Nic.WINSPrimaryServer,$Nic.WINSSecondaryServer 
     $registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $computername) 
     $baseKey = $registry.OpenSubKey("SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}") 
     $subKeyNames = $baseKey.GetSubKeyNames() 
     ForEach ($subKeyName in $subKeyNames) 
     { 
      $subKey = $baseKey.OpenSubKey("$subKeyName") 
      $ID = $subKey.GetValue("NetCfgInstanceId") 
      If ($ID -eq $Nic.SettingId) 
      { 
       $componentID = $subKey.GetValue("ComponentID") 
       If ($componentID -match "ven_14e4") 
       { 
        $myObj.NICModel = "Broadcom" 
        $requestedMediaType = $subKey.GetValue("RequestedMediaType") 
        $enum = $subKey.OpenSubKey("Ndi\Params\RequestedMediaType\Enum") 
        $myObj.SpeedDuplex = $enum.GetValue("$requestedMediaType") 
       } 
       ElseIf ($componentID -match "ven_8086") 
       { 
        $myObj.NICModel = "Intel" 
        $SD = $subKey.GetValue("*SpeedDuplex") 
        $enum = $subKey.OpenSubKey("Ndi\Params\*SpeedDuplex\Enum") 
        $myObj.SpeedDuplex = $enum.GetValue("$SD") 
       } 
       ElseIf ($componentID -match "b06bdrv") 
       { 
        $myObj.NICModel = "Broadcom" 
        $SD = $subKey.GetValue("*SpeedDuplex") 
        $enum = $subKey.OpenSubKey("BRCMndi\Params\*SpeedDuplex\Enum") 
        $myObj.SpeedDuplex = $enum.GetValue("$SD") 
       } 
       Else 
       { 
        $myObj.NICModel = "unknown" 
        $myObj.SpeedDuplex = "unknown" 
       } 
      } 
     } 
     $myCol += $myObj 
    } 
} 
$myCol 

WMI位:

#Check for local groups on server 
net localgroup administrators 

#checking event log for errors 
LogWrite "Checking System Event log for errors" 
Get-Eventlog system -newest 2000 | where {$_.entryType -match "Error"} | Format-Table TimeWritten, EventID, Message -auto 

LogWrite ` 


LogWrite "Checking Application Event log for errors" 
Get-Eventlog application -newest 2000 | where {$_.entryType -match "Error"} | Format-Table TimeWritten, EventID, Message -auto 

Get-WMIObject Win32_LogicalDisk | Select SystemName,DriveType,DeviceID,VolumeName,@{Name=」size(GB)」;Expression={「{0:N1}」 -f($_.size/1gb)}},@{Name=」freespace(GB)」;Expression={「{0:N1}」 -f($_.freespace/1gb)}},@{Name=」Percentage(%) Free」;Expression={「{0:0}%」 -f($_.freespace*100/$_.size)}}| Format-Table -AutoSize 

$pagefilesize = Get-WmiObject win32_pagefile | ForEach-Object {$_.FileSize/1gb} 

#LogWrite "Page File is set to"$pagefilesize"GB" 

#check pagefile is systemed managed - if it is set to 0 then it is system managed 
$PageFileSystem = Get-WmiObject Win32_PageFileSetting 

if ($PageFileSystem.MaximumSize -eq "0") 
{ 
LogWrite "Page File is System Managed and set to"$pagefilesize"GB" 
} 
else 
{ 
LogWrite "*********ERROR - Page File is not System Mangaged*********" 
} 

回答

2

運行powershell.exe -file script.ps1 *> C^:\ log.txt這個應該寫日誌文件中的所有輸出和錯誤

或者,你可以使用start-tr腳本:

start-transcript -path c:\temp\mylog.txt 
.\myscript.ps1 
stop-transcript 
+0

'*>'只適用於PowerShell v3,它仍然不會捕獲write-host/Console.WriteLine輸出。但如果你使用V3,這是我所知道的最好的解決方案。 –

+0

部分工作...我現在得到所有的信息,但格式非常糟糕。它將每個輸出彼此相鄰,而不是放在一個新行上......即「此腳本正在Server2047計算機上運行說明:TESING_SERVER,JAK R05Server屬性:」 - 這應該按行分割 - 一行用於Computer desc和另一行名稱等等。目前這將所有內容放在一行旁邊。 – lara400

+0

抱歉 - 忘記提及 - 此腳本用於Powershell v2 ..我將更新說明 – lara400

1

你有沒有考慮過在函數中收集你的腳本?然後你可以用Out-File -Append來運行腳本中的函數來記錄它。由於寫主機是交互式cmdlet,因此將Write-Host替換爲Write-Output。然後在runco​​mmand中提供logpath。這裏是一個樣本:

無標題2。PS1

param($logpath) 

function test { 
$service = Get-Service | where {$_.status -eq "Running" } 
$service | ft name, status -auto 
Write-output "hey" 
ping 10.0.0.1 

net localgroup 
} 

if($logpath) { 
    #Run functions in script 
    test | Out-File -Append $logpath 
} else { 
    #Run functions in script 
    test 
} 

然後,我在CMD下/蝙蝠運行:

powershell.exe -noprofile -file Untitled2.ps1 -logpath "c:\users\graimer\desktop\testlog.txt" 

或者在PowerShell中:

.\Untitled2.ps1 -logpath "c:\users\graimer\desktop\testlog.txt"