понедельник, 22 февраля 2021 г.

PowerShell script to copy groups from one user to another +

There is a useful PowerShell script that is written by friend of mine, talented helpdesk/support person.
He obviously should work in programming field.

I think that the functions of the script are obvious from the picture, 
but the most interesting is by far the possibility to copy groups of one user to another.
It can be useful when creating a new user that should be similar to some existing user.


Here is the script itself.

=START OF SCRIPT=

function form 

    {

    Add-Type -AssemblyName System.Windows.Forms

    Add-Type -AssemblyName System.Drawing

    

     # Build Form

    $form = New-Object System.Windows.Forms.Form

    $form.Text = 'Data Entry Form'

    $form.Size = New-Object System.Drawing.Size(550,300)

    $form.StartPosition = 'CenterScreen'


    #Please enter TZ

    $label = New-Object System.Windows.Forms.Label

    $label.Location = New-Object System.Drawing.Point(10,20)

    $label.Size = New-Object System.Drawing.Size(280,20)

    $label.Text = 'Please enter some value:'

    $form.Controls.Add($label)


    #no coment

    $nf = '

    (\__/)

    (="."=)

    E[:]||||[:]З

    (")_(")'


    #Member of:

    $label2 = New-Object System.Windows.Forms.Label

    $label2.Location = New-Object System.Drawing.Point(340,20)

    $label2.Size = New-Object System.Drawing.Size(280,20)

    $label2.Text = 'Member of:'

    $form.Controls.Add($label2)


    #your username :

    $label2 = New-Object System.Windows.Forms.Label

    $label2.Location = New-Object System.Drawing.Point(10,130)

    $label2.Size = New-Object System.Drawing.Size(280,20)

    $label2.Text = 'uSeRnAmE'

    $form.Controls.Add($label2)


    #search box input

    $inBox = New-Object System.Windows.Forms.TextBox

    $inBox.Location = New-Object System.Drawing.Point(10,40)

    $inBox.Size = New-Object System.Drawing.Size(260,200)

    $form.Controls.Add($inBox)


    #enter user source

    $ussour = New-Object System.Windows.Forms.TextBox

    $ussour.Location = New-Object System.Drawing.Point(320,180)

    $ussour.text="source user"

    $ussour.Size = New-Object System.Drawing.Size(100,10)

    $form.Controls.Add($ussour)


    #enter user target 

    $ustar = New-Object System.Windows.Forms.TextBox

    $ustar.Location = New-Object System.Drawing.Point(320,210)

    $ustar.Text="target user"

    $ustar.Size = New-Object System.Drawing.Size(100,10)

    $form.Controls.Add($ustar)

    

    #box for username output

    $outBox = New-Object System.Windows.Forms.RichTextBox

    $outBox.Location = New-Object System.Drawing.Point(10,155)

    $outbox.font = "arial,10"

    $outBox.Multiline = $true

    $outBox.ScrollBars ="vertical"

    $outBox.Size = New-Object System.Drawing.Size(300,100)

    $outbox.AutoSize = $true

    $form.Controls.Add($outBox)

 

    #box for group 

    $memberofbox = New-Object System.Windows.Forms.TextBox

    $memberofbox.Location = New-Object System.Drawing.Point(320,40)

    $memberofbox.MultiLine = $True

    $memberofbox.ScrollBars = "Vertical"

    $memberofbox.Size = New-Object System.Drawing.Size(200,130)

    $form.Controls.Add($memberofbox)


     # Add search user Button

    $Button = New-Object System.Windows.Forms.Button

    $Button.Location = New-Object System.Drawing.Size(15,75)

    $Button.Size = New-Object System.Drawing.Size(90,23)

    $Button.Text = "search user"

    $Form.Controls.Add($Button)


    # sync groups of users

    $syButton = New-Object System.Windows.Forms.Button

    $syButton.Location = New-Object System.Drawing.Size(440,180)

    $syButton.Size = New-Object System.Drawing.Size(90,53)

    $syButton.Text = "sync groups of users "

    $Form.Controls.Add($syButton)


    # search printer by IP from printer server

    $prButton = New-Object System.Windows.Forms.Button

    $prButton.Location = New-Object System.Drawing.Size(210,75)

    $prButton.Size = New-Object System.Drawing.Size(100,23)

    $prButton.Text = "find printer by IP"

    $Form.Controls.Add($prButton)


    # Add search last log on Button

    $llButton = New-Object System.Windows.Forms.Button

    $llButton.Location = New-Object System.Drawing.Size(115,75)

    $llButton.Size = New-Object System.Drawing.Size(90,53)

    $llButton.Text = "Last logon + -  When Created "

    $Form.Controls.Add($llButton)


    # Add search group Button

    $gButton = New-Object System.Windows.Forms.Button

    $gButton.Location = New-Object System.Drawing.Size(15,105)

    $gButton.Size = New-Object System.Drawing.Size(90,23)

    $gButton.Text = "search group"

    $Form.Controls.Add($gButton)


    #add chekbox

    $chekbox=New-Object System.Windows.Forms.CheckBox

    $chekbox.Location=New-Object System.Drawing.Size(320,20)

    $chekbox.Size=New-Object System.Drawing.Size(15,15)

    $chekbox.Checked = $false

    $form.controls.Add($chekbox)


    #keyboard enter and  esc

    $form.KeyPreview=$True

    #enter

    $form.add_keydown({if ($_.keycode -eq "Enter" ) {$button.PerformClick() } } )


    $form.add_keydown({if ($_.virtualkeycode -eq 38 ) {$gbutton.PerformClick() } } )

    #esc

    $form.add_keydown({if($_.keycode -eq "Escape") {$form.Close() } } )


    $prButton.add_click(

        {

            $ip = $inBox.Text

            $pr=get-WmiObject -class Win32_printer -ComputerName 10.28.28.165,10.28.28.160 | Select-Object -Property  shareName, comment

            foreach($printer in $pr) 

                {

                    if ($printer.comment -eq $ip) 

                        {

                            $outBox.Lines= $printer.shareName

                         }

                }

        }

    )


    #sync group's users 

    $syButton.add_click(

        {

        $usersource = $ussour.Text 

        $usertarget= $ustar.text

        $memof=Get-ADPrincipalGroupMembership $usersource

        $outBox.Forecolor="red"

        $outBox.Lines = "add this groups manauly to user:"

        #set user's group " test123 "

        for ($i=0; $i -le $memof.name.Count-1; $i++)

            {

            #if this NOT Distribution​Group​ and NOT Domain Users

            if ($memof.name[$i] -notmatch "\*" -and $memof.name[$i] -notmatch "Domain Users")

                {

                $q=$memof.name[$i]

                $memberofbox.Appendtext("{0}`n" -f $q)

                Add-ADPrincipalGroupMembership -Identity $usertarget -MemberOf $memof.name[$i]

                }

            else

                {

                $noadd= $memof.name[$i]

                $outBox.Appendtext("{0}`n" -f $noadd)

                }

            }

        }

                        )

    

    #serch last logon date

    $llButton.add_click(

        {

        $x=$inbox.Text

        $x = $x.Trim()

        if($x)             

            {

                $lld = Get-ADUser -LDAPFilter "(sAMAccountName=$x)"

                if($lld)

                    {

                        $out=Get-ADUser -Identity “$x” -Properties “LastLogonDate”,"whenCreated"

                        $outBox.Lines = 'MM/DD/YYYY', $out.LastLogonDate, $out.whenCreated

                    }

                else {$outBox.Lines = "user not found"}

            }

            else {$outBox.Lines = "user not found"}

        }

                        )



    #add group group buton event

    $gButton.add_click(

        {

        $x = $inBox.Text

        if ($x)

            {

                $out=get-adgroup -Filter "name -like '*$x*'" -Properties * | Select-Object name

                $outBox.lines = $out.name

             } 

        else 

            {

                [System.Windows.MessageBox]::Show('Enter something','error')

            }

        }

                        )


    #Add Button event 

    $Button.Add_Click(

        {

        $outbox.Clear()

        $x = $inBox.Text

        if ($x -match '^\d+$')

            {    <#if entering numbers #>

                $out=Get-AdUser -Filter * -Properties postalCode, postOfficeBox, SamAccountName, Enabled | Where-Object {$_.postalCode, $_.postOfficeBox -like $x -or $_.SamAccountName -eq $x }| Select-Object SamAccountName, givenname, surname, Enabled

                chek_out

            }


        elseif ($inBox.TextLength -ne 0) 

            {   <#if entering string #>

                $out = get-ADUser -Filter * -Properties Name, Description, SamAccountName, DisplayName, Enabled, Givenname | where {$_.displayName, $_.Description, $_.FirstName, $_.SamAccountName -like "*$x*"} | Select-Object SamAccountName, Description, Enabled

                chek_out

            }

        else {[System.Windows.MessageBox]::Show('Enter something ','error')}

         }

                    )

     $form.ShowDialog() | Out-Null 

    }

