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

时间:2015-06-11 08:50:24

标签: powershell 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来说还是新手,但猜测你会在阅读上述内容后继续工作:)

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

您不应该使用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

注意,我使用了" $(表达式)"在字符串中无处不在的构造,它简化了从许多表达式中构建单个字符串的语法。结果