简体   繁体   English

从AD创建txt文件并查询列出的服务器以获取系统信息并导出到csv

[英]Create txt file from AD and query servers listed to get system information and export to csv

I'm trying to create a list of computer systems in AD, which I would like to save to a text file, and then using the text file retrieve the system information, ie make, model, manufacturer, serial number etc. 我正在尝试在AD中创建计算机系统列表,我想将其保存到文本文件中,然后使用该文本文件检索系统信息,即品牌,型号,制造商,序列号等。

Rather than try and tackle this all in one go I thought I'd do the query system information first, but the first problem is I can read contents from text file, but it only displays the information from first server then stops and secondly I've set it to (or tried to set it to) export to CSV, but it creates the CSV file but no information on the CSV file. 我不是想一劳永逸地解决所有这些问题,而是以为我会先处理查询系统信息,但是第一个问题是我可以从文本文件中读取内容,但是它仅显示来自第一个服务器的信息,然后停止,然后我会已将其设置为(或尝试将其设置为)导出为CSV,但它创建了CSV文件,但没有有关CSV文件的信息。 Also at some point I'm going to need to sort headers too. 同样在某些时候,我也需要对标题进行排序。

New-Item -ItemType Directory -Force -Path C:\Computer_Details
set-location C:\Computer_Details
$results = ForEach ($Computersystem in $Computer)
{
$Computer = Get-Content -path C:\computers.txt
$computerSystem = Get-CimInstance CIM_ComputerSystem
$computerBIOS = Get-CimInstance CIM_BIOSElement
$computerOS = Get-CimInstance CIM_OperatingSystem
$computerCPU = Get-CimInstance CIM_Processor
$computerHDD = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = 'C:'"}
Write-Host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"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 + ", Service Pack: " + $computerOS.ServicePackMajorVersion
"User logged In: " + $computerSystem.UserName
"Last Reboot: " + $computerOS.LastBootUpTime 
$results | Export-Csv ComputerDetails.csv

Any help would be greatly appreciated and probably should mention I'm fairly new to PowerShell, but guessing you'll work that out reading the above :) 任何帮助将不胜感激,也许应该提到我对PowerShell还是相当陌生的,但是猜测您将通过阅读上面的内容来完成工作:)

You need to define $Computer outside the foreach loop. 您需要在foreach循环之外定义$Computer In addition to that, you'd want to gather all the system information strings per computer inside the loop: 除此之外,您还希望在循环内收集每台计算机的所有系统信息字符串:

New-Item -ItemType Directory -Force -Path C:\Computer_Details
set-location C:\Computer_Details

# Get computer names from file
$Computers = Get-Content -path C:\computers.txt

# Loop over each computer name
$results = foreach($Computer in $Computers)
{
    # Set up a remote session to the machine in question
    try{
        $ComputerSession = New-CimSession -ComputerName $Computer

        # Fetch all the information from it
        $computerSystem = Get-CimInstance CIM_ComputerSystem -CimSession $ComputerSession
        $computerBIOS = Get-CimInstance CIM_BIOSElement -CimSession $ComputerSession
        $computerOS = Get-CimInstance CIM_OperatingSystem -CimSession $ComputerSession
        $computerCPU = Get-CimInstance CIM_Processor -CimSession $ComputerSession
        $computerHDD = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = 'C:'" -CimSession $ComputerSession
        $Sucess = $true
    } catch {
        Write-Warning "Unable to fetch information from $Computer"
        $Success = $false
    }
    if($Sucess){
        # Create a new object containing all the details you want
        New-Object psobject -Property @{
            "ComputerName"     = $Computer
            "Manufacturer"     = $computerSystem.Manufacturer
            "Model"            = $computerSystem.Model
            "Serial Number"    = $computerBIOS.SerialNumber
            "CPU"              = $computerCPU.Name
            "HDD Capacity"     = "{0:N2}GB" -f ($computerHDD.Size/1GB)
            "HDD Space"        = "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) 
            "HDD Free"         = "{0:N2}GB" -f ($computerHDD.FreeSpace/1GB)
            "RAM"              = "{0:N2}GB" -f ($computerSystem.TotalPhysicalMemory/1GB)
            "Operating System" = "{0}, Service Pack: {1}" -f $computerOS.caption,$computerOS.ServicePackMajorVersion
            "User logged In"   = $computerSystem.UserName
            "Last Reboot"      = $computerOS.LastBootUpTime
        }
    } else {
        New-Object psobject -Property @{
            "ComputerName"     = $Computer
        }
    }        
}
# $results is now an array of the objects created above, export it to a CSV file!
$results | Export-Csv ComputerDetails.csv

You should not use Write-Host as a method to produce data, it's only good for debugging purposes. 您不应该使用Write-Host作为产生数据的方法,它仅用于调试目的。 Instead output a single string inside your foreach-loop as a result, this way it will be properly gathered in your results variable. 而是在您的foreach循环内输出一个字符串,这样,它将被正确地收集到您的results变量中。 Next, you want to iterate through more than a single computer, but instead you get the only file there is, c:\\computers.txt and more, you don't query Get-CIMInstance against any remote computer identified by its name. 接下来,您要遍历一台以上的计算机,但要获取的唯一文件是c:\\computers.txt等,您不会针对由其名称标识的任何远程计算机查询Get-CIMInstance To resolve: First, get the computer name out of computers.txt content, one by one, then execute a series of remote requests to CIM instances on that computer by providing -ComputerName as an argument to Get-CIMInstance . 解决方法:首先,从computers.txt内容中逐一获取计算机名称,然后通过提供-ComputerName作为Get-CIMInstance的参数,对该计算机上的CIM实例执行一系列远程请求。 And finally, collect the output as a single string (this is preferred to simplify further parsing, if there is any) and use it as an output of your script block. 最后,将输出收集为单个字符串(如果有的话,最好使用它来简化进一步的解析),并将其用作脚本块的输出。

$computers=get-content -path C:\computers.txt
$results = ForEach ($Computer in $Computers)
{
    try {
    # remote computer might not be accessible! 
    $computerSystem = Get-CimInstance CIM_ComputerSystem -computername $computer
    $computerBIOS = Get-CimInstance CIM_BIOSElement -computername $computer
    $computerOS = Get-CimInstance CIM_OperatingSystem -computername $computer
    $computerCPU = Get-CimInstance CIM_Processor -computername $computer
    $computerHDD = Get-CimInstance Win32_LogicalDisk -computername $computer -Filter "DeviceID = 'C:'"}
    # once you've collected the data, prepare output string
    $output="System Information for: $($computerSystem.Name)`r`n"
    $output+="Manufacturer: $($computerSystem.Manufacturer)`r`n"
    $output+="Model: $($computerSystem.Model)`r`n"
    $output+="Serial Number: $($computerBIOS.SerialNumber)`r`n"
    $output+="CPU: $($computerCPU.Name)`r`n"
    $output+="HDD Capacity: $("{0:N2}" -f ($computerHDD.Size/1GB)) GB`r`n"
    $output+="HDD Space: $("{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size)) Free ($({0:N2}" -f ($computerHDD.FreeSpace/1GB)) GB)"
    $output+="RAM: $({0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)) GB`r`n"
    $output+="Operating System: $($computerOS.caption), Service Pack: $($computerOS.ServicePackMajorVersion)`r`n"
    $output+="User logged In: $($computerSystem.UserName)`r`n"
    $output+="Last Reboot: $($computerOS.LastBootUpTime)`r`n"
    } catch {
    $output="$computer is not accessible!"
    }
    # once built, output the string into the variable
    $output
}
$results | Out-File ComputerDetails.txt -Encoding UTF8

Note, I have used a "$(expression)" construction everywhere in the strings, it simplifies syntax of building a single string out of many expressions' results. 注意,我在字符串的任何地方都使用了“ $(表达式)”构造,它简化了从许多表达式的结果中构造单个字符串的语法。

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

相关问题 Powershell查询AD以导出.txt文件中所有用户的电子邮件? - Powershell Query AD to export emails of all users in .txt file? 从txt文件中获取数据并将其导出为CSV - Fetch data from txt file and export it to CSV 将信息从txt文件提取到CSV - Extract information from txt file to CSV AD 查询到 .txt 文件以删除现有用户的 .csv 中的行 - AD Query into .txt file to remove line in .csv of existing users 从 txt 文件中提取名字和姓氏并使用它从 AD 中获取用户名并将其导入 csv - Extracting first and last names from txt file and using it to get username from AD and import it into csv 从 powershell 中的多个 cmdlet 创建所有已禁用 AD 用户的 csv 文件和邮箱 Output 信息 - Create csv file of all disabled AD users with mailboxes Output information from multiple cmdlets in powershell 让 pssessions 为 txt 文件中列出的多个服务器正确循环的问题 - Issue getting pssessions to loop properly for multiple servers listed in a txt file 从通配符组中获取广告组成员和属性,然后导出到csv - Get Ad group members and properties from wildcard groups then export to csv 无法从空的.txt文件执行export-csv - Unable to do export-csv from an empty .txt file 从.txt文件中提取数据,并将其导出到.csv文件#powershell - Extract Data from .txt files and export it to a .csv file #powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM