2013-01-08 35 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"