Tuesday, December 18, 2012

Get Folder Permissions by Scripts (3)

In terms of getting folder permissions by using PS script, the first two blogs are just good enough. As a bonus and my personal reference, here are simple helper function and entry point. That completes my blog about my script.

The help function is a very simple one which prints out the usage of the script. The same practice can be applied to other scripts.

# FUNCTION LISTINGS
# =============================================================================
# Function: HelpInfo
# Created: [11/25/2009]
# Author: David Chu
# Arguments:
#   $p_scriptApp: script application
# -------------------------------------------
# Purpose: display usage message
# -------------------------------------------
function HelpInfo($p_scriptApp)
{
 Write-Output ""
 Write-Output "Description: Get permissions for a folder."
 Write-Output "Parameters: folderPath [outFile] [/s]"
 Write-Output "Where"
 Write-Output "      folderPath: a path for a folder"
 Write-Output "      outFile: output file"
 Write-Output "      /s: recursive to get sub folders"
 Write-Output "Examples:"
 Write-Output "This one running from PowerShell:"
 Write-Output ("  PS: {0} C:\MyData" -f $p_scriptApp)
 Write-Output "The following example running from cmd console:"
 Write-Output ("  cmd> PowerShell {0} C:\MyData myFile /s" -f $p_scriptApp)
}

PS entry section is the place to get all the parameters from command line.

# Example to run this script:
# @powershell d:\Scripts\GetPerm.ps1 'D:\Displays\Public' /s
# Result:
D:\Displays\Users Everyone:(OI)(CI)F
                 BUILTIN\Administrators:(OI)(CI)F
                 NT AUTHORITY\SYSTEM:(OI)(CI)F
                 CREATOR OWNER:(OI)(CI)(IO)F
                 BUILTIN\Users:(OI)(CI)R
                 BUILTIN\Users:(CI)(special access:)

                                   FILE_APPEND_DATA

                 BUILTIN\Users:(CI)(special access:)

                                   FILE_WRITE_DATA
#*=============================================================================
#* SCRIPT BODY
#*=============================================================================
# example parameter values for this script. Add the parameter to PowerGUI Script
# Editor's Input toolbar:
# C:\Users /s
$scriptApp = "GetPerm.ps1"

# check input arguments
if ( $args.length -eq 0 -or $args.length -gt 3 )
{
 HelpInfo $scriptApp
 return
}
# Get the first input parameter
$i = 0
$folderPath = $args[$i++
$outFile = "outFile.txt"]
$recursive = $false
if ($args.length -gt 1)
{
 if ( $args[$i++].ToLower() -eq "/s")
 {
   $recursive = $true
   if ($args.length -gt 2)
   {
     $outFile = $args[$i++]
   }
 }
 else
 {
   $outFile = $args[$i++]
   if ($args[$i++] -eq "/s")
   {
     $recursive = $true
   }
 }
}
# check if source and dest pathes exist
if ( Test-Path -Path $folderPath | where {!$_PSIsContainer} )
{
 GetFolderPermission $folderPath $recursive $outFile
} else {
 Write-Output ("Invalid path: {0}" -f $folderPath)
}

#*=============================================================================
#* END OF SCRIPT BODY
#*=============================================================================

In PS, command line argument can be obtained from $args[] collection, and the number of arguments is the property of length of $args.  Each argument can be enclosed by single quote ' char, not double " char.

0 comments: