Advertisement
Guest User

New-NetworkHomeDrive

a guest
Apr 1st, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Creates folder and maps it as network drive.
  4. .DESCRIPTION
  5. This script creates a folder if it doesn't exist yet and then maps it as a network drive.
  6. This essentially replaces the built-in Active Directory "Home Drive" Mapping.
  7. #>
  8. $username=$env:username
  9. $drivePath="\\server\$username"
  10. $driveLabel="Home"
  11. $driveLetter="S"
  12.  
  13. # Check if folder exist, else create folder.
  14.  
  15. If (!(Test-Path "$drivePath")) {
  16.     New-Item -path "$drivePath" -type directory
  17.     $Acl = Get-Acl "$drivePath"
  18.     $Ar = New-Object  system.security.accesscontrol.filesystemaccessrule("$username","FullControl","ContainerInherit, ObjectInherit","None","Allow")
  19.     $Acl.SetAccessRule($Ar)
  20.     Set-Acl $drivePath $Acl
  21. }
  22.  
  23. # Remove network map first, in case path is updated.
  24.  
  25. $letterExt = $driveLetter + ":\"
  26.  
  27. if (Test-Path $letterExt) {
  28.     Remove-PSDrive -Name "$driveLetter" -Force
  29. }
  30. Start-Sleep 2
  31. New-PSDrive –Name “$driveLetter” –PSProvider FileSystem –Root "$drivePath" –Persist -Scope Global
  32.  
  33. echo $drivePath
  34.  
  35. # Rename network drive.
  36.  
  37. $sh=New-Object -com Shell.Application
  38. $sh.NameSpace("$letterExt").Self.Name = $driveLabel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement