1

Powershell v2 の場合

私は以下のコードを持っています (これは、私が取り組んできた大量コードのサブセクションにすぎません)。すべての場所に write-host があり、それをログ ファイルに記録できるかどうかを確認するように求められました。このリンクを使用して、手順に従い、他の多くのことを試しましたが、すべての出力を機能させることができません:

Powershell でログ ファイルを作成する

多くのことがログファイルに表示されていません-ログファイルでは空白ですが、画面に出力が表示されます-つまり:

  1. WMI 呼び出し出力のいずれか - すなわち

    @(Get-WmiObject -Class Win32_OperatingSystem | Select Caption, BuildNumber, CountryCode, CSDVersion, CSName, InstallDate, @{Name=”Physical Memory Free”;Expression={“{0:N1}GB” -f($ .FreePhysicalMemory/ 1mb)}}, @{Name="ページング ファイルの空き容量";Expression={"{0:N1}GB" -f($ .FreeSpaceInPagingFiles/1mb)}}, @{Name="空き仮想メモリ"; Expression={“{0:N1}GB” -f($_.FreeVirtualMemory/1mb)}} | Format-List)

  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*********"
}
4

3 に答える 3

2

run powershell.exe -file script.ps1 *> c:\log.txt これにより、すべての出力とエラーがログファイルに書き込まれます

あるいは、 start-transcript を使用できます:

start-transcript -path c:\temp\mylog.txt
.\myscript.ps1
stop-transcript
于 2013-01-08T15:50:12.067 に答える
1

関数でスクリプトを収集することを検討しましたか? 次に、スクリプト内の関数を実行しOut-File -Appendてログに記録できます。write-host はインタラクティブなコマンドレット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/bat で次のように実行します。

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

またはpowershellで:

.\Untitled2.ps1 -logpath "c:\users\graimer\desktop\testlog.txt"
于 2013-01-08T17:10:34.600 に答える
0

別の答えは、スクリプトの実行後にバッファの内容をキャプチャすることです。ここを見てください: http://blogs.msdn.com/b/powershell/archive/2009/01/10/capture-console-screen.aspx

于 2013-01-25T11:19:35.030 に答える