Added a New script

This commit is contained in:
freedbygrace
2024-06-13 08:32:26 -04:00
committed by GitHub
parent f40c4ea83d
commit 0ee07971be
2 changed files with 174 additions and 43 deletions
@@ -0,0 +1,109 @@
Try
{
#Initialize the task sequence environment
Try {[System.__ComObject]$TSEnvironment = New-Object -ComObject "Microsoft.SMS.TSEnvironment"} Catch {}
#Define variable(s)
[Regex]$BusTypeExclusions = "(^USB$)"
[String[]]$RequiredModules = @("Storage")
$IsUEFI = [Boolean]::Parse($TSEnvironment.Value('_SMSTSBootUEFI'))
$LogMessage = "IsUEFI = $($IsUEFI.ToString())"
Write-Verbose -Message "$($LogMessage)" -Verbose
[UInt32]$OperatingSystemDiskNumber = $TSEnvironment.Value('OSDDiskIndex')
$LogMessage = "Operating System Disk Number = $($OperatingSystemDiskNumber)"
Write-Verbose -Message "$($LogMessage)" -Verbose
$LogMessage = "Operating System disk WILL NOT be wiped because it has already been formatted by the operating system deployment solution."
Write-Verbose -Message "$($LogMessage)" -Verbose
#Import required module(s)
ForEach ($RequiredModule In $RequiredModules)
{
If (!(Get-Module -Name $RequiredModule))
{
$Null = Import-Module -Name ($RequiredModule) -DisableNameChecking -Force -ErrorAction Stop
}
}
#Get the physical disks attached to the device
$PhysicalDisks = Get-PhysicalDisk | Where-Object {($_.BusType -inotmatch $BusTypeExclusions.ToString())} | Sort-Object -Property @('Size')
#Measure the amount of physical disks detected within the device (Ensure that storage interface(s) are not disabled within the BIOS settings)
$PhysicalDiskCount = $PhysicalDisks | Measure-Object | Select-Object -ExpandProperty Count
#Determine the disk that will store the operating system
$OperatingSystemDisk = $PhysicalDisks | Where-Object {($_.DeviceID -ieq $OperatingSystemDiskNumber)}
#Determine the disk(s) that will not store the operating system
$DataDisks = $PhysicalDisks | Where-Object {($_.DeviceID -ine $OperatingSystemDisk.DeviceID)}
$DataDiskCount = $DataDisks | Measure-Object | Select-Object -ExpandProperty Count
#Get the partitions/volume(s) on the disk
If ($DataDiskCount -gt 0)
{
ForEach ($DataDisk In $DataDisks)
{
[Int]$DataDiskNumber = $DataDisk.DeviceID
[String]$DataDiskVolumeName = "DATA00$($DataDiskNumber)"
$TaskSequenceVariableName = "OSDFormatDataDisk00$($DataDiskNumber)"
$TSEnvironment.Value($TaskSequenceVariableName) = "False"
[ScriptBlock]$GetDataDiskInfo = {Get-Disk -Number ($DataDiskNumber)}
[ScriptBlock]$FormatAndPartitionDisk = {
#Clean the disk and format it according to the specification(s)
Switch ($IsUEFI)
{
{($_ -eq $True)}
{
$PartitionStyle = [Microsoft.PowerShell.Cmdletization.GeneratedTypes.Disk.PartitionStyle]::GPT
}
{($_ -eq $False)}
{
$PartitionStyle = [Microsoft.PowerShell.Cmdletization.GeneratedTypes.Disk.PartitionStyle]::MBR
}
}
#Perform disk partitioning and formatting
[String]$ActivityMessage = "Attempting to format and partition data disk $($DataDiskNumber). Please Wait..."
[String]$StatusMessage = "$($ActivityMessage)"
[Int]$ProgressID = 1
Write-Progress -ID ($ProgressID) -Activity ($ActivityMessage) -Status ($StatusMessage)
Switch ($GetDataDiskInfo.Invoke())
{
{($_.PartitionStyle -imatch "(^RAW$)")}
{
$Null = Initialize-Disk -InputObject ($GetDataDiskInfo.Invoke()) -PartitionStyle ($PartitionStyle) -PassThru -Confirm:$False -Verbose
}
{($_.PartitionStyle -inotmatch "(^RAW$)")}
{
$Null = Clear-Disk -InputObject ($GetDataDiskInfo.Invoke()) -RemoveData -Confirm:$False -PassThru -Verbose
$Null = Initialize-Disk -InputObject ($GetDataDiskInfo.Invoke()) -PartitionStyle ($PartitionStyle) -PassThru -Confirm:$False -Verbose
}
}
$Null = New-Partition -InputObject ($GetDataDiskInfo.Invoke()) -UseMaximumSize -AssignDriveLetter -Verbose | Format-Volume -FileSystem NTFS -NewFileSystemLabel ($DataDiskVolumeName) -Confirm:$False -Verbose
Write-Progress -ID ($ProgressID) -Activity ($ActivityMessage) -Completed
}
#Clean the disk and format it according to the specification(s)
$FormatAndPartitionDisk.Invoke()
}
}
}
Catch
{
Write-Error -ErrorRecord ($_)
}
+65 -43
View File
@@ -45,17 +45,29 @@ Try
{ {
ForEach ($DataDisk In $DataDisks) ForEach ($DataDisk In $DataDisks)
{ {
[Int]$DataDiskNumber = $DataDisk.DeviceID [System.Int32]$DataDiskNumber = $DataDisk.DeviceID
[String]$DataDiskVolumeName = "DATA00$($DataDiskNumber)" [String]$DataDiskVolumeName = "DATA$($DataDiskNumber.ToString('000'))"
$TaskSequenceVariableName = "OSDFormatDataDisk00$($DataDiskNumber)" $TaskSequenceVariableName = "OSDFormatDataDisk$($DataDiskNumber.ToString('000'))"
$TSEnvironment.Value($TaskSequenceVariableName) = "False" $TSEnvironment.Value($TaskSequenceVariableName) = "False"
[ScriptBlock]$GetDataDiskInfo = {Get-Disk -Number ($DataDiskNumber)} [ScriptBlock]$GetDataDiskInfo = {
Param
(
[System.Int32]$DiskNumber
)
Write-Output -InputObject (Get-Disk -Number ($DiskNumber))
}
[ScriptBlock]$FormatAndPartitionDisk = { [ScriptBlock]$FormatAndPartitionDisk = {
Param
(
[System.Int32]$DiskNumber
)
#Clean the disk and format it according to the specification(s) #Clean the disk and format it according to the specification(s)
Switch ($IsUEFI) Switch ($IsUEFI)
{ {
@@ -71,7 +83,7 @@ Try
} }
#Perform disk partitioning and formatting #Perform disk partitioning and formatting
[String]$ActivityMessage = "Attempting to format and partition data disk $($DataDiskNumber). Please Wait..." [String]$ActivityMessage = "Attempting to format and partition data disk $($DiskNumber). Please Wait..."
[String]$StatusMessage = "$($ActivityMessage)" [String]$StatusMessage = "$($ActivityMessage)"
@@ -79,70 +91,80 @@ Try
Write-Progress -ID ($ProgressID) -Activity ($ActivityMessage) -Status ($StatusMessage) Write-Progress -ID ($ProgressID) -Activity ($ActivityMessage) -Status ($StatusMessage)
Switch ($GetDataDiskInfo.Invoke()) Switch ($GetDataDiskInfo.Invoke($DiskNumber))
{ {
{($_.PartitionStyle -imatch "(^RAW$)")} {($_.PartitionStyle -imatch "(^RAW$)")}
{ {
$Null = Initialize-Disk -InputObject ($GetDataDiskInfo.Invoke()) -PartitionStyle ($PartitionStyle) -PassThru -Confirm:$False -Verbose $Null = Initialize-Disk -InputObject ($GetDataDiskInfo.Invoke($DiskNumber)) -PartitionStyle ($PartitionStyle) -PassThru -Confirm:$False -Verbose
} }
{($_.PartitionStyle -inotmatch "(^RAW$)")} {($_.PartitionStyle -inotmatch "(^RAW$)")}
{ {
$Null = Clear-Disk -InputObject ($GetDataDiskInfo.Invoke()) -RemoveData -Confirm:$False -PassThru -Verbose $Null = Clear-Disk -InputObject ($GetDataDiskInfo.Invoke($DiskNumber)) -RemoveData -Confirm:$False -PassThru -Verbose
$Null = Initialize-Disk -InputObject ($GetDataDiskInfo.Invoke()) -PartitionStyle ($PartitionStyle) -PassThru -Confirm:$False -Verbose $Null = Initialize-Disk -InputObject ($GetDataDiskInfo.Invoke($DiskNumber)) -PartitionStyle ($PartitionStyle) -PassThru -Confirm:$False -Verbose
} }
} }
$Null = New-Partition -InputObject ($GetDataDiskInfo.Invoke()) -UseMaximumSize -AssignDriveLetter -Verbose | Format-Volume -FileSystem NTFS -NewFileSystemLabel ($DataDiskVolumeName) -Confirm:$False -Verbose $Null = New-Partition -InputObject ($GetDataDiskInfo.Invoke($DiskNumber)) -UseMaximumSize -AssignDriveLetter -Verbose | Format-Volume -FileSystem NTFS -NewFileSystemLabel ($DataDiskVolumeName) -Confirm:$False -Verbose
Write-Progress -ID ($ProgressID) -Activity ($ActivityMessage) -Completed Write-Progress -ID ($ProgressID) -Activity ($ActivityMessage) -Completed
} }
If (($GetDataDiskInfo.Invoke()).NumberOfPartitions -gt 0) #Determine if the disk should be formatted
{ Switch ($True)
$DataDiskVolumes = $GetDataDiskInfo.Invoke() | Get-Partition | Get-Volume | Where-Object {($_.DriveLetter -imatch "(^[a-zA-Z]$)")} {
{(($GetDataDiskInfo.Invoke($DataDiskNumber)).NumberOfPartitions -gt 0)}
{
$DataDiskVolumes = $GetDataDiskInfo.Invoke($DataDiskNumber) | Get-Partition | Get-Volume | Where-Object {($_.DriveLetter -imatch "(^[a-zA-Z]$)")}
$DataDiskVolumeCount = $DataDiskVolumes | Measure-Object | Select-Object -ExpandProperty Count $DataDiskVolumeCount = $DataDiskVolumes | Measure-Object | Select-Object -ExpandProperty Count
If ($DataDiskVolumeCount -gt 0) Switch ($DataDiskVolumeCount -gt 0)
{ {
ForEach ($DataDiskVolume In $DataDiskVolumes) {($_ -eq $True)}
{ {
$FoldersInVolumeRoot = Get-ChildItem -Path "$($DataDiskVolume.DriveLetter):\" -Force | Where-Object {($_ -is [System.IO.DirectoryInfo])} :DataDiskVolumeLoop ForEach ($DataDiskVolume In $DataDiskVolumes)
{
$FoldersInVolumeRoot = Get-ChildItem -Path "$($DataDiskVolume.DriveLetter):\" -Force | Where-Object {($_ -is [System.IO.DirectoryInfo])}
If ($FoldersInVolumeRoot.Name -imatch "^Windows|Windows\.Old$") Switch ($True)
{ {
#Set the value of the 'OSDFormatDataDisk' variable which will tell the task sequence that this disk is ok to wipe {($FoldersInVolumeRoot.Name -imatch "(^Windows$)|(^Windows\.Old$)")}
$TSEnvironment.Value($TaskSequenceVariableName) = "True" {
#Set the value of the 'OSDFormatDataDisk' variable which will tell the task sequence that this disk is ok to wipe
$TSEnvironment.Value($TaskSequenceVariableName) = "True"
$LogMessage = "Disk $($DataDiskNumber) will be wiped and formatted because a previous Windows installation was detected and could cause errors with the installation of the new operating system." $LogMessage = "Disk $($DataDiskNumber) will be wiped and formatted because a previous Windows installation was detected and could cause errors with the installation of the new operating system."
Write-Verbose -Message "$($LogMessage)" -Verbose Write-Verbose -Message "$($LogMessage)" -Verbose
#Clean the disk and format it according to the specification(s) #Clean the disk and format it according to the specification(s)
$FormatAndPartitionDisk.Invoke() $FormatAndPartitionDisk.Invoke($DataDiskNumber)
#Exit the for loop because at least one volume has already met the requirements for the disk to be formatted #Exit the for loop because at least one volume has already met the requirements for the disk to be formatted
Break Break DataDiskVolumeLoop
} }
} }
} }
} }
}
}
If (($GetDataDiskInfo.Invoke().NumberOfPartitions -eq 0) -or ($GetDataDiskInfo.Invoke().PartitionStyle -imatch '^RAW$')) {(($GetDataDiskInfo.Invoke($DataDiskNumber).NumberOfPartitions -eq 0) -or ($GetDataDiskInfo.Invoke($DataDiskNumber).PartitionStyle -imatch '^RAW$'))}
{ {
$LogMessage = "Disk $($DataDiskNumber) will be wiped and formatted because no partition(s) or volume(s) could be found. The disk has not been provisioned." $LogMessage = "Disk $($DataDiskNumber) will be wiped and formatted because no partition(s) or volume(s) could be found. The disk has not been provisioned."
Write-Verbose -Message "$($LogMessage)" -Verbose Write-Verbose -Message "$($LogMessage)" -Verbose
#Set the value of the 'OSDFormatDataDisk' variable which will tell the task sequence that this disk is ok to wipe #Set the value of the 'OSDFormatDataDisk' variable which will tell the task sequence that this disk is ok to wipe
$TSEnvironment.Value($TaskSequenceVariableName) = "True" $TSEnvironment.Value($TaskSequenceVariableName) = "True"
#Clean the disk and format it according to the specification(s) #Clean the disk and format it according to the specification(s)
$FormatAndPartitionDisk.Invoke() $FormatAndPartitionDisk.Invoke($DataDiskNumber)
} }
}
} }
} }
} }
Catch Catch
{ {
Write-Error -ErrorRecord ($_) Write-Error -ErrorRecord ($_)
} }