function chek_out

    {

        #if user is  exist then

            if ($out -ne $null )

                {

                    if($out.count -gt 1) #אם נמצא מספר משתמשים 

                        {

                            for ($i=0; $i -le $out.Count-1; $i++)

                                {#הצגת משתמשים לפי כמותם 

                                    if ($out.Enabled[$i] -eq 'true') #user is enable write is green

                                        {$outBox.SelectionColor = 'green'}

                                    else #user is disable write in red

                                         {$outBox.SelectionColor = 'Red'}

                                    $te = $out.SamAccountName[$i] +"    ," + $out.Description[$i] #$te = username + Description

                                    $outBox.Appendtext("{0}`n" -f $te)  #write to outbox

                                }

                         }

                     else #אם נמצא משתמש אחד בלבד

                        { #Description is empty

                            if ($out.Description -eq $null)

                                {

                                    if ($out.Enabled -eq 'true')

                                        {$outBox.Forecolor="green"}

                                    else 

                                        {$outBox.Forecolor="red"}    

                                    $outBox.text= $out.SamAccountName                                    

                                 } 

                             else #Description is NOT empty

                                {

                                    $te = $out.SamAccountName +"     ," + $out.Description

                                    if ($out.Enabled -eq 'true')

                                        {$outBox.Forecolor="green"}

                                    else {$outBox.Forecolor="red"} 

                                    $outBox.Appendtext("{0}`n" -f $te)

                                 }

                             if ($chekbox.Checked -eq $true) 

                                  {

                                   #get member of and put them to memberofbox like text 

                                   $memof=Get-ADPrincipalGroupMembership $out.SamAccountName | select name 

                                   $memberofbox.Text = $memof.name | Out-String

                                   }

                        }

                 }

                #if user not found

            else 

                {

                    [System.Windows.MessageBox]::Show("Not found")

                    #$outBox.Text = "Not found"

                    $memberofbox.text = $nf 

                }        

    }

function Hide-Console

{

    Add-Type -Name Window -Namespace Console -MemberDefinition '

[DllImport("Kernel32.dll")]

public static extern IntPtr GetConsoleWindow();


[DllImport("user32.dll")]

public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);

'

    $consolePtr = [Console.Window]::GetConsoleWindow()

    #0 hide

    [Console.Window]::ShowWindow($consolePtr, 0)

}

Hide-Console

form

#nltest /DSGETDC:domain.local

==END OF SCRIPT==