locked
Creating a diskpart script file using Powershell RRS feed

  • Question

  • Hi,

    I have a very odd (and very annoying) problem...

     

    I have a powershell script which creates a diskpart file to online any given number of iSCSI attached disks.  The powershell script takes input from a csv which includes the disk numbers then creates a txt file with the relevent diskpart commands (e.g select disk X, online disk, etc..)

     

    The powershell script runs without error and creates the output file, however when I attempt to run diskpart with the outputfile (i.e. diskpart /s outputfile.txt) diskpart does not acknowledge/accept the file and just returns a list of valid commands.  Yet if I manually create a empty txt file via Windows Explorer and copy and paste the contents of the powershell generated script file in to it, then run the diskpart against this file all works as expected.

     

    So it seems as though that in writing to the file via powershell something is happening to the file fortmatting which diskpart does not like - any ideas what?

     

    I have tried several work arounds including manually creating the txt file before running the powershell and creating the txt file explicitly from powershell but get the same result.

    Although I can workaround the issue as described above it would be nice to get this working as intended, or least understand why the problem occurs - so any help/advice would (as always) be much appreciated!

    for reference the powershell script is detailed below:

     

    $a="select disk "

    $b="attributes disk clear readonly"

    $c="online disk"

    import-csv .\input.csv | foreach {

     $a+$_.diskNumber >> outputfile.txt

     $b >> outputfile.txt

     $c >> outputfile.txt

    }

    Monday, August 22, 2011 4:08 PM

Answers

  • import-csv .\input.csv | foreach {
    $command = @"
    select disk $($_.diskNumber)
    attributes disk clear readonly
    online disk
    "@
    
    $command | diskpart
    }
    

    • Proposed as answer by Bigteddy Monday, August 22, 2011 4:33 PM
    • Marked as answer by phil_e Tuesday, August 23, 2011 12:25 PM
    Monday, August 22, 2011 4:16 PM

All replies

  • import-csv .\input.csv | foreach {
    $command = @"
    select disk $($_.diskNumber)
    attributes disk clear readonly
    online disk
    "@
    
    $command | diskpart
    }
    

    • Proposed as answer by Bigteddy Monday, August 22, 2011 4:33 PM
    • Marked as answer by phil_e Tuesday, August 23, 2011 12:25 PM
    Monday, August 22, 2011 4:16 PM
  • Your solution works a treat Kazun - than you very much indeed !

     

    Tuesday, August 23, 2011 12:30 PM
  • Kazun,

    Thank you for sharing your script. I tired it but did not work for me. I modified yours slightly to configure 134 SAN devices as mount points.   I've already set the SAN policy to OnlineAll and cofigured 4 other devices. When I run the diskpart commands per disk from CLI its works. Please let me know what I'm missing.Thank you.

    $B = import-csv -Path "C:\Support\Prep_Disks\3-CreateMountPoints\MountPoints.csv"

    $B | Foreach-Object {

    $DiskNum = $_.DiskNumber
    $MP = $_.MountPath
    $DiskLBL = $_.DiskLabel

    $command = @"
    SELECT DISK $DiskNum
    ATTRIBUTES DISK CLEAR READONLY
    ONLINE DISK
    CONVERT MBR
    CREATE PARTITION PRIMARY
    ASSIGN MOUNT=$MP
    FORMAT FS=NTFS UNIT=64k LABEL=$DiskLBL QUICK
    "@

    $command | diskpart
    }


    • Edited by KC2009 Friday, January 27, 2012 6:28 AM
    Friday, January 27, 2012 6:28 AM
  • DiskPart.exe - Powershell Parser for Windows Server 2008 R2

    https://gallery.technet.microsoft.com/DiskPartexe-Powershell-0f7a1bab

    Sunday, October 12, 2014 11:29 PM
  • I know this is very old but I found a simpler solution.  Simply pipe your file to diskpart.

    type outputfile.txt | diskpart

    works like a charm.


    • Proposed as answer by biff512 Friday, January 23, 2015 4:22 PM
    • Edited by biff512 Friday, January 23, 2015 4:22 PM
    Friday, January 23, 2015 4:21 PM