question

EricMoteberg-0327 avatar image
0 Votes"
EricMoteberg-0327 asked RichMatheisen-8856 commented

Powershell Help to list Security Groups that match a pattern for each user in a csv list

I am trying to get Security Groups for a list of members that starts with a certain sequence, e.g. "Mumbo - Jumbo -*".

I have a list of samaccountnames in a CSV file, and I want the script to find and export to a CSV only the Security Groups that match that pattern.

I can list all of the Security Groups for each member, but that's too much. Also, I want to have the samaccountname listed for each one.

Any help would be appreciated.

windows-server-powershellwindows-active-directory
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered

I think I got your requirements right. I haven't run this code, but it looks to be substantially right:

 Get-Content ListOfUsers.txt |
     ForEach-Object{
         $u = $_
         (Get-ADUser $_).memberof |
             ForEach-Object{
                 if ($_ -match "^CN=Mumbo - Jumbo -.+,"){
                     [PSCustomObject]@{
                         Name = $u
                         Group = (Get-ADGroup -Identity $_).samaccountname
                     }
                 }
             }
     } | Export-Csv SecGroupAndUser.csv -NoTypeInformation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered RichMatheisen-8856 commented

Hello,

In addition if you get an error message you will probably need to set the execution policy before you can run your script.

Set-ExecutionPolicy
Change the user preference for the execution policy of the shell.

Syntax
Set-ExecutionPolicy [-executionPolicy] Policy
{ Unrestricted | RemoteSigned | AllSigned | Restricted | Default | Bypass | Undefined}
[[-Scope] ExecutionPolicyScope ] [-Force]
[-whatIf] [-confirm] [CommonParameters]

Key
-ExecutionPolicy Policy
A new execution policy for the shell.

    Valid values:
        
    Restricted
    Do not load configuration files or run scripts.
    This is the default.
        
    AllSigned
    Require that all scripts and configuration files be signed
    by a trusted publisher, including scripts that you write on the
    local computer.
        
    RemoteSigned
    Require that all scripts and configuration files downloaded
    from the Internet be signed by a trusted publisher.
        
    Unrestricted
    Load all configuration files and run all scripts.
    If you run an unsigned script that was downloaded from the
    internet, you are prompted for permission before it runs.

    Bypass
    Nothing is blocked and there are no warnings or prompts.

    Undefined
    Remove the currently assigned execution policy from the
    current scope. This parameter will not remove an execution
    policy that is set in a Group Policy scope.

-Force
Suppress all prompts.
By default, Set-ExecutionPolicy displays a warning whenever the
execution policy is changed.

 -Scope ExecutionPolicyScope
    The scope of the execution policy.

    Valid values:
      Process       Affect only the current PowerShell process.
      CurrentUser   Affect only the current user.
      LocalMachine  Affect all users of the computer.

    To remove an execution policy from a particular scope, set the
    execution policy for that scope to Undefined.

-WhatIf
Describe what would happen if you executed the command without actually
executing the command.

-Confirm
Prompt for confirmation before executing the command.
In order to change the Execution policy, you must be running PowerShell As Administrator.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why would it be necessary to change the execution policy?

0 Votes 0 ·