Create Diskpart Answer file

Hi,

Am trying to create a Diskpart answer file, below is the script I use. It divided into 2 functions. GET-DISKPARTINFO & INVOKE-EDriveConfig.
GET-DISKPARTINFO works fine and am able to get all the info but the problem is on the function INVOKE-EDriveConfig.

$drivelist & all the $_ do not have any value. Can you help and let me what I am doing wrong.

The final result generated the bootemup.txt but its missing the disk number & dont look like the IF conduction is also working cause there is no value in $_Status and it would always return false.

Could you please help !!

[attachment file=“DiskConfig_v2.txt”]

This line is wrong:

$drivelist=$info | where { $_.Type -eq 'SAS' -and $_.DiskSize -gt 9GB -and $_.DiskSize -lt 11GB }

You can’t do a comparison of the size that way. The DiskSize property is a string and it should be an int for the -gt and -lt to work.

Your best bet is to remove the GB at the end then convert it to an int. Then you can do comparisons with another int.

That being said, parsing the text output of a command line application seems like the most difficult way to get the size and type of a local disk. You’re better off using a Win32_LogicalDisk WMI call.

Hi Sean,

Have tried to convert it to int

$intsize=$size.substring(0,$length-2)

but I get the following error
Exception calling “Substring” with “2” argument(s): “Length cannot be less than zero.
Parameter name: length”
At C:\Install\DiskConfig\INVOKE-EDriveConfig.ps1:45 char:25

  • $intsize=$size.substring <<<< (0,($length-2))
    • CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
    • FullyQualifiedErrorId : DotNetMethodException

Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.
Parameter name: length"
At C:\Install\DiskConfig\INVOKE-EDriveConfig.ps1:45 char:25

  • $intsize=$size.substring <<<< (0,($length-2))
    • CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
    • FullyQualifiedErrorId : DotNetMethodException

You don’t define $length, so by default it’s $null. And $null-2 is negative 2. The error states that you cannot have a length below 0.

I understand that you may have spent some time getting this to work but i think using " Get-WMIobject Win32_DiskDrive" in place of Diskpart would probably be much easier and you may not need 2 separate functions to get the results you need.
Also use “Here-Strings” instead of add-content for multiple lines of text.

Note: before you do a size comparison you will have to make sure that the source and destination sizes are in the appropriate format (i.e, Compare MB - MB or GB - GB Eg: 102456 -gt 102450 or 3 -gt 2 )