简体   繁体   中英

How can I read multiple computer descriptions?

I want to create a script which reads all the computernames from a CSV file. And from all of these, I want the description. Also it should be exported in a single CSV.

This is what I tried but...

$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
}

From your comment I gather that file Computernamen.csv is not a CSV file at all, but just a text file with computer names each on a separate line. In that case, you do not use Import-Csv , but Get-Content to retrieve an array of computer names.

Also (others already made that clear) you are not using the $computername variable in the foreach loop at all AND by adding the -ExpandProperty switch to the Select-Object cmdlet, you are not receiving an object with property Description, but just the description as string. For outputting a CSV with Export-Csv , you need to have a (series of) objects.

Also, I would recommend using the Join-Path cmdlet for creating file paths instead of contatenating string with + so you don't have to worry about possible missing backslashes.

Instead of $MyInvocation.MyCommand.Definition you can also use the $PSScriptRoot variable to get the current script path. In Windows PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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