【问题标题】:Outputting "foreach" results to CSV (with columns intact) (listing all services on all servers in AD)将“foreach”结果输出到 CSV(列完整)(列出 AD 中所有服务器上的所有服务)
【发布时间】:2012-01-27 03:50:51
【问题描述】:

使用以下代码:

   # List all servers
    $servers = Get-QADComputer -OSName *server*,*hyper-v*

    # Looping through the servers
    foreach ($server in $servers) {
    $i++
    $intSize = $intSize + $server.Length

    # Writing statusbar
    Write-Progress -activity "Scanning servers . . ." -status "Scanned: $i of $($servers.length) - Working on $($server.name)" -percentComplete (($i / $servers.length)  * 100)

    # Pinging the server in Powershell-way!
    if (Test-Connection -ComputerName $server.name -count 1 -Quiet ) {
        echo $server.name
    }
}
$respondingservers = $i - $notresponding

我正在循环浏览我的 270 个 AD 服务器的列表,并计算谁在线、运行 snmp、wmi 等。但我想做的不是指定要检查的服务并获取每台服务器上的完整列表。我想将其输出到 CSV,以便我可以将其导入 Excel 并按服务名称等进行排序。这当然意味着它需要以合理的方式输出。

例如,如果服务器 X 正在运行 DNS SERVER,并且它在第 4 列中结束,那么以下所有 DNS SERVER 结果都应该在我的电子表格中的同一列中结束。

我要求太多还是可以做到?现在我什至无法输出到 CSV,所以任何帮助都将不胜感激。

【问题讨论】:

    标签: powershell csv active-directory foreach


    【解决方案1】:

    您可以像这样为您的服务器导出服务信息:

        # Pinging the server in Powershell-way!
        if (Test-Connection -ComputerName $server.name -count 1 -Quiet ) {
            echo $server.name
            $services = $services, (get-wmiobject -ComputerName $server.name win32_service | select systemname, name, started)
        }
    }
    # export information about services to csv file
    $services | Export-Csv services.csv -NoTypeInformation
    

    这将生成一个具有以下结构的 csv 文件:

    "systemname","name","started"
    "SERVER01","AeLookupSvc","False"
    "SERVER02","AeLookupSvc","False"
    ...
    

    然后可以使用此 csv 在 Excel 中创建数据透视表(行 = 系统名称,列 = 名称)以显示您想要的信息。

    【讨论】:

    • 你好,这个脚本会生成一个大的 CSV 文件吗?因为,我在列出所有用户时遇到了问题。而且我不能把找到的所有东西都放在 CSV 中。
    猜你喜欢
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多