【问题标题】:How can I read multiple computer descriptions?如何阅读多个计算机描述?
【发布时间】:2019-11-21 12:47:52
【问题描述】:

我想创建一个从 CSV 文件中读取所有计算机名的脚本。从所有这些中,我想要描述。此外,它应该以单个 CSV 格式导出。

这是我尝试过的,但是...

$path = Split-Path -Parent $MyInvocation.MyCommand.Definition 

$path_import_csv = $path + "\" + "Computernamen.csv"
$path_export_csv = $path + "\" + "Alessio.csv"

$computernames = Import-Csv $path_import_csv

foreach ($computername in $computernames) {
    Get-ADComputer -SearchBase "OU=1,OU=2,OU=3,DC=my,DC=domain" -Properties * |
        Select -Expand description |
        Export-Csv -Path $path_export_csv -Append -Delimiter ";" -NoTypeInformation -Force
}

【问题讨论】:

  • “但是……”什么?你期望你的代码做什么?它的实际作用是什么?
  • foreach 循环从不使用$computername。这是代码中的复制粘贴错误还是实际功能?
  • 您的foreach 循环没有搜索$computername。如果你想这样做,那么你需要Get-AdComputer $computername。如果Computernamen.csv 确实是一个CSV,那么它要么已经有一个标题,要么你需要添加一个。如果它有一个标题,那么$computernames.header 将是访问列表的语法。如果它没有标题,则只需使用Get-Content $path_import_csv 读取文件。如果要导出为CSV,则需要输出带有属性的对象,即select -expand description只输出描述文本,需要变成select description
  • 请展示导入 CSV 的示例,以及您希望在输出 CSV 中看到的内容。
  • 嘿感谢所有的回复!我对powershell真的很陌生,并尝试学习它:D示例IMPORT csv Computernames Computer1 Computer2 Computer3 ...示例EXPORT csv Description Name1 Name2 Name3

标签: powershell csv active-directory


【解决方案1】:

从您的评论中,我了解到文件 Computernamen.csv 根本不是 CSV 文件,而只是一个文本文件,每个文件都位于单独的行中。 在这种情况下,您不使用Import-Csv,而是使用Get-Content 来检索计算机名称数组。

另外(其他人已经明确表示)您根本没有在 foreach 循环中使用$computername 变量,并且通过将-ExpandProperty 开关添加到Select-Object cmdlet,您没有收到对象 具有属性 Description,但只是描述为 string 。 要输出带有Export-Csv 的 CSV,您需要有一个(一系列)对象。

另外,我建议使用 Join-Path cmdlet 创建文件路径,而不是使用 + 连接字符串,这样您就不必担心可能缺少反斜杠。

除了$MyInvocation.MyCommand.Definition,您还可以使用$PSScriptRoot 变量来获取当前脚本路径。 在 Windows PowerShell 2.0 中,此变量仅在脚本模块 (.psm1) 中有效。 从 Windows PowerShell 3.0 开始,它在所有脚本中都有效。

$path            = Split-Path -Parent $MyInvocation.MyCommand.Definition   # or use $PSScriptRoot
$path_import_csv = Join-Path -Path $path -ChildPath 'Computernamen.csv'
$path_export_csv = Join-Path -Path $path -ChildPath 'Alessio.csv'
$computernames   = Get-Content $path_import_csv
$searchBase      = 'OU=1,OU=2,OU=3,DC=my,DC=domain'

$result = foreach ($computername in $computernames) {
    # it is bad practice to use -Properties * if all you need is a small set of properties
    $computer = Get-ADComputer -Filter "Name -eq '$computername'" -SearchBase $searchBase -Properties Name, Description -ErrorAction SilentlyContinue
    # did we find a computer by that name?
    if ($computer) {
        # output an object with the two selected properties to get collected in the $result variable
        $computer | Select-Object Name, Description
    }
    else {
        Write-Host "A computer with name '$computername' does not exist."
    }
}

# output the result on console
$result | Format-Table -AutoSize

# save the result as proper CSV file
$result | Export-Csv -Path $path_export_csv -Delimiter ';' -NoTypeInformation -Force

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多