Jump to content

DiskPart script auto-generation


Go to solution Solved by MHz,

Recommended Posts

I do Windows deployments constantly, and I need to write a program that auto-generates DiskPart scripts.

It will read the pipe input, parse the parameters, and output a text file to the Console. Then you can redirect the text to a file to use with the DiskPart script switch.

But I am having a problem. When I put a number in the partition parameters, for example, "c,20%,1024,*,a", it replaces 1024 in this case with 2100. Which is not what I want.

Here's the code that I'm open-sourcing, and if you can have a look at it, that would be great! If you have the solution, please tell me! I want to get this project done so I can do the WMI disk size tool, dstool, which creates the value to pipe to this program, plctxt.

$DiskSize = 20480
$testData = "c,20%,1024,*,a"
$PartitionParams = StringSplit($testData, ",", 2)
$FinalFile = ""
For $ExamineParams In $PartitionParams
   If $ExamineParams = "a" Then
      $FinalFile = $FinalFile & "active" & @CRLF
   ElseIf $ExamineParams = "c" Then
      $FinalFile = $FinalFile & "clean" & @CRLF
   ElseIf $ExamineParams = "*" Then
      $FinalFile = $FinalFile & "create partition primary" & @CRLF & "format fs=" & "ntfs" & " quick" & @CRLF
   ElseIf $ExamineParams <> StringInStr($ExamineParams, "%") Then
      $MultiString = "0." & StringRegExpReplace($ExamineParams, "[%]", "")
      $ActualGenSize = $DiskSize * $MultiString
      $SizeToReport = Round($ActualGenSize, -1)
      $FinalFile = $FinalFile & "create partition primary size=" & $SizeToReport & @CRLF & "format fs=" & "ntfs" & " quick" & @CRLF
   ElseIf $ExamineParams == StringIsInt($ExamineParams) Then
      $FinalFile = $FinalFile & "create partition primary size=" & $ExamineParams & @CRLF & "format fs=" & "ntfs" & " quick" & @CRLF
   EndIf
Next
ConsoleWrite($FinalFile)

The partitioning commands are as follows:

c = clean partition table

a = set partition as active.

20% = use 20% of space, could be any percentage

1024 = however many megabytes you want your partition to be

* = use the rest of the space on disk

And you use it like this:

dstool \\.\PhysicalDrive0 | plctxt c,20%,a,40%,* ntfs > %userprofile%\Desktop\partlayout.txt

Can you help me?

Thanks,

- 32bitcrusade

Link to comment
Share on other sites

  • Solution

Perhaps this may help as some of the ElseIf conditions seemed illogical.

$DiskSize = 20480
$testData = "c,20%,1024,*,a"
$PartitionParams = StringSplit($testData, ",", 2)
$FinalFile = ""
For $ExamineParams In $PartitionParams
   If $ExamineParams = "a" Then
      $FinalFile &= "active" & @CRLF
   ElseIf $ExamineParams = "c" Then
      $FinalFile &= "clean" & @CRLF
   ElseIf $ExamineParams = "*" Then
      $FinalFile &= "create partition primary" & @CRLF & "format fs=" & "ntfs" & " quick" & @CRLF
   ElseIf StringInStr($ExamineParams, "%") Then
      $MultiString = "0." & StringReplace($ExamineParams, "%", "")
      $ActualGenSize = $DiskSize * $MultiString
      $SizeToReport = Round($ActualGenSize, -1)
      $FinalFile &= "create partition primary size=" & $SizeToReport & @CRLF & "format fs=" & "ntfs" & " quick" & @CRLF
   ElseIf StringIsInt($ExamineParams) Then
      $FinalFile &= "create partition primary size=" & $ExamineParams & @CRLF & "format fs=" & "ntfs" & " quick" & @CRLF
   EndIf
Next
ConsoleWrite($FinalFile)

Outputs:

clean
create partition primary size=4100
format fs=ntfs quick
create partition primary size=1024
format fs=ntfs quick
create partition primary
format fs=ntfs quick
active

I can only guess that is what you want for the output.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...