添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I need help with the code below. I want the script to perform the following: prompt the user for an AD group name, if the group name is found, export the group members to a CSV file. one of the requirements is that I must include a function statement. Thank you in advance.

The code works if I use a variable like the following example: $groupsusers = Get-ADGroup -Identity $nameofgroup, instead of the function statement. However, I don't want to use a variable, I want to implement a function statement.

$prompt = "Enter A Group Name"
$nameofgroup = Read-Host $prompt
until(!$(dsquery Group-Object $nameofgroup; $prompt = "Group 
'$nameofgroup' was not found, try again"))
$nameofgroup = Read-Host $prompt
function GetGroupInfoToCsv (#what parameters go here?){
ForEach-Object{
$settings = @{ Group = $_.DistinguishedName; Member = $null }
$_| Get-ADGroupMember |
ForEach-Object{
    $settings.Member = $_.DistinguishedName
    New-Object PsObject -Property $settings
GetGroupInfoToCsv | Export-Csv .\GroupMembers.csv -NoTypeInformation
                your  code does not make any sense ... and at least one part will not run at all. what do you want the function to do? have you working code to put in the function?
                    – Lee_Dailey
                Oct 12 '19 at 4:18
                    

You could combine the asking for user input and returning the info into the same function. Something like this:

function Get-GroupMembers {
    $prompt = "Enter A Group Name. Press Q to quit"
    # create an endless loop
    while ($true) {
        Clear-Host
        $answer = Read-Host $prompt
        # if the user has had enough, exit the function
        if ($answer -eq 'Q') { return }
        # try and find one or more AD groups using the answer as (part of) the name
        $group = Get-ADGroup -Filter "Name -like '*$answer*'"
        # if we have found something, exit the while loop and start enumerating the members
        if ($group) { break }
        $prompt = "Group '$answer' was not found, try again. Press Q to quit"
    # you only get here if Get-ADGroup found one or more groups
    $group | ForEach-Object {
        # output a PSObject with the properties you are after
        $members = $_ | Get-ADGroupMember
        foreach ($member in $members) {
            [PsCustomObject]@{
                'Group'  = $_.DistinguishedName
                'Member' = $member.DistinguishedName
# call the function
$groupinfo = Get-GroupMembers
# test if the function returned anything. 
# the user could have cancelled of the group had no members to output
if ($groupinfo) {
    Write-Host "Adding $($groupinfo.Count) items to the CSV file"
    # without -Append, you would overwrite your CSV file..
    $groupinfo | Export-Csv .\GroupMembers.csv -NoTypeInformation -Append
else {
    Write-Host 'Nothing found..'

As you can see, I have changed the function name so it complies with the Verb-Noun convention in PowerShell.

improve this answer Theo, thanks for providing your knowledge/solution to my initial question. Your code executes perfectly. Question for you, how can I implement this line or something similar: Get-ADgroup -filter * | sort name | select name before the script asks to enter a group name? Thanks in advance. – ovictech20 Oct 13 '19 at 6:01 @ovictech20 You could put this line in at the very top of the function: $allgroups = Get-ADGroup -Filter * and next in the While loop change $group = Get-ADGroup -Filter "Name -like '*$answer*'" into: $group = $allgroups | Where-Object { $_.Name -like "*$answer*" } – Theo Oct 13 '19 at 9:56

Okay so from my interpretation it seems that you want to use a function to pipe to the Export-Csv function as the data source. From what I am seeing in the code above, you are piping the GetGroupInfoToCsv function when exporting the csv but the function is not returning anything, therefore there is no data that is being stored in the csv.

You would need to return data in that function, for example, an array of objects. Here is an example how to do this:

function test () 
    return @([pscustomobject]@{test="test1"})
test | Export-Csv -NoTypeInformation -Path [YOUR_PATH_HERE]
        
            
                    improve this answer