简体   繁体   English

Powershell:如何将 AD PC 结果导出到 CSV

[英]Powershell: How to export AD PC results to CSV

newbie question here.新手问题在这里。 I have this script below for powershell that gives me the PC information from AD.我在下面为 powershell 提供了这个脚本,它为我提供了来自 AD 的 PC 信息。 but i've been having trouble exporting the results to csv.但我一直无法将结果导出到 csv。 also how do just get the ipv4 ip address and dont include ipv6?还如何获得 ipv4 ip 地址而不包括 ipv6? and say if the PC is offline, can it just give me another column to show that if it's offline, it returns like RPC server unavailable or Offline.并说如果 PC 处于离线状态,是否可以再给我一列以显示如果它处于离线状态,它会返回 RPC 服务器不可用或离线。 enter image description here Appreciate the help.在此处输入图像描述感谢帮助。

$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)
        ""
        "-------------------------------------------------------"
}

assign a variable to foreach and output the results to a CSV file.将变量分配给 foreach 和 output 将结果分配给 CSV 文件。 The below code is not tested but should work.以下代码未经测试,但应该可以工作。 Using PscustomObject will make the data structured and a easy way to handle data.使用 PscustomObject 将使数据结构化,并以一种简单的方式处理数据。 To learn more please refer https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.2要了解更多信息,请参阅https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.2

To find a PC online or offline you can use Test-Connection and save the result in pscustomobject.要在线或离线查找 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

Much questions:很多问题:

First for RPC error at the begining of the loop try to establish a cimsession :首先对于循环开始时的 RPC 错误尝试建立cimsession

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

At leasr至少

For IPV4:对于 IPV4:

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

To Put everythin in a CSV file:要将所有内容放入 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

thanks for the response.感谢您的回复。

I've added the things in my script.我已经在我的脚本中添加了这些东西。 but the IPv4 new line shows nothing.但 IPv4 新行没有显示任何内容。 for the csv file the columns were mixed up and not as what i put it in the script?对于 csv 文件,列被混淆了,而不是我在脚本中放入的内容? is there a way that the columns exported follow the sequence i wrote in the script?有没有办法让导出的列按照我在脚本中写的顺序? also the export-csv line is it possible to automatically run it rather than i have to click enter to run the line? export-csv 行是否可以自动运行它而不是我必须单击 Enter 来运行该行? also the exported column for IPAddress is exported as System.Object[] IPAddress 的导出列也导出为 System.Object[]

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

$lines = @()

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

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

$line = New-Object -TypeName PSCustomObject -Property @{
"Machine Name" = $computerSystem.DNSHostName;
"Manufacturer" = $($computerSystem.Manufacturer);
"Model" = $($computerSystem.SystemFamily);
"IP Address" = $($computerIPv4.Ipaddress);
"Serial Number" = $($computerBIOS.SerialNumber);
"CPU" = $($computerCPU.Name);
"HDD Capacity" = "{0:N2}" -f $($computerHDD.Size/1GB) + "GB";
"HDD Space" = "{0:N2}" -f $($computerSystem.TotalPhysicalMemory/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)); 
}

  $lines += $line


        write-host "System Information for: " $computerSystem.Name - 

BackgroundColor DarkCyan "-------------------------------------------------------" "Machine Name: " + $computerSystem.DNSHostName "Manufacturer: " + $computerSystem.Manufacturer "Model: " + $computerSystem.SystemFamily "IP Address: " + $computerIPv4.Ipaddress "IPv4: " + $IpAddressV4.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 BackgroundColor DarkCyan "------------------------------------------------------------ --------" "机器名称:" + $computerSystem.DNSHostName "制造商:" + $computerSystem.Manufacturer "型号:" + $computerSystem.SystemFamily "IP 地址:" + $computerIPv4.Ipaddress "IPv4: " + $IpAddressV4.Ipaddress "序列号:" + $computerBIOS.SerialNumber "CPU:" + $computerCPU.Name "硬盘容量:" + "{0:N2}" -f ($computerHDD.Size/1GB) + " GB" "硬盘空间:" + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " 空闲 (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB ) + "GB)" "RAM:" + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB" "操作系统:" + $computerOS.caption + " "+ $computerOS.BuildNumber

        "User logged In: " + $computerSystem.UserName
        "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
        ""
        "-------------------------------------------------------"
}
$lines | Export-Csv "C:\Temp\computers-systeminfo.csv" -NoTypeInformation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM