0

How do you pipe information into a Get-ADcomputer command in PowerShell?

I'd like to import a list of computers and descriptions from a CSV file and use the CSV data to gather additional information for the computers listed in the CSV file. However, when I attempt to use information from the CSV file, the Get-Command runs using the string ($_.description) instead of the description info from CSV file (value of $_.description).

My code:

$csv = Import-Csv C:\Temp\computers.csv -Header @("name","info","description")
foreach ($line in $csv) {
    Get-AdComputer -LDAPFilter "(description = $_.description)" -Properties * -SearchScope Subtree -SearchBase "OU=computers,DC=example" 
}
Devinput
  • 53
  • 1
  • 7
  • What are the headings in your csv? – Itchydon Sep 07 '17 at 21:47
  • 1
    `foreach` is one word. And since you are using `foreach` and not `ForEach-Object` with a pipeline, you will use the defined variable. e.g. `_.description` -> `$line.description` (Also $computer would be more descriptive than $line) – BenH Sep 07 '17 at 21:48

1 Answers1

1

Try (I am assuming one of your headings in the csv is name description):

$csv = Import-Csv C:\Temp\computers.csv
foreach ($line in $csv) {
    $description = $line.description
    Get-AdComputer -LDAPFilter "(description = $description)" -Properties * -SearchScope Subtree -SearchBase "OU=computers,DC=example" 
}
Itchydon
  • 2,572
  • 6
  • 19
  • 33
  • I experience the same issue with $line.description. Instead of the value of $line.description what actually executes is the string $line.description. – Devinput Sep 07 '17 at 21:52
  • 1
    `$_` not being populated by a `foreach` loop is only one half of the problem. The other half is that property expansion inside a string doesn't work. – Ansgar Wiechers Sep 07 '17 at 21:54
  • THANK YOU @AnsgarWiechers! Property expansion inside a string was the issue. Created a variable for the description and that resolved it. – Devinput Sep 07 '17 at 22:12