Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## USER VARIABLES Start ##
- $vhdSharePath = "\\DOMAIN\DFSRoot$\OSTVHD" ## Path to VHD Store
- ## USER VARIABLES End ##
- ## GLOBAL VARIABLES Start ##
- $userName = $env:USERNAME
- $userDomain = $env:USERDOMAIN
- $secUser = New-Object System.Security.Principal.NTAccount($userName)
- $userSID = $secUser.Translate([System.Security.Principal.SecurityIdentifier])
- $vhdFileName = $userName + "_EMCache.vhd"
- $vhdUserDirectoryName = $userName + "_" + $userSID
- $vhdDirectoryPath = $vhdSharePath + "\" + $vhdUserDirectoryName
- $vhdPath = $vhdDirectoryPath + "\" + $vhdFileName
- $workingDir = $env:USERPROFILE + "\AppData\Local\Temp\VHDCache"
- $filesystemlabel = $userName + "_EMcache"
- $outlookDir = $env:USERPROFILE + "\AppData\Local\Microsoft\Outlook"
- $newWorkingDir = $workingDir + "\VHD"
- ## GLOBAL VARIABLES End ##
- ## Check if disk exsits or else create VHD
- $user = $userName
- if($user -ne "") {
- Write-Host "Processing user" $user -ForegroundColor Cyan
- try
- {
- $secUser = New-Object System.Security.Principal.NTAccount($user)
- $SID = $secUser.Translate([System.Security.Principal.SecurityIdentifier])
- $diskName = "\${user}_EMCache.vhd"
- $folderName = "${User}_${SID}"
- $shareFolder = "$vhdSharePath\${folderName}"
- $diskPath = "${shareFolder}${diskName}"
- $testdiskpath = test-path $diskPath
- if(!$testdiskpath)
- {
- # Create the directory and set Allow and Full Control permissions
- if(-not (Test-Path $shareFolder))
- {
- try
- {
- # Create the directory for the user
- Write-Host "Creating directory ${shareFolder}"
- New-Item -Path $shareFolder -Type Directory -Force | Out-Null
- # Set permissions on the user folder
- $acl = Get-Acl $shareFolder
- $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","InheritOnly","Allow")
- $acl.SetAccessRule($ar)
- Set-Acl $shareFolder $acl
- }
- catch
- {
- Write-Warning "_Error: $($_.exception.message)"
- "${user}_Error: $($_.exception.message)" | Add-Content $failedFolder
- }
- }
- else
- {
- Write-Warning "Directory for $user already exists - skipping"
- }
- # Build command used for DiskPart
- $maximum = "51200" # Expands to a maximum of 30GB
- $type = "expandable"
- $label = "$User"+"_EMCache"
- $command = @"
- create vdisk file=$diskPath maximum=$maximum type=$type
- select vdisk file=$diskPath
- attach vdisk
- create partition primary
- format fs=ntfs label="$label" quick
- detach vdisk
- exit
- "@
- # Create the user VHD via DiskPart
- Write-Host "Creating Virtual Disk"
- $command | DiskPart | Where-Object { $_.Length -gt 0 } | Out-Null
- Write-Host "Created Virtual Disk"
- }
- else
- {
- Write-Warning "EMCache path for $user already exists - skipping"
- }
- }
- catch
- {
- # Output any errors to the failed.txt file
- Write-Warning "Error processing ${user}: $($_.exception.message)"
- "${user}_Error: $($_.exception.message)" | Add-Content $failedFolder
- }
- }
- ## Check VHD exit ##
- ## ATTACH VHD Start ##
- [string]$label = ""
- "Attaching existing VHD"
- $cmd = @"
- Select vdisk file=$vhdPath
- attach vdisk
- exit
- "@
- $cmd | diskpart | Out-Null
- $vol = Get-WMIObject -Class Win32_Volume -Filter "Label='$filesystemlabel'"
- $label = $vol.Label
- "Attach VHD to system - complete"
- ## ATTACH VHD End ##
- ## READING VOLUME INFORMATION Start ##
- "Reading volume information"
- $link = $vol.DeviceID
- "Reading volume information - complete"
- ## READING VOLUME INFORMATION End ##
- ## REMOVING DRIVE LETTER Start ##
- "Removing drive letter if present"
- Set-WmiInstance -input $vol -Arguments @{DriveLetter=$null} | out-null
- "Removing drive letter if present - Complete"
- ## REMOVING DRIVE LETTER End ##
- ## SETTING ROOT PERMISSIONS ON DISK Start ##
- "Setting root permissions on disk"
- icacls.exe "$link" /grant "$varUserdomain\${varUsername}:(OI)(CI)(F)" | Out-Null
- "Setting root permissions on disk - complete"
- ## SETTING ROOT PERMISSIONS ON DISK End ##
- ## MOUNTING Start ##
- "Mounting"
- $mountPath = Join-Path -Path $workingDir -ChildPath VHD
- New-item -path $mountPath -itemtype "Directory" -ErrorAction SilentlyContinue |Out-Null
- mountvol.exe $mountPath $link | Out-Null
- "Mounting - complete"
- ## MOUNTING End ##
- ## DELETE RECYCLE BIN Start ##
- "Delete recycle bin"
- $recycleDir = "$mountPath\`$RECYCLE.BIN"
- if(Test-Path $recycleDir)
- {
- Remove-Item -Path $recycleDir -Force -Recurse
- }
- "Delete recycle bin - Complete"
- ## DELETE RECYCLE BIN End ##
- ## CREATE FOLDERS IN VHD Start ##
- "Create Outlook directory"
- if(Test-Path -Path $outlookDir){
- Write-Host "Local Outlook Directory found" -ForegroundColor Green
- $copyLocalOutlookData = Copy-Item -Path $outlookDir -Destination $newWorkingDir -Recurse -Force
- if($copyLocalOutlookData){
- Write-Host "Local outlook data is copied!" -ForegroundColor Green
- } else {
- Write-Host "Copy local outlook data failed!" -ForegroundColor Red
- #exit 1
- }
- $removeLocalOutlookData = Remove-Item -Path $outlookDir -Recurse -Force
- if($removeLocalOutlookData){
- Write-Host "Local outlook data is removed!" -ForegroundColor Green
- }
- } else {
- Write-Host "Local Outlook Directory doesn't exist or is removed already!" -ForegroundColor Yellow
- }
- $testworkingdir = Test-Path "$workingDir\VHD\Outlook"
- if (!$testworkingdir){
- New-Item -Path $workingDir\VHD\Outlook -ItemType Directory |out-null
- }
- "Create Outlook directory - Complete"
- ## CREATE FOLDERS IN VHD End ##
- ## CREATE JUNCTION Start ##
- "Create Junction"
- New-Item -ItemType SymbolicLink -Path $outlookDir -Value $workingDir\VHD\Outlook | Out-Null
- "Junction created!"
- ## CREATE JUNCTION End ##
Advertisement
Add Comment
Please, Sign In to add comment