Your basic ITPro blog... What's going on at work, what I'm interested in.

Friday, March 14, 2008

Tear-Down Shared Folders

Here is the script to tear down the shared folders/security groups we are using -

###############################################################################################
##
##   DeleteGroup.ps1
##
## Deletes File Shares and associated Security Groups:
##         - Deletes MOD, RO, groups for security assignments to group shares
##         - Deletes the shared folders themselves
##
## If using a TXT file for input, it contains a list of folder names.
##
## TXT File Format:
##   One Column
##       - First row = NAME (used as data element identifier)
##       - Subsequent rows = <name of the folder to be created>  **13 characters or less
##            - Longer names will be truncated to 13 characters!!
## TXT file example:
##    NAME
##    Folder001
##    Folder002
##
## Best if run on GC of Domain
##
###############################################################################################

function get_option {
    # This function allows user to select input type
    #   - single folder entered at command line or .TXT file
    Clear-Host
    Write-Host -ForegroundColor Cyan "WELCOME TO THE 'DELETE SHARED FOLDER(S) SCRIPT'"
    Write-Host "Do you want to enter a single folder name or use a .TXT file for input?"
    Write-Host "  Please enter '1' to type a single folder name"
    Write-Host "  Please enter '2' to use a .TXT input file"
    $script:chosen = Read-Host "1 or 2"
    switch ($script:chosen) {
        1 {
            Write-Host "You selected to enter a single folder name."
            $script:foldername = Read-Host "Enter the folder name"
            Write-Host "You entered '$script:foldername'"
            $script:isBatch = $FALSE
            $script:MayProceed = $TRUE
        }
        2 {
            Write-Host "You selected to enter a .TXT file name for input."
            $script:ListOfFolders = Read-Host "Enter the .TXT filename"
            Write-Host "You entered '$script:ListOfFolders'"
            $script:MayProceed = Test-Path $script:ListOfFolders
            if ($script:MayProceed) {
                Write-Host "File exists... Proceeding"
                $script:isBatch = $TRUE
            }
            else {
                Write-Host -ForegroundColor Red "File does not exist... Try again"
                Start-Sleep -Seconds 2
            }
        }
        default {
            Write-Host -ForegroundColor Red "You did not choose a valid option... Please try again..."
            Start-Sleep -Seconds 3
        }
    }
}

function process_folder {
    param([string]$foldername)
    # Do we actually have a folder name?
    if ($foldername.length -ne 0)
    {
        Write-Host "************************************************"
        Write-Host "Now processing folder name:" $foldername "...."

        # Defining path to folder
        $FullPath = "\\W2003\Groups\" + $foldername

        # Does this folder exist?
        $isThere = Test-Path $FullPath
        if ($isThere)
        {
            # Delete Folder
            Write-Host "Deleting the folder..." -NoNewline
            Remove-Item -path $FullPath -Recurse
            Write-Host "  DONE!"

            # Create variables holding group names
            # If necessary, shorten folder name to 13 characters
            if ($foldername.length  -gt 13) {
                $MODgroup = "s_" + $foldername.substring(0,13) + "_MOD"
                $ROgroup  = "s_" + $foldername.substring(0,13) + "_RO"
            }
            else {
                $MODgroup = "s_" + $foldername + "_MOD"
                $ROgroup  = "s_" + $foldername + "_RO"
            }
            # Deletes Groups
            Write-Host "Deleting the MODify security group..." -NoNewline
            $deletedGrp = Remove-QADObject -Identity $MODgroup -DeleteTree -Force
            Write-Host "  DONE!"
            Write-Host "Deleting the ReadOnly security group..." -NoNewline
            $deletedGrp = Remove-QADObject -Identity $ROgroup -DeleteTree -Force
            Write-Host "  DONE!"
        }
        else
        {
            Write-Host -ForegroundColor Red $foldername "folder does not exist."
        }
    }
}

$chosen = "0"
$ListOfFolders = ""
$foldername = ""
$isBatch = $FALSE
$MayProceed = $FALSE

do {get_option} until ($chosen -eq "1" -or $chosen -eq "2" -and $MayProceed -eq $TRUE)

if ($isBatch) {
    $ListOfFolderNames = Import-CSV $ListOfFolders
    foreach ($folder in $ListOfFolderNames) {
        $foldername = $folder.NAME
        process_folder $foldername
    }
}
else {
    process_folder $foldername
}

Write-Host "All Finished. Thank you."

No comments:

Additional Info

My photo
email: support (AT) mangrumtech (DOT) com
mobile: 480-270-4332