I have a script that checks whether there are drives with certain letters. If that letter exists, it will removes these letter. Then the script goes looking for drives according to their 'labels' in changes their letters according to what is specified in the script.
I'm looking to perfect the script so that in the first part it will look for any drives with the letters E or D, and if so, remove them. If it is not found, then just go to the second part of changing according to the label of the disk.
Get-Volume -DriveLetter E | Get-Partition | Remove-PartitionAccessPath -AccessPath E:\
Get-Volume -DriveLetter D | Get-Partition | Remove-PartitionAccessPath -AccessPath D:\
$DataPartition = Get-WMIObject Win32_Volume | where{ $_.Label -eq 'Data'}
$FileServerPartition = Get-WMIObject Win32_Volume | where{ $_.Label -eq 'FileServer'}
$DataPartition.DriveLetter = $null
$DataPartition.Put()
$FileServerPartition.DriveLetter = $null
$FileServerPartition.Put()
Try
{
Set-WmiInstance -input $DataPartition -Arguments @{DriveLetter="D:"} | Out-File -FilePath C:\Windows\Temp\FixPartitionsLog.txt -Append
Set-WmiInstance -input $FileServerPartition -Arguments @{DriveLetter="E:"} | Out-File -FilePath C:\Windows\Temp\FixPartitionsLog.txt -Append
}
Catch
{
$ErrorMessage = $_.Exception.Message | Out-File -FilePath C:\Windows\Temp\FixPartitionsLog.txt
}
sleep 5
Restart-Service server -Force
This was the solution I used in the end:
$driveLetters= (Get-Volume).DriveLetter
if ($driveLetters -contains "d" -or $driveLetters -contains "e")
{
switch ($driveLetters)
{
"d" {Get-Volume -DriveLetter D | Get-Partition | Remove-PartitionAccessPath -AccessPath D:\}
"e" {Get-Volume -DriveLetter E | Get-Partition | Remove-PartitionAccessPath -AccessPath E:\}
}
}
$DataPartition = Get-WMIObject Win32_Volume | where{ $_.Label -eq 'Data'}
$FileServerPartition = Get-WMIObject Win32_Volume | where{ $_.Label -eq 'FileServer'}
$DataPartition.DriveLetter = $null
$DataPartition.Put()
$FileServerPartition.DriveLetter = $null
$FileServerPartition.Put()
Try
{
Set-WmiInstance -input $DataPartition -Arguments @{DriveLetter="D:"} | Out-File -FilePath C:\Windows\Temp\FixPartitionsLog.txt -Append
Set-WmiInstance -input $FileServerPartition -Arguments @{DriveLetter="E:"} | Out-File -FilePath C:\Windows\Temp\FixPartitionsLog.txt -Append
}
Catch
{
$ErrorMessage = $_.Exception.Message | Out-File -FilePath C:\Windows\Temp\FixPartitionsLog.txt
}
sleep 5
Restart-Service server -Force
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With