Reputation: 351
One thing you learn about programming is always dealing with errors. In this situation, my program doesn't have errors, however; nothing seems to print. I simply want my group name and managedby information in 2 columns. Can you explain why nothing outputs?
Import-Module ActiveDirectory
$exportlist = "C:\Temp\managedby.txt"
Clear-Content $exportlist
$Header = `
"Group ID Name" + "|" + "ManagedBy"
$Header | Out-File $exportlist -Append
$list = get-adgroup -properties name, managedby -filter {name -notlike "WA*" -or name -notlike "workstation*"} `
| Select name, managedby
$listing =`
$list.name + "|" + $list.managedby
$listing | Out-File $exportlist -Append
This is what comes out when I run it in PowerShell:
PS F:\> $Header = `
>> "Group ID Name" + "|" + "ManagedBy"
>>
PS F:\> write-host $Header
Group ID Name|ManagedBy
PS F:\> $list = get-adgroup -properties name, managedby -filter {name -notlike "WA*" -or name -notlike "workstation*"} `
>> | Select name, managedby
PS F:\> $listing =`
>> $list.name + "|" + $list.managedby
PS F:\> write-host $listing
|
Looks like $listing
contains nothing?
Upvotes: 0
Views: 69
Reputation: 200283
After thinking some more about it: the most likely scenario is probably that you're using PowerShell v2 and Get-ADGroup
produces more than one result. In PowerShell v2 $list.Name
would not expand to a list of the Name
properties of all array elements, but the (nonexistent) property Name
of the array object, thus resulting in $null
, which is then cast to an empty string during the string concatenation.
Property expansion in PowerShell v2:
PS C:\Temp> $PSVersionTable.PSVersion.ToString()
2.0
PS C:\Temp> $f = Get-ChildItem
PS C:\Temp> $f
Directory: C:\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 03.12.2014 20:38 0 a.txt
-a--- 03.12.2014 20:38 0 b.txt
PS C:\Temp> $f.Name
PS C:\Temp> $f | select -Expand Name
a.txt
b.txt
Property expansion in PowerShell v3:
PS C:\Temp> $PSVersionTable.PSVersion.ToString()
3.0
PS C:\Temp> $f = Get-ChildItem
PS C:\Temp> $f
Directory: C:\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 03.12.2014 20:38 0 a.txt
-a--- 03.12.2014 20:38 0 b.txt
PS C:\Temp> $f.name
a.txt
b.txt
PS C:\Temp> $f | select -expand Name
a.txt
b.txt
With that said, your objective seems to be to export the names and managers of AD groups to a file, separated by |
characters. However, you're trying way too hard. You can easily do it just by using the Export-Csv
cmdlet, which allows you to specify whatever delimiter you like.
Import-Module ActiveDirectory
$exportlist = 'C:\Temp\managedby.txt'
$attr = 'Name', 'ManagedBy'
$fltr = { Name -notlike "WA*" -or Name -notlike "workstation*" }
Get-ADGroup -Properties $attr -Filter $fltr | Select $attr |
Export-Csv $exportlist -NoType -Delimiter '|'
If you want the name of the manager rather than the distinguished name, use a calculated property for resolving it:
Get-ADGroup -Properties $attr -Filter $fltr |
Select Name, @{n='ManagedBy';e={Get-ADObject -Identity $_.ManagedBy | select -Expand Name}} |
Export-Csv $exportlist -NoType -Delimiter '|'
Upvotes: 2