Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Creates folder and maps it as network drive.
- .DESCRIPTION
- This script creates a folder if it doesn't exist yet and then maps it as a network drive.
- This essentially replaces the built-in Active Directory "Home Drive" Mapping.
- #>
- $username=$env:username
- $drivePath="\\server\$username"
- $driveLabel="Home"
- $driveLetter="S"
- # Check if folder exist, else create folder.
- If (!(Test-Path "$drivePath")) {
- New-Item -path "$drivePath" -type directory
- $Acl = Get-Acl "$drivePath"
- $Ar = New-Object system.security.accesscontrol.filesystemaccessrule("$username","FullControl","ContainerInherit, ObjectInherit","None","Allow")
- $Acl.SetAccessRule($Ar)
- Set-Acl $drivePath $Acl
- }
- # Remove network map first, in case path is updated.
- $letterExt = $driveLetter + ":\"
- if (Test-Path $letterExt) {
- Remove-PSDrive -Name "$driveLetter" -Force
- }
- Start-Sleep 2
- New-PSDrive –Name “$driveLetter” –PSProvider FileSystem –Root "$drivePath" –Persist -Scope Global
- echo $drivePath
- # Rename network drive.
- $sh=New-Object -com Shell.Application
- $sh.NameSpace("$letterExt").Self.Name = $driveLabel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement