gpt4 book ai didi

powershell - 日志文件输出未捕获所有数据

转载 作者:行者123 更新时间:2023-12-03 01:20:51 25 4
gpt4 key购买 nike

对于Powershell v2

我有下面的代码(这只是我一直在努力的海量代码的一部分)。整个地方到处都有写主机,有人问我是否可以将其放入日志文件。使用此链接,我已按照步骤操作并尝试了其他操作,但是我无法使所有输出正常工作:

Create Log File in Powershell

很多事情都没有出现在日志文件中-在日志文件中只是空白,但是在屏幕上显示了输出-即:

  • 任何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=”Free Space In Paging Files”;Expression={“{0:N1}GB” -f($.FreeSpaceInPagingFiles/1mb)}}, @{Name=”Free Virtual Memory”;Expression={“{0:N1}GB” -f($_.FreeVirtualMemory/1mb)}} | Format-List)

  • 刚刚运行的任何命令:即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*********"
    }

    最佳答案

    运行powershell.exe -file script.ps1 *> c:\ log.txt这应该将所有输出和错误写入日志文件

    或者,您可以使用start-transcript:

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

    关于powershell - 日志文件输出未捕获所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14218403/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com