I am new to powershell, I need your help! (function statement)

Welcome Forums General PowerShell Q&A I am new to powershell, I need your help! (function statement)

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 week, 1 day ago.

  • Author
    Posts
  • #182430

    Participant
    Topics: 1
    Replies: 0
    Points: 12
    Rank: Member

    Hi

    I am new to PowerShell, I tried getting answers/help on the internet before arriving here, but no luck. I am working on a script and I want it to perform the following: The script will prompt the user to input an AD group name, If the AD group name matches, the script will then export the specified group to a csv file. However, at this point, the code below does not export any group information to a csv file. One of the requirements for this is that I want to use a function statement, the script works if I use a variable like this: $groupsusers = Get-ADGroup -Identity $nameofgroup. Thanks in advance.

    $prompt = "Enter A Group Name"
    do
    {
    $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
  • #182478

    Participant
    Topics: 1
    Replies: 1552
    Points: 2,700
    Helping Hand
    Rank: Community Hero

    I recommend for you to start to learn the basics of Powershell from scratch with a good book or an online tutorial. The code you posted shows that you probably won't understand the help you can get here or in any other forum yet.

    I think it is beyond the scope of this forum to teach you the fundamentals of Powershell. You cannot learn a complex technology by piecing together some code snippets you found on the internet without understanding what every piece does.

    Regardless of that you can try to find something fitting in the PowershellGallery or in the MicrosoftTechnetScriptGallery ... something like this has been asked a thousand times already.

    You might watch the inventor of Powershell explaining the basics ... Getting Started with Powershell.

  • #182517

    Participant
    Topics: 2
    Replies: 497
    Points: 1,230
    Helping Hand
    Rank: Community Hero

    PSKoans may also be of use.

    For the immediate question, though, you don't need / shouldn't have the ForEach-Object inside the function. You're not providing any input to the cmdlet from the function, so the block will never run. For pipeline input you can use a process { } block instead, but since the function appears to need no pipeline input, probably best to just strip away that layer.

    As for parameters, basically anything you want the function to be able to "see"that comes from outside the function should be included in its parameters.  You will also need to send the values to the function when calling it, not just when defining it. Technically speaking, a function can "see" values outside its scope in some cases, but attempting to locate what's going wrong when this is being used tends to be significantly more complicated and confusing.

     

You must be logged in to reply to this topic.