0
votes

I have a bat file that starts up a PS1 script, the PS1 script is supposed to go to AD and export the names listed in a Security Group. It runs fine and exports fine, however the data is incorrect, almost as if it is exporting some unknown data that I'm not sure where from. Here is my powershell script:

$uni = Read-host 'Your username'

$SG = Read-host 'Security Group'

start-sleep -s 3

powershell.exe get-adgroupmember "$SG" | export-csv -path "C:\users\$uni\desktop\members.csv"

When opening the CSV, it shows similar:

TYPE System.String

Length 79 79 63 17 34 79 26 54 1

Am I needing to declare or import a pssession? if so, what is the configurationname for active directory?

2
Remove powershell.exe and add -NoTypeInformation switch: get-adgroupmember "$SG" | export-csv -path "C:\users\$uni\desktop\members.csv" -NoTypeInformation.Alexander Obersht
I tried what you suggested, however I get a message within' cmd.exe, stating 'get-adgroupmember' is not recognized, so essentially it's trying to run the powershell command from cmd.exe rather than powershell. It stopped giving this error when I left powershell.exe in the begining.DHB
You're not getting it from cmd.exe. You're getting a PowerShell exception - because Get-ADGroupmember is not a stock cmdlet. You need to import ActiveDirectory module first (and probably install RSAT to be able to do that).Alexander Obersht
copy that, I do have RSAT installed, and my AD Module is installed, but I understand exactly what you're getting at. Perhaps run an import in the PS1 script? I did try, powershell.exe import-module ActiveDirectory | get-adgroupmember "$SG" yada yada... however get the same error. Is there another way to import the module? I need it all on the same line essentially.DHB
You sound terribly confused. First, like I said, you don't need to call powershell.exe inside a PowerShell script - drop it. Second, you shouldn't pipe output from Import-Module to Get-ADGroupMember. Instead make them separate commands. Perhaps this example can illustrate what I mean?Alexander Obersht

2 Answers

0
votes

Most likely you're getting a collection of objects in your data. You have to expand or join the data before passing to Export-CSV

See the following articles for some idea:

1) http://blog.millersystems.com/powershell-exporting-multi-valued-attributes-via-export-csv-cmdlet/ 2) http://www.msexchange.org/kbase/ExchangeServerTips/ExchangeServer2010/Powershell/WhydoIgetSystem.StringwhenusingGet-MessageTrackingLogandexportingtoaCSV.html

The articles give the following examples:

1) change the following

Get-TransportServer | Get-MessageTrackingLog -ResultSize -Start "11/28/2011" -Sender [email protected] | Select * | Export-Csv D:\Reports\Sent_Nuno.csv -NoType

to

Get-TransportServer | Get-MessageTrackingLog -ResultSize -Start "11/28/2011" -Sender [email protected] | Select {$_.Recipients}, {$_.RecipientStatus}, * | Export-Csv D:\Reports\Sent_Nuno.csv -NoType

2) change the following

Get-QADUser seth -IncludeAllProperties | select name, proxyaddresses | Export-Csv .seth-nojoin.csv - See more at: http://blog.millersystems.com/powershell-exporting-multi-valued-attributes-via-export-csv-cmdlet/#sthash.JCccKLui.dpuf

to

Get-QADUser seth -IncludeAllProperties | select name, @{Name=’proxyAddresses';Expression={[string]::join(“;”, ($_.proxyAddresses))}} | Export-Csv .seth-all_proxyaddresses.csv - See more at: http://blog.millersystems.com/powershell-exporting-multi-valued-attributes-via-export-csv-cmdlet/#sthash.JCccKLui.dpuf

You'll need to import the ActiveDirectory module for the get-ADGroupMember cmdlet, but I assume you've already gotten past that part as you have output instead of errors. I don't have the ability to test currently as I can't import the module (need RSAT or AD Management Gateway Service installed), but I think you would need to insert a select command in your pipeline (to enumerate the problematic data fields) that looks something like this:

powershell.exe get-adgroupmember "$SG" | Select {$_.nameOfVariableReturningSystemString}, * | export-csv -path "C:\users\$uni\desktop\members.csv
0
votes

Alexander Obersht answered my question through comments, although duct_tape_coder you are also correct. Thanks for the help guys, appreciated.