Tag: duplicates

  • Creating Security Groups for File Shares in Bulk using PowerShell

    Creating Security Groups for File Shares in Bulk using PowerShell

    Security Groups are great for managing large groups for permissions.  A client requested that they needed to have Read-Only, Read-Write, and Ready-Modify (allow for deleting) for all their file shares for better management.

    Getting the Share Names

    In order for me to create the groups I needed the share names. PowerShell to the rescue!

    Type the following on the File Server/ Cluster to list all the shares and capture the output in a text file:

    WmiObject -class Win32_Share -computer <yourfileserver.fqdn) | Out-File c:\scripts\shares.txt

    [su_note note_color=”#fafae8″]On your file-server you may have a lot of share but for example purposes I am showing just one.[/su_note]

    Output should be similar to:

    Cleaning up the Share Names

    Now that we have the Share names we need to do a bit of cleanup to avoid having duplicates.

    • We need to remove all entries for hidden shares “$”
    • We need to remove duplicates
    • We need to change the case of the share names to lower case. ( I prefer lowercase but you can decide to do what best fits your needs)

    Follow my guide to removing duplicates in a text file using NotePad++

    Once the sharenames are clean save it to a text file.

    Client Requirement for the Security Groups:

    For each file share there are three security groups needed:

    • <Sharename>_RO : Read-Only
    • <Sharename>_RW : Read & Write
    • <Sharename>_RM : Read & Modify

    For PowerShell to do this I needed to create a .CSV file with all the security group entries.  Now, there are many ways this can be done. I will share what I have been doing.

    Open up Microsoft Excel and copy the share on a column to the right (lets say K2)

    Now on Cell A2 your value should be =CONCATENATE(K2,”_RW”) and drag it down.

    It should look something like this:

    Do the same for RO & RM. Now you have all the security groups names you need to create.

    Create a file called FileShares_Groups.csv  using the following format.

    Create the file Create Security Groups for File Shares.ps1

    .NOTES
    	File Name:
    	Author: Mohammed Wasay
    	Contact Info:
    		Website:www.mowasay.com
    		Twitter:@wasay
    	Requires:
    	Tested:
    .PARAMETER
        None
    
    .EXAMPLE
        .\Create Security Groups for File Shares.ps1
    #>
    #Import Active Directory Module
    Import-Module ActiveDirectory
    
    $csv = Import-Csv -Path "c:\scripts\FileShares_Groups.csv"
    
    ForEach ($item In $csv)
        {
            $create_group = New-ADGroup -Name $item.GroupName -GroupCategory $item.GroupCategory -groupScope $item.GroupScope -Path $item.OU
            Write-Host -ForegroundColor Green "Group $($item.GroupName) created!"
        }
    

    Copy the two files: FileShares_Groups.csv & Create Security Groups for File Shares.ps1  into a folder called C:\scripts  on the Domain Controller.

    Run the PowerShell script and see the security groups get created.

     

     

  • Remove duplicates, blank lines, spaces, to get unique values and sort data in one operation

    Remove duplicates, blank lines, spaces, to get unique values and sort data in one operation

    From time to time I come across this need; where I need to scrub a file where there are duplicates, there are blank lines, the sort order is all wack, and it just needs to be formatted to where it can be more readable and/or usable.

    This method just doesn’t apply to text, but also applies to numbers.

    Software Prerequisites:

    • NotePad++
    • TextFX Characters Plug-in for NotePad++

    Enabling TextFX Characters Plug-in

    Install NotePad++ with all defaults

    Goto Plugins > Plugin Manager > Show Plugin Manager

    Install TextFX Characters Plugin

    Once successfully downloaded it will prompt for a restart.

    After a successful restart of the application you should now see the TextFX entry in the toolbar.

    Removing duplicates, blank lines, and sorting data

    • Paste the text into Notepad++ (CTRL+V). As you can see, there were lines and half of them were blank.

    • Mark all the text (CTRL+A). Click TextFX → Click TextFX Tools → Check +Sort outputs only UNIQUE (at column) lines (if not already checked).

    • Click TextFX → Click TextFX Tools → Click Sort lines case insensitive (at column)

    • Duplicates and blank lines have been removed and the data has been sorted alphabetically. (The first line that may appear empty contains a space, which is regarded as a character and is included in the list of unique data.)

    [su_tooltip position=”north” content=”Please check permissions on the files and folders. If you have unique or specialized permission on the file or folders these wont work.”]

    Changing to lowercase

    To change the text to lowercase Goto: TextFX > TextFX Characters > lower case[/su_tooltip]

    This has saved me a lot of time when working with IP addresses or cleaning up text.