1

新手问题在这里。我在下面有这个用于 powershell 的脚本,它为我提供了来自 AD 的 PC 信息。但我一直无法将结果导出到 csv。还如何获取 ipv4 ip 地址而不包括 ipv6?并说如果 PC 处于离线状态,是否可以再给我一列以显示如果它处于离线状态,它会返回 RPC 服务器不可用或离线。在此处输入图像描述 感谢帮助。

$ArrComputers = Get-Content "C:\Temp\computers-systeminfo.txt"
#Specify the list of PC names in the line above. "." means local system

Clear-Host
foreach ($Computer in $ArrComputers) 
{
    $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
    $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
    $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
    $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
    $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
    $computerIPv4 = Get-WmiObject WIn32_NetworkAdapterConfiguration -Computer $Computer


        write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
        "-------------------------------------------------------"
        "Manufacturer: " + $computerSystem.Manufacturer
        "Model: " + $computerSystem.SystemFamily
        "Machine Name: " + $computerSystem.DNSHostName
        "IP Address: " + $computerIPv4.Ipaddress
        "Serial Number: " + $computerBIOS.SerialNumber
        "CPU: " + $computerCPU.Name
        "HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
        "HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
        "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
        "Operating System: " + $computerOS.caption + " "+ $computerOS.BuildNumber

        "User logged In: " + $computerSystem.UserName
        "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
        ""
        "-------------------------------------------------------"
}
4

2 回答 2

1

将变量分配给 foreach 并将结果输出到 CSV 文件。以下代码未经测试,但应该可以工作。使用 PscustomObject 将使数据结构化,并以一种简单的方式处理数据。要了解更多信息,请参阅 https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.2

要在线或离线查找 PC,您可以使用 Test-Connection 并将结果保存在 pscustomobject 中。

    $ArrComputers = Get-Content "C:\Temp\computers-systeminfo.txt"
#Specify the list of PC names in the line above. "." means local system

Clear-Host
$output = foreach ($Computer in $ArrComputers) {
    $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
    $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
    $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
    $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
    $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
    $computerIPv4 = Get-WmiObject WIn32_NetworkAdapterConfiguration -Computer $Computer

    [PSCustomObject]@{
        "Manufacturer"     = $computerSystem.Manufacturer
        "Model"            = $computerSystem.SystemFamily
        "Machine Name"     = $computerSystem.DNSHostName
        "IP Address"       = $computerIPv4.Ipaddress
        "Serial Number"    = $computerBIOS.SerialNumber
        "CPU"              = $computerCPU.Name
        "HDD Capacity"     = "{0:N2}" -f ($computerHDD.Size / 1GB) + "GB"
        "HDD Space"        = "{0:P2}" -f ($computerHDD.FreeSpace / $computerHDD.Size) + "Free(" + "{0:N2}" -f ($computerHDD.FreeSpace / 1GB) + "GB)"
        "RAM"              = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory / 1GB) + "GB"
        "Operating System" = $computerOS.caption + " " + $computerOS.BuildNumber
        "User logged In"   = $computerSystem.UserName
        "Last Reboot"      = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
    }
}
$output | Export-csv C:\Temp\outputfilename.csv -NoTypeInformation
于 2022-01-06T05:11:32.607 回答
0

很多问题:

首先对于循环开始时的 RPC 错误尝试建立一个cimsession

$cimSession = New-CimSession -ComputerName $Computer
if ($cimSession -eq $null)
{
  write-host "Pas Glop $Computer"
  continue
}

至少

对于 IPV4:

$IpAddressV4 = (Get-NetIPConfiguration -CimSession $cimSession | Where-Object { $_.IPv4DefaultGateway -ne $null -and  $_.NetAdapter.Status -ne "Disconnected"  }).IPv4Address.IPAddress

将所有内容放入 CSV 文件中:

$lines = @()
foreach ($Computer in $ArrComputers) 
{
  $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
  $line = New-Object -TypeName PSCustomObject -Property @{"Computer" = $Computer;"Manufacturer" = $($computerSystem.Manufacturer)}
  $lines += $line
}
$lines | Export-Csv "PathToYourCSVFile" -NoTypeInformation
于 2022-01-06T05:14:59.927 回答