繁体   English   中英

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

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

我正在尝试在AD中创建计算机系统列表,我想将其保存到文本文件中,然后使用该文本文件检索系统信息,即品牌,型号,制造商,序列号等。

我不是想一劳永逸地解决所有这些问题,而是以为我会先处理查询系统信息,但是第一个问题是我可以从文本文件中读取内容,但是它仅显示来自第一个服务器的信息,然后停止,然后我会已将其设置为(或尝试将其设置为)导出为CSV,但它创建了CSV文件,但没有有关CSV文件的信息。 同样在某些时候,我也需要对标题进行排序。

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

任何帮助将不胜感激,也许应该提到我对PowerShell还是相当陌生的,但是猜测您将通过阅读上面的内容来完成工作:)

您需要在foreach循环之外定义$Computer 除此之外,您还希望在循环内收集每台计算机的所有系统信息字符串:

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

您不应该使用Write-Host作为产生数据的方法,它仅用于调试目的。 而是在您的foreach循环内输出一个字符串,这样,它将被正确地收集到您的results变量中。 接下来,您要遍历一台以上的计算机,但要获取的唯一文件是c:\\computers.txt等,您不会针对由其名称标识的任何远程计算机查询Get-CIMInstance 解决方法:首先,从computers.txt内容中逐一获取计算机名称,然后通过提供-ComputerName作为Get-CIMInstance的参数,对该计算机上的CIM实例执行一系列远程请求。 最后,将输出收集为单个字符串(如果有的话,最好使用它来简化进一步的解析),并将其用作脚本块的输出。

$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

注意,我在字符串的任何地方都使用了“ $(表达式)”构造,它简化了从许多表达式的结果中构造单个字符串的语法。

暂无
暂无

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

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