Advertisement
Madmacs77

Set_OSDDiskindex

Jul 20th, 2018
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Begin
  2. # Load Microsoft.SMS.TSEnvironment COM object
  3. {
  4. try {
  5.         $TSEnvironment = New-Object -ComObject Microsoft.SMS.TSEnvironment -ErrorAction Continue
  6.     }
  7.     catch [System.Exception] {
  8.         Write-Warning -Message "Unable to construct Microsoft.SMS.TSEnvironment object"; break
  9.     }
  10. }
  11.  
  12. Process
  13. {
  14.  
  15. function Write-CMLogEntry
  16.     {
  17.         param (
  18.             [parameter(Mandatory = $true, HelpMessage = "Value added to the log file.")]
  19.             [ValidateNotNullOrEmpty()]
  20.             [string]$Value,
  21.             [parameter(Mandatory = $true, HelpMessage = "Severity for the log entry. 1 for Informational, 2 for Warning and 3 for Error.")]
  22.             [ValidateNotNullOrEmpty()]
  23.             [ValidateSet("1", "2", "3")]
  24.             [string]$Severity,
  25.             [parameter(Mandatory = $false, HelpMessage = "Name of the log file that the entry will written to.")]
  26.             [ValidateNotNullOrEmpty()]
  27.             [string]$FileName = "Invoke-Set_OSDDiskIndex.log"
  28.         )
  29.         # Determine log file location
  30.         $LogFilePath = Join-Path -Path $Script:TSEnvironment.Value("_SMSTSLogPath") -ChildPath $FileName
  31.        
  32.         # Construct time stamp for log entry
  33.         $Time = -join @((Get-Date -Format "HH:mm:ss.fff"), "+", (Get-WmiObject -Class Win32_TimeZone | Select-Object -ExpandProperty Bias))
  34.        
  35.         # Construct date for log entry
  36.         $Date = (Get-Date -Format "MM-dd-yyyy")
  37.        
  38.         # Construct context for log entry
  39.         $Context = $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
  40.        
  41.         # Construct final log entry
  42.         $LogText = "<![LOG[$($Value)]LOG]!><time=""$($Time)"" date=""$($Date)"" component=""Set_OSDDiskIndex.log"" context=""$($Context)"" type=""$($Severity)"" thread=""$($PID)"" file="""">"
  43.        
  44.         # Add value to log file
  45.         try
  46.         {
  47.             Out-File -InputObject $LogText -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop
  48.         }
  49.         catch [System.Exception] {
  50.             Write-Warning -Message "Unable to append log entry to Invoke-Set_OSDDiskIndex.log file. Error message: $($_.Exception.Message)"
  51.         }
  52.     }
  53.  
  54.  
  55.     # Determine OSDDISKINDEX
  56.     # If booted to Windows
  57.     if (($TSEnvironment -ne $null) -and ($TSEnvironment.Value("_SMSTSinWinPE") -ne $true)){
  58.        
  59.     # Set log file location
  60.     $LogFilePath = Join-Path -Path $TSEnvironment.Value("_SMSTSLogPath") -ChildPath $LogFileName
  61.  
  62.         try
  63.         {
  64.        
  65.         $BootInfo = (gwmi Win32_BootConfiguration)
  66.         $BootInfoSplit = ($BootInfo).Caption -split "\\"
  67.         $TargetDisk = $BootInfoSplit[2].Trim('Harddisk')
  68.         $TSEnvironment.Value('OSDDiskIndex') = $TargetDisk
  69.        
  70.         Write-CMLogEntry -Value "The value of OSDDISKINDEX is being set to $TargetDisk" -Severity 1
  71.  
  72.         }
  73.         catch [System.Exception]
  74.         {
  75.  
  76.         Write-CMLogEntry -Value "An error occured while setting the OSDDISKINDEX value. Error message: $($_.Exception.Message)" -Severity 3; exit 1
  77.  
  78.         }
  79.     }
  80.  
  81.     #If Booted to WinPE
  82.     if (($TSEnvironment -ne $null) -and ($TSEnvironment.Value("_SMSTSinWinPE") -eq $true)){
  83.  
  84.     # Set log file location
  85.     $LogFilePath = Join-Path -Path $TSEnvironment.Value("_SMSTSLogPath") -ChildPath $LogFileName
  86.  
  87.    
  88.     #Gather disk information. We'll need it further down.
  89.     $DiskInfo = Get-WmiObject Win32_DiskDrive |
  90.         ForEach-Object {
  91.             $disk = $_
  92.             $partitions = "ASSOCIATORS OF " +
  93.             "{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
  94.             "WHERE AssocClass = Win32_DiskDriveToDiskPartition"
  95.             Get-WmiObject -Query $partitions |
  96.                 ForEach-Object {
  97.                 $partition = $_
  98.                 $drives = "ASSOCIATORS OF " +
  99.                     "{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
  100.                     "WHERE AssocClass = Win32_LogicalDiskToPartition"
  101.                     Get-WmiObject -Query $drives |
  102.                         ForEach-Object {
  103.                         [PSCustomObject]@{
  104.                         Disk        = $disk.DeviceID
  105.                         DiskSize    = $disk.Size
  106.                         DiskModel   = $disk.Model
  107.                         Partition   = $partition.Name
  108.                         RawSize     = $partition.Size
  109.                         Type        = $partition.Type
  110.                         DriveLetter = $_.DeviceID
  111.                         VolumeName  = $_.VolumeName
  112.                         Size        = $_.Size
  113.                         FreeSpace   = $_.FreeSpace
  114.                         }
  115.                 }
  116.             }
  117.         }
  118.    
  119.     #Check to see if there are multiple installs of Windows
  120.     $BootDrive = (Get-ChildItem Env:SystemDrive | Select-Object -ExpandProperty Value)
  121.  
  122.     $ArrayDisks = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType = 3 AND DeviceID != '$BootDrive'" | Select-Object -ExpandProperty DeviceID
  123.  
  124.     $Array = foreach ($ArrayDisk in $ArrayDisks)
  125.         {
  126.  
  127.         (Test-Path "$ArrayDisk\Windows\System32\ntoskrnl.exe")
  128.  
  129.         }
  130.  
  131.     If (($Array -match $True).count -gt 1)
  132.        
  133.         {
  134.  
  135.         Write-CMLogEntry -Value "Multiple installations of Windows have been detected. Unable to safely set which disk to image from within WinPE." -Severity 3; exit 1
  136.  
  137.         }
  138.    
  139.    
  140.     try
  141.         {
  142.        
  143.         $OSDDetectedWinDrive = (Get-ChildItem Env:_OSDDetectedWinDrive | Select-Object -ExpandProperty Value)
  144.         $OSDDetectedWinDrive = (($OSDDetectedWinDrive).Trim("\"))
  145.         $TSTargetdisk = ($DiskInfo.Where({$_.Driveletter -eq "$OSDDetectedWinDrive"})).Partition
  146.         $TSTargetdisk = ($TSTargetdisk -split ", ")
  147.         $TSTargetdisk = $TSTargetdisk[0].Trim('Disk #')
  148.         $TSEnvironment.Value('OSDDiskIndex') = $TSTargetdisk
  149.  
  150.         Write-CMLogEntry -Value "The value of OSDDISKINDEX is being set to $TSTargetDisk" -Severity 1
  151.        
  152.         }
  153.         catch [System.Exception]
  154.         {
  155.             Write-CMLogEntry -Value "An error occured while setting the OSDDISKINDEX value. Error message: $($_.Exception.Message)" -Severity 3; exit 1
  156.         }
  157.  
  158.     }
  159.  
  160. #Test for multiple EFI System Partitons
  161. $EFITest = (gwmi -Query "Select * from Win32_DiskPartition WHERE Type = 'GPT: System'" | Select-Object -ExpandProperty Name)
  162. If ($EFItest.count -gt 1){
  163.  
  164. Write-CMLogEntry -Value "Multiple EFI boot partitons have been found. This condition will result in the system being unbootable after imaging." -Severity 3; exit 1
  165.  
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement