Domain→OU=Client Computers→OU=Location
Each computers has an extensionAttribute1 value.
I need to get each computer's extensionAttribute1 and export to a CSV file.
I ran below code, but was unable to get it right. Tried few variation with no success.
I ran this first (no error here):
$Computers = Get-ADComputer -Filter * -SearchBase "OU=Location,OU=Client Computers,DC=ABC,DC=ABC1" -Properties *
Then I ran this:
foreach ($Computer in $Computers) {
Get-ADComputer $Computer -Filter * -Properties extensionAttribute5 |
Export-Csv C:\computer_users.csv
}
and got the following error:
Get-ADComputer : A positional parameter cannot be found that accepts
argument
I tried with parenthesis, commas, single quotes, double quotes, … just can't figure it out.
Get-ADComputer $Computer -Filter * ...
is the same as
Get-ADComputer -Identity $Computer -Filter * ...
If you take a look at the documentation you'll see that the parameters -Identity and -Filter are mutually exclusive. Besides, you don't need the loop and the second Get-ADComputer call anyway. Simply select the properties you want from your first Get-ADComputer call and pipe the result to Export-Csv:
$ou = 'OU=Location,OU=Client Computers,DC=ABC,DC=ABC1'
Get-ADComputer -Filter * -SearchBase $ou -Properties extensionAttribute1 |
Select-Object Name, extensionAttribute1, ... |
Export-Csv 'C:\computer_users.csv' -NoType
Related
Ok so I am trying to use a variable to make up a where statement on a get-aduser command.
This is what I am trying to emulate which works:
get-aduser -filter * | where { $_.DistinguishedName -like "*Users,DC*" }
Note the are asterix before and after the User,DC. Not showing for some reason.
So here is what I have in the script which returns nothing:
$SearchBase = (get-addomain).DistinguishedName
$DefaultUsersOU = "*OU=Users," + $SearchBase
get-aduser -Filter * -Properties lastLogonTimeStamp -Searchbase "$($SearchBase)" | where-object { $_.DistinguishedName -like $DefaultUsersOU }
Found a similar article on here Where object as variable but if I apply that thinking to the DistinguishedName field it doesnt work either.
I am trying to run the get-aduser query below and I keep getting the error Get-AdUser Cannot convert to the type system.string. Any idea what might be the problem? TIA
$Base = (Get-ADOrganizationalUnit -Filter {(Name -like "Department")}).DistinguishedName
Get-ADUser -Filter * -SearchBase $Base -Properties Name
I tested this, and I can confirm that if your call to Get-ADOrganizationalUnit returns more than one OU, then the DistinguishedName property will be an array rather than a plain string. So you will need to change your call to Get-ADOrganizationalUnit so that it returns only one.
You can do that by either using the -ResultSetSize parameter to only use the first result:
$Base = (Get-ADOrganizationalUnit -Filter {(Name -like "Department")} -ResultSetSize 1).DistinguishedName
Or change the Filter so that it matches only one OU. I assume you're using -like because you're using a wildcard in your actual code, so you probably just have to be more specific.
Update: If you want users from all the matched OUs, then you can use ForEach-Object:
Get-ADOrganizationalUnit -Filter {(Name -like "Department")} |
ForEach {
Get-ADUser -Filter * -SearchBase $_.DistinguishedName -Properties Name
}
Just getting started with Powershell and I've run into a roadblock. I'm trying to iterate through AD and get a list of all OU's. From there I'm trying to get user account info for each user in each OU. To test I've been able to get the DN for all OU's and output to console but when I try and pass those values to the get-aduser cmdlt it fails.
Here's my code:
import-module activedirectory
$SearchBase = get-adorganizationalunit -filter * -searchbase "ou=users,ou=myUsers,dc=company,dc=local" -Properties CanonicalName | select-object -Property distinguishedName
foreach ($ou in $SearchBase) {
get-aduser -filter * -searchbase $ou -Properties givenName,sn,mail
}
I'm getting the following error message: "The supplied distinguishedName must belong to one of the following partitions..."
I think the issue is that when passing $ou to the get-aduser cmdlt the distinguished name must be enclosed in quotes after -searchbase correct? If so not sure how to go about that. Any help is appreciated.
The issue you are having is you need to expand the property you are selecting. You will notice if you run:
get-adorganizationalunit -filter * -searchbase "ou=users,ou=myUsers,dc=company,dc=local" -Properties CanonicalName | select-object -Property distinguishedName
It will show the parent property:
There are two ways to fix this:
Expand the property in your select statement:
$SearchBase = get-adorganizationalunit -filter * -searchbase "ou=users,ou=myUsers,dc=company,dc=local" -Properties CanonicalName | select-object -ExpandProperty distinguishedName
OR Call the Property in your foreach:
foreach ($ou in $SearchBase) {
get-aduser -filter * -searchbase $ou.distinguishedName -Properties givenName,sn,mail
}
I have made this script to set/change the location attribute of computers in a specific OU.
My variable is ok but when I execute, it is not taking it line by line.
How can I solve this.
Import-module ActiveDirectory
$computers= Get-ADcomputer -Filter * -searchbase "OU=XX,OU=XXX,OU=WXXX,DC=TGE,DC=XX,DC=XX" | ForEach-Object {$_.Name}
Set-ADcomputer -identity "$computers" -Location "buildingA"
Your $Computers is an [Array] Object which means it has a set of objects inside
The Set-ADComputer command cannot process all the array at once, to process each one of them you need to use the 'Foreach' method for example:
Foreach ($Computer in $Computers)
{
Set-ADcomputer -identity $computer -Location "buildingA"
}
Another Approach is add the foreach after the pipe, like this:
Get-ADcomputer -Filter * -searchbase "OU=XX,OU=XXX,OU=WXXX,DC=TGE,DC=XX,DC=XX" | ForEach-Object {Set-ADComputer -identity $_ -Location "buildingA"}
Try piping the output of Get-ADcomputer into Set-ADcomputer:
Get-ADcomputer -Filter * -searchbase "OU=XX,OU=XXX,OU=WXXX,DC=TGE" | Set-ADcomputer -Location "buildingA"
From a primal form with checkbox I get a csv file with some properties.
I want to used these properties with cmdlet get-Adusers. The script is working if I only have only one value in the csv but not with some.
My CSV file is format like this :
"PropertiesSelected";"FilterSelected"
"SN,EmailAddress,CN,SamAccountName"; "DC=myDomain,DC=COM"
For the moment I don't try to used "filterSelected"
I think the problem is that powershell understand "SN,EmailAddress,CN,SamAccountName" as a single value
The command I used is like that :
Import-Csv c:\TempPowerShell\CheckBoxResults.csv -delimiter ";" |
ForEach-Object {
$FilterSelected=$_.FilterSelected
$PropertiesSelected=$_.PropertiesSelected
Get-ADUser -Properties "$PropertiesSelected" -Filter * -SearchScope Subtree -SearchBase "DC=MyDomain,DC=COM" -Server MyServer:3268
}
Thanks a lot for your help, I'm going to be crazy :)
Regards
Julien
You cant use a String with multiple properties as parameter input.
You can get around this by constructing your command as string and then executing it with invoke-expression:
$command="get-aduser -properties $PropertiesSelected -Filter * -SearchScope Subtree -SearchBase "DC=MyDomain,DC=COM" -Server MyServer:3268"
Invoke-expression $command
Regards Paul
ForEach-Object {
$FilterSelected=$_.FilterSelected
$PropertiesSelected=$_.PropertiesSelected
Get-ADUser -Properties $PropertiesSelected.split(",") -Filter * -SearchScope Subtree -SearchBase "DC=MyDomain,DC=COM" -Server MyServer:3268
}