Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell and diskpart

Tags:

powershell

In short I have a volume that I need to assign a drive letter to (using diskpart). The problem now comes in that the volume does not remain the same. You enter disk part a do a "list volume" and the specific volume would be volume 0, then "exit". Enter again and the do a "list volume" again and this time it is volume 4. And so it continues. Now if this was done by a person it would not be an issue, however this is an automated task, that will "disconnect" the volume on windows 2003 and used on other servers and mounted again on the windows 2003 server.

I'm trying to write a script in powershell that will be able to identify the volume based on a few unique field(s). The problem comes in that I'm getting stuck on interpreting the output of diskpart's "list volume" command with powershell.

The following command provides the output that I need to work with but there after I'm lost.

cls
$dp = "list volume" | diskpart | ? { $_ -match "^  [^-]" }
$dp | format-table  -auto

and this is the output it provides and the volume that I'm looking for is Volume 1.

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  Volume 0     F                       DVD-ROM         0 B  Healthy            
  *Volume 1                             Partition    100 GB  Healthy*            
  Volume 2     E   DATA         NTFS   Partition    547 GB  Healthy            
  Volume 3     C   OS           NTFS   Partition     39 GB  Healthy    System  
  Volume 4     D   APPS         NTFS   Partition     98 GB  Healthy            

Can anybody help me in the right direction here please. I'm at my tether's end.

like image 893
Maartin Avatar asked Jan 21 '23 18:01

Maartin


1 Answers

Yes I got it!!

Here is the answer.
Using VB Script I managed to create a script that did what I was looking for, this I then translated to Powershell and below is the script.

$drive = gwmi Win32_Volume | where {$_.DeviceID -like "*b0f012f6-82b1-11df-a41c-001f29e8f0be*"}
$drive.AddMountPoint("T:\")
$drive.DriveLetter = "T:"
$drive.Put_
$drive.Mount()

The Device ID I obtained by running the following script:

# get volumes on this system
$volumes = get-wmiobject Win32_Volume
# display volume info
# There are {0} volumes on this system, as follows: " -f ($volumes.length)
# Iterate through volumes and display information
foreach ($vol in $volumes) {
    "Volume: {0}" -f ++$i
    "============================="
    $vol | Format-List Access,Automount,Availability,BlockSize,BootVolume,Capacity,Caption,Compressed,ConfigManagerErrorCode,ConfigManagerUserConfig,CreationClassName,Description,DeviceID,DirtyBitSet,DriveLetter,DriveType,ErrorCleared,ErrorDescription,ErrorMethodology,FileSystem,FreeSpace,IndexingEnabled,InstallDate,Label,LastErrorCode,MaximumFileNameLength,Name,NumberOfBlocks,PageFilePresent,PNPDeviceID,PowerManagementCapabilities,PowerManagementSupported,Purpose,QuotasEnabled,QuotasIncomplete,QuotasRebuilding,SerialNumber,Status,StatusInfo,SupportsDiskQuotas,SupportsFileBasedCompression,SystemCreationClassName,SystemName,SystemVolume
}

from a post on msdn on the class Win32_Volume.

I hope this might help somebody else

Thank you to everybody that help!

like image 196
Maartin Avatar answered Feb 04 '23 15:02

Maartin