-5

I have to create script which lists every group that user, specified by name belongs and shows name of specified user(only local groups and accounts). Specified username should be only argument in script. If we don't declare username, script should use name of user running script. If we declare username which isn't current in system, script shows nothing so there won't be error.

It's my first script homework, I understand how basic programing algorithms work. Unfortunately I'm not familiar with scripting in powershell. I would be very glad if someone could lend a hand and write script or show some directed tutorials.

Dixon
  • 36
  • 1
  • 5
  • 1
    what have you tried so far? some code to show you have _tried_ something would be lovely ... [*grin*] – Lee_Dailey Jan 08 '19 at 23:37
  • I've wasted some time searching for commands. Apparently Get-ADPrincipalGroupMembership is not the way. Now I'm just learning powershell syntax. – Dixon Jan 08 '19 at 23:50
  • Since this is homework, hasn't your teacher provided you with materials to learn how to program in powershell? Please include what you have tried so far so we can help.Include whats going wrong and what you are expecting to happen so we can help you learn. This isn't a complete my homework for me site. – Shadowzee Jan 09 '19 at 00:19
  • Yeah, I knew some people will get me wrong like I just want to get my homework done :D. In fact we don't have any materials and didn't have any lecture about doing scripts in powershell. So I'm asking silly questions. Sorry :/ – Dixon Jan 09 '19 at 00:37

1 Answers1

2

As for …

Apparently Get-ADPrincipalGroupMembership is not the way

… that's not really a valid statement and there are several Q&A's on your use case on this very site. Basically, it's just something like this.

# Get users with base properties and their group membership, display user and group name
 ForEach ($TargetUser in (Get-ADUser -Filter *))
 {
 "`n" + "-"*12 + " Showing group membership for " + $TargetUser.SamAccountName
 Get-ADPrincipalGroupMembership -Identity $TargetUser.SamAccountName | Select Name
 }

# Results

 ------------ Showing group membership for Administrator
Name     
----     
Domain Users     
Administrators     
...

------------ Showing group membership for Guest
Domain Guests     
Guests

Update for OP

I used the cmdlet to explain what you were using. If you are on PowerShell v5, there are already local group cmdlets for this.

Get-Command -Name *LocalUser*

# Results

CommandType     Name

Cmdlet          Disable-LocalUser
Cmdlet          Enable-LocalUser
Cmdlet          Get-LocalUser
Cmdlet          New-LocalUser
Cmdlet          Remove-LocalUser
Cmdlet          Rename-LocalUser
Cmdlet          Set-LocalUser 


Get-Command -Name *LocalGroup*

# Results

CommandType     Name

Cmdlet          Add-LocalGroupMember
Cmdlet          Get-LocalGroup
Cmdlet          Get-LocalGroupMember
Cmdlet          New-LocalGroup
Cmdlet          Remove-LocalGroup
Cmdlet          Remove-LocalGroupMember
Cmdlet          Rename-LocalGroup
Cmdlet          Set-LocalGroup

Then doing something like this...

Clear-Host
$LocalUserName = Read-Host -Prompt 'Enter a username'

# If no user is passed, list all
If($LocalUserName -eq '')
{ 
    ForEach($GroupName in Get-LocalGroup)
    {
        Get-LocalGroupMember -Group "$($GroupName.Name)" | 
        Select @{n='GroupName';e={$($GroupName.Name)}},Name
    } 
}
Else
{
    # process only the user passed
    Get-LocalGroup | 
    %{
        If(Get-LocalGroupMember -Group "$($_.Name)" -Member $LocalUserName -ErrorAction SilentlyContinue)
        {
            [PSCustomObject]@{
                GroupName = $_.Name
                Username = $LocalUserName
            }
        }
    }
}

If you are on lower versions, the you can use the PowerShellGallery.com module, or use ADSI directly. There are lots of articles and samples for this on this site and all over the web.

Example:

LocalUserManagement 3.0 a module that performs various local user management functions

See also:

Managing Local User Accounts with PowerShell - Part 1

postanote
  • 15,138
  • 2
  • 14
  • 25
  • I can only use commands inside Microsoft.PowerShell.LocalAccounts module. Thank you anyway. – Dixon Jan 09 '19 at 00:54