Workspace-Guru

Create-and-attach-VHD-OST.ps1

Aug 18th, 2018
3,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## USER VARIABLES Start ##
  2. $vhdSharePath = "\\DOMAIN\DFSRoot$\OSTVHD" ## Path to VHD Store
  3.  
  4. ## USER VARIABLES End ##
  5.  
  6. ## GLOBAL VARIABLES Start ##
  7. $userName = $env:USERNAME
  8. $userDomain = $env:USERDOMAIN
  9. $secUser = New-Object System.Security.Principal.NTAccount($userName)
  10. $userSID = $secUser.Translate([System.Security.Principal.SecurityIdentifier])
  11. $vhdFileName = $userName + "_EMCache.vhd"
  12. $vhdUserDirectoryName = $userName + "_" + $userSID
  13. $vhdDirectoryPath = $vhdSharePath + "\" + $vhdUserDirectoryName
  14. $vhdPath = $vhdDirectoryPath + "\" + $vhdFileName
  15. $workingDir = $env:USERPROFILE + "\AppData\Local\Temp\VHDCache"
  16. $filesystemlabel = $userName + "_EMcache"
  17. $outlookDir = $env:USERPROFILE + "\AppData\Local\Microsoft\Outlook"
  18. $newWorkingDir = $workingDir + "\VHD"
  19.  
  20. ## GLOBAL VARIABLES End ##
  21.  
  22. ## Check if disk exsits or else create VHD
  23. $user = $userName
  24.  
  25.     if($user -ne "") {
  26.     Write-Host "Processing user" $user -ForegroundColor Cyan
  27.        
  28.     try
  29.     {
  30.         $secUser = New-Object System.Security.Principal.NTAccount($user)
  31.         $SID = $secUser.Translate([System.Security.Principal.SecurityIdentifier])
  32.         $diskName = "\${user}_EMCache.vhd"
  33.         $folderName = "${User}_${SID}"
  34.         $shareFolder = "$vhdSharePath\${folderName}"
  35.         $diskPath = "${shareFolder}${diskName}"
  36.         $testdiskpath = test-path $diskPath
  37.  
  38.         if(!$testdiskpath)
  39.         {
  40.             # Create the directory and set Allow and Full Control permissions
  41.             if(-not (Test-Path $shareFolder))
  42.             {
  43.                 try
  44.                 {
  45.                     # Create the directory for the user
  46.                     Write-Host "Creating directory ${shareFolder}"
  47.                     New-Item -Path $shareFolder -Type Directory -Force | Out-Null
  48.  
  49.                     # Set permissions on the user folder
  50.                     $acl = Get-Acl $shareFolder
  51.                     $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","InheritOnly","Allow")
  52.                     $acl.SetAccessRule($ar)
  53.                     Set-Acl $shareFolder $acl
  54.                 }
  55.                 catch
  56.                 {
  57.                     Write-Warning "_Error: $($_.exception.message)"
  58.                     "${user}_Error: $($_.exception.message)" | Add-Content $failedFolder
  59.                 }
  60.             }
  61.             else
  62.             {
  63.                 Write-Warning "Directory for $user already exists - skipping"
  64.             }
  65.  
  66. # Build command used for DiskPart
  67. $maximum = "51200"  # Expands to a maximum of 30GB
  68. $type = "expandable"
  69. $label = "$User"+"_EMCache"
  70.  
  71. $command = @"
  72. create vdisk file=$diskPath maximum=$maximum type=$type
  73. select vdisk file=$diskPath
  74. attach vdisk
  75. create partition primary
  76. format fs=ntfs label="$label" quick
  77. detach vdisk
  78. exit
  79. "@
  80.             # Create the user VHD via DiskPart
  81.             Write-Host "Creating Virtual Disk"
  82.             $command | DiskPart | Where-Object { $_.Length -gt 0 } | Out-Null
  83.             Write-Host "Created Virtual Disk"
  84.         }
  85.         else
  86.         {
  87.             Write-Warning "EMCache path for $user already exists - skipping"
  88.         }
  89.     }
  90.     catch
  91.     {
  92.         # Output any errors to the failed.txt file
  93.          Write-Warning "Error processing ${user}: $($_.exception.message)"
  94.         "${user}_Error: $($_.exception.message)" | Add-Content $failedFolder
  95.     }
  96.   }
  97.  
  98. ## Check VHD exit ##
  99.  
  100. ## ATTACH VHD Start ##
  101. [string]$label = ""
  102.  
  103. "Attaching existing VHD"
  104.  
  105. $cmd = @"
  106. Select vdisk file=$vhdPath
  107. attach vdisk
  108. exit
  109. "@
  110.  
  111.     $cmd | diskpart | Out-Null
  112.  
  113.     $vol = Get-WMIObject -Class Win32_Volume -Filter "Label='$filesystemlabel'"
  114.     $label = $vol.Label
  115.  
  116.  
  117. "Attach VHD to system - complete"
  118.  
  119. ## ATTACH VHD End ##
  120.  
  121.  
  122. ## READING VOLUME INFORMATION Start ##
  123.  
  124. "Reading volume information"
  125.  
  126. $link = $vol.DeviceID
  127.  
  128. "Reading volume information - complete"
  129.  
  130. ## READING VOLUME INFORMATION End ##
  131.  
  132.  
  133. ## REMOVING DRIVE LETTER Start ##
  134.  
  135. "Removing drive letter if present"
  136. Set-WmiInstance -input $vol -Arguments @{DriveLetter=$null} | out-null
  137. "Removing drive letter if present - Complete"
  138.  
  139. ## REMOVING DRIVE LETTER End ##
  140.  
  141.  
  142. ## SETTING ROOT PERMISSIONS ON DISK Start ##
  143.  
  144. "Setting root permissions on disk"
  145.  
  146. icacls.exe "$link" /grant "$varUserdomain\${varUsername}:(OI)(CI)(F)" | Out-Null
  147.  
  148. "Setting root permissions on disk - complete"
  149.  
  150. ## SETTING ROOT PERMISSIONS ON DISK End ##
  151.  
  152.  
  153. ## MOUNTING Start ##
  154.  
  155. "Mounting"
  156. $mountPath = Join-Path -Path $workingDir -ChildPath VHD
  157.  
  158. New-item -path $mountPath -itemtype "Directory" -ErrorAction SilentlyContinue |Out-Null
  159.  
  160. mountvol.exe $mountPath $link | Out-Null
  161.  
  162. "Mounting - complete"
  163. ## MOUNTING End ##
  164.  
  165. ## DELETE RECYCLE BIN Start ##
  166. "Delete recycle bin"
  167.  
  168. $recycleDir = "$mountPath\`$RECYCLE.BIN"
  169.  
  170. if(Test-Path $recycleDir)
  171. {
  172. Remove-Item -Path $recycleDir -Force -Recurse
  173. }
  174.  
  175. "Delete recycle bin - Complete"
  176. ## DELETE RECYCLE BIN End ##
  177.  
  178. ## CREATE FOLDERS IN VHD Start ##
  179. "Create Outlook directory"
  180.  
  181. if(Test-Path -Path $outlookDir){
  182.     Write-Host "Local Outlook Directory found" -ForegroundColor Green
  183.     $copyLocalOutlookData = Copy-Item -Path $outlookDir -Destination $newWorkingDir -Recurse -Force
  184.  
  185.     if($copyLocalOutlookData){
  186.         Write-Host "Local outlook data is copied!" -ForegroundColor Green
  187.     } else {
  188.         Write-Host "Copy local outlook data failed!" -ForegroundColor Red
  189.         #exit 1
  190.     }
  191.  
  192.     $removeLocalOutlookData = Remove-Item -Path $outlookDir -Recurse -Force
  193.  
  194.     if($removeLocalOutlookData){
  195.         Write-Host "Local outlook data is removed!" -ForegroundColor Green
  196.     }
  197.  
  198. } else {
  199.     Write-Host "Local Outlook Directory doesn't exist or is removed already!" -ForegroundColor Yellow
  200. }
  201.  
  202. $testworkingdir = Test-Path "$workingDir\VHD\Outlook"
  203.  
  204. if (!$testworkingdir){
  205. New-Item -Path $workingDir\VHD\Outlook -ItemType Directory |out-null
  206. }
  207. "Create Outlook directory - Complete"
  208. ## CREATE FOLDERS IN VHD End ##
  209.  
  210. ## CREATE JUNCTION Start ##
  211. "Create Junction"
  212. New-Item -ItemType SymbolicLink -Path $outlookDir -Value $workingDir\VHD\Outlook | Out-Null
  213. "Junction created!"
  214. ## CREATE JUNCTION End ##
Advertisement
Add Comment
Please, Sign In to add comment