Create File Folder and permission

I needed a quick script to include in my runbooks – one to create the home drive for my new user process. I got the base for this off the internet ( https://activedirectoryfaq.com/2017/09/powershell-create-home-directory-grant-permissions/ ) but I wanted to add a little bit of checking to see if the file existed first, etc.

param([Parameter(Mandatory=$true)][String]$samAccountName)

$User = Get-ADUser -Identity $samAccountName -Properties l

$office = $User.l

$homePath = "\\my.netork\user$\" + $Office + "\{0}" -f $samAccountName

 
if($User -ne $Null) {
	
	#check home path to make sure one doesn't already exist
	[string]$homepathtest = Test-Path $homePath
	
	If ($homepathtest -eq "False") 
		{
		#create home drive
	    $homeShare = New-Item -path $homePath -ItemType Directory -force -ea Stop
	 	
	    $acl = Get-Acl $homeShare
	 	
		#permissison home drive
	    $FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Full"
	    $AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
	    $InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
	    $PropagationFlags = [System.Security.AccessControl.PropagationFlags]"InheritOnly"
	 
	    $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($User.SID, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
	    $acl.AddAccessRule($AccessRule)
	 
	    Set-Acl -Path $homeShare -AclObject $acl -ea Stop
	 	
	    Write-Host ("HomeDirectory created at {0}" -f $homePath)
	
		}
		
	If($homepathtest -eq "true") {
		Write-Host ("Home Directory at {0} already exists" -f $homePath )
		}
	
#
} 

Powershell – More Code Snippits

Getting the Date:

$now = Get-Date

I like this format for File names:

$Cdate = get-date -f “yyyy-MM-dd-HH-mm-ss”

How to log to a file:

#
Log to file
#
$STR_Error = $null
$NOW = Get-Date -Format s
$STR_Error = $NOW + “, ” + $STR_AffectedGroupName + “, FAILED, AD Group not found.,” + $STR_ADGETERROR
Echo $STR_Error | Out-File $log -Append
$STR_Error = $null

Powershell – Useful snippits of code

I find myself using some pieces of code frequently. As I work, I often look back and copy something so that I can save time. I have long since lost the references online from which I may have learned – but I give full credit to those wonderful folks online who post stuff.. so here are some of my favorites.

#
Comparing strings
#
$ARR_ERSAPPLIST = $ARR_ERSAPPLIST | Sort-Object
$ARR_NPGAPPLIST = $ARR_NPGAPPLIST | Sort-Object
$ARR_APPLIST = Compare-Object $ARR_ERSAPPLIST -DifferenceObject $ARR_NPGAPPLIST -PassThru
$ARR_APPLIST2 = Compare-Object -ReferenceObject $ARR_NPGAPPLIST -DifferenceObject $ARR_ERSAPPLIST -PassThru
$ERSfromNPG = $ARR_ERSAPPLIST |% {if ($ARR_NPGAPPLIST -contains $_) { $_ }}
$NPGfromERS = $ARR_NPGAPPLIST |% {if ($ARR_ERSAPPLIST -contains $_) { $_ }}

#
create a hash table object
#
$myObject2 = New-Object BOB -TypeName PSObject
$myObject2 | Add-Member -type NoteProperty -name Name -Value "Doug_PC"
$myObject2 | Add-Member -type NoteProperty -name Manufacturer -Value "Dell"
$myObject2 | Add-Member -type NoteProperty -name ProcessorSpeed -Value "2.6 Ghz"
$myObject2 | Add-Member -type NoteProperty -name Memory -Value "131"
$masterlist += $myObject2


#
Yes No
#
YesNo
$YesNo = [System.Windows.Forms.MessageBox]::Show("No User Name was entered. The script will exit")
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to delete these files?", `
0,"Delete Files",4)
If ($intAnswer -eq 6) {
$a.popup("You answered yes.")
} else {
$a.popup("You answered no.")
}