Powershell Automation Script

Hello Team,

I am working on the automation task with the help of BigFix and backend automation tool where I need to get RAW partition Disk details, then initialize and clean it after that partition creation. Backend automation team only knows the size of the Disk and know what letter need to assign against the size, but they don’t know about Disk Number which is mandatory part to create Partition. I am able to get both the details Disk Number and Disk Size which is correspondent to each other with below command.

Get-Disk | where-object {($.PartitionStyle -contains “RAW”)} | Select-Object {$.Size /1GB},{$_.Number} | ft -HideTableHeaders

      80         0
      10         3
      11         4
      12         5
      13         6
      13         7

Below is the command to create partition. Here you can see DiskNumber we need to enter for New Partition.

New-Partition -DiskNumber 3 -UseMaximumSize -DriveLetter A | Format-Volume -FileSystem NTFS

I am completely stuck here and dont know what to do next to meet the goal. Any mapping can we do in above table so that if automation tool enter the size “13” then it should check in above table and put Disk Number 7 in the Partition command and if they enter size “10” then it should put “3” in Partition command.

I hope i am clear engough to explain the issue and help needed. Kindly help

First of all … When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

Unfortunately not. What is your actual question?

And BTW …

I see the number 13 twice in your table?! Why is it not disk number 6 then? :man_shrugging:t3:
How do you distinguish between disks if there are some with the same size … if the size is your criteria of choice?

If you’re looking for the script to pick the best fitting drive you would want to iterate through each disk (the byte count, not the GB) and store bytes % size requirement from script. Whichever one has the lowest modulus value is your best fit. Then select that disk number.