none
Diskpart /s script.txt not working - invalid commands? RRS feed

  • Question

  • Hi,

    I've written a small PowerShell script which is intended to write a Diskpart script dynamically, execute it and report the results. I'm intending to roll the script out via SCCM in order to resize a single partition as to allow for 2 new partitions to be created. Here is a copy of the PowerShell script. It's being ran on Windows 7:

    # Change the below expression to determine required size TO REDUCE C: - in megabytes.
    $newPartitionSize = 20480
    $desiredReduceSize = ((40*1024)*1.01) # (41943040 megabytes (more 1%) - allows 2 x 20GB partitions and a 1% buffer)
    
    # Calculate current disk space (total size and in use)
    $currentSize = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='c:'" | Select -ExpandProperty Size
    $currentSize = (($currentSize/1024)/1024) # wmiobject outputs in bytes, so convert up to megabytes
    $freeSpace = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='c:'" | Select -ExpandProperty FreeSpace
    $freeSpace = (($freeSpace/1024)/1024) # wmiobject outputs in bytes, so convert up to megabytes
    $spaceInUse  = $currentSize - $freeSpace
    
    
    Write-Host "- Partition resizing script -"
    
    
    if (($currentSize) -le $desiredReduceSize)
    {
         Write-Host "[!] Error, C:\ is already 40GB or less. Not resizing c:\"
         Write-Host "[-] I am still going to try to create the other partitions, though!"
         Write-Host "[-] Lets just hope there's enough unpartitioned space on the disk for it to succeed.."
    }
    
    if ($freeSpace -le $desiredReduceSize) {
         Write-Host "[!] Error, not enough free space to reduce C:\"
         Write-Host "[-] Delete some files and try again! QUITTING!!"
         exit;
    }
    
    # Check to see if the AUDIO directory exists. Delete if it does.
    if (Test-Path C:\AUDIO\ -pathType container) {
        Write-Host "[-] Notice: AUDIO folder already exists. Deleting!"
        Remove-Item "C:\AUDIO\" -recurse 
    }
    
    if (Test-Path C:\DATA\ -pathType container) {
        Write-Host "[-] Notice: DATA folder already exists. Deleting!"
        Remove-Item "C:\DATA\" -recurse 
    }
    
    # Calculate the number of megabytes to shrink c:\ by..
    #$reduceBy = ($currentSize - ($desiredReduceSize))
    $reduceBy = [math]::floor(($desiredReduceSize))
    $sizeCMDToUse = "SHRINK DESIRED=" + $reduceBy + " NOERR"
    
    # delete the diskpart.txt script if it exists...
    if (Test-Path .\diskpartscript.txt) {
       Remove-Item .\diskpartscript.txt
    }
    
    # generate a new diskpart.txt script.................
    Write-Output "REM -- Refresh the hard disks --"  >> .\diskpartscript.txt
    Write-Output "RESCAN" >> .\diskpartscript.txt
    
    Write-Output "REM -- Select the primary hard disk for manipulation --"  >> .\diskpartscript.txt
    Write-Output "SELECT DISK=SYSTEM" >> .\diskpartscript.txt
    Write-Output "" >> .\diskpartscript.txt
    
    Write-Output "REM -- Ensure enough capacity to create partitions --"  >> .\diskpartscript.txt
    Write-Output "REM canIdoifthenelseindiskpart?" >> .\diskpartscript.txt
    
    Write-Output "REM -- Resize primary partition (c:) to 40GB only --" >> .\diskpartscript.txt
    Write-Output "SELECT VOLUME=C" >> .\diskpartscript.txt
    Write-Output $sizeCMDToUse >> .\diskpartscript.txt
    Write-Output "" >> .\diskpartscript.txt
    
    Write-Output "SELECT DISK=SYSTEM" >> .\diskpartscript.txt
    Write-Output "REM -- Create first new 20GB storage volume on primary hard disk (hdd0) --" >> .\diskpartscript.txt
    Write-Output "CREATE PARTITION PRIMARY SIZE=20480 NOERR" >> .\diskpartscript.txt
    Write-Output "REM -- Format the partition to NTFS --" >> .\diskpartscript.txt
    Write-Output "FORMAT FS=NTFS label=""DATA"" QUICK NOERR OVERRIDE" >> .\diskpartscript.txt
    Write-Output "REM -- The partition shouldn't be bootable, anyway --" >> .\diskpartscript.txt
    Write-Output "INACTIVE" >> .\diskpartscript.txt
    Write-Output "REM -- Assign the new 20GB partition to c:\data --" >> .\diskpartscript.txt
    Write-Output "ASSIGN MOUNT=C:\DATA NOERR" >> .\diskpartscript.txt
    Write-Output "" >> .\diskpartscript.txt
    
    Write-Output "REM -- Ok, go back to the root disk for next partition --"  >> .\diskpartscript.txt
    Write-Output "SELECT DISK=SYSTEM" >> .\diskpartscript.txt
    Write-Output "" >> .\diskpartscript.txt
    
    Write-Output "REM -- Create second new 20GB storage volume on primary hard disk (hdd0) --" >> .\diskpartscript.txt
    Write-Output "CREATE PARTITION PRIMARY SIZE=20480 NOERR" >> .\diskpartscript.txt
    Write-Output "REM -- Format the partition to NTFS --" >> .\diskpartscript.txt
    Write-Output "FORMAT FS=NTFS label=""AUDIO"" QUICK NOERR OVERRIDE" >> .\diskpartscript.txt
    Write-Output "REM -- The partition shouldn't be bootable, anyway --" >> .\diskpartscript.txt
    Write-Output "INACTIVE" >> .\diskpartscript.txt
    Write-Output "REM -- Assign the new 20GB partition to c:\audio --" >> .\diskpartscript.txt
    Write-Output "ASSIGN MOUNT=C:\AUDIO NOERR" >> .\diskpartscript.txt
    
    
    # ok, we've completed the diskpart script file. Now call diskpart utility to run that script..
    $DiskPartResult = (diskpart /s .\diskpartscript.txt)
    
    # sort out results... report upon success/failure
    # ?
    
    
    # finishing up, delete temporary files and end script.
    
    if (Test-Path .\diskpartscript.txt) {
    Remove-Item .\diskpartscript.txt # delete our diskpart.txt script (temporary file)
    }
    
    Write-Host "- Resize complete! Script finished. -"
    
    # eof
    

    The script above generates a diskpart script, as listed below...

    REM -- Refresh the hard disks --
    RESCAN
    REM -- Select the primary hard disk for manipulation --
    SELECT DISK=SYSTEM
    
    REM -- Ensure enough capacity to create partitions --
    REM canIdoifthenelseindiskpart?
    REM -- Resize primary partition (c:) to 40GB only --
    SELECT VOLUME=C
    SHRINK DESIRED=41369 NOERR
    
    SELECT DISK=SYSTEM
    REM -- Create first new 20GB storage volume on primary hard disk (hdd0) --
    CREATE PARTITION PRIMARY SIZE=20480 NOERR
    REM -- Format the partition to NTFS --
    FORMAT FS=NTFS label="DATA" QUICK NOERR OVERRIDE
    REM -- The partition shouldn't be bootable, anyway --
    INACTIVE
    REM -- Assign the new 20GB partition to c:\data --
    ASSIGN MOUNT=C:\DATA NOERR
    
    REM -- Ok, go back to the root disk for next partition --
    SELECT DISK=SYSTEM
    
    REM -- Create second new 20GB storage volume on primary hard disk (hdd0) --
    CREATE PARTITION PRIMARY SIZE=20480 NOERR
    REM -- Format the partition to NTFS --
    FORMAT FS=NTFS label="AUDIO" QUICK NOERR OVERRIDE
    REM -- The partition shouldn't be bootable, anyway --
    INACTIVE
    REM -- Assign the new 20GB partition to c:\audio --
    ASSIGN MOUNT=C:\AUDIO NOERR
    

    However, that script doesn't seem to work. Whenever I attempt to execute (just the above diskpart script) via a command prompt, it just presents the diskpart help dialog and doesn't execute the commands. However, if I enter diskpart and paste the above script directly into the diskpart command prompt, the process succeeds.

    Any idea why diskpart.exe isn't processing the script via the command line (via the PowerShell script call), either through the PowerShell script, or via  cmd.exe "diskpart.exe /s .\diskpartscript.txt" ?

    Thanks!

    Monday, January 13, 2014 7:48 AM

Answers

All replies

  • Are you sure that the diskpart script file is closed when you feed it to diskpart?
    Monday, January 13, 2014 1:16 PM
  • Are you sure that the diskpart script file is closed when you feed it to diskpart?
    Monday, January 13, 2014 1:16 PM
  • Write-output does not leave files open.

    The DISKPART command will terminate with an error  which you are not checking.  It is likely that your text file has errors that you are not aware of.

    Save the return text from DISKPART.


    ¯\_(ツ)_/¯

    Monday, January 13, 2014 3:05 PM
  • Hi,

    Check the encoding of your diskpart file. My guess is that diskpart isn't liking the unicode input file.

    EDIT: You can use Out-File .\diskpartscript.txt -Encoding ascii -Append.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)


    • Edited by Mike Laughlin Monday, January 13, 2014 3:56 PM
    • Marked as answer by pdvdff Tuesday, January 14, 2014 4:29 AM
    Monday, January 13, 2014 3:40 PM
  • Hi,

    Check the encoding of your diskpart file. My guess is that diskpart isn't liking the unicode input file.

    EDIT: You can use Out-File .\diskpartscript.txt -Encoding ascii -Append.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)


    Thanks Mike,

    That was exactly the issue. Seems you do need to watch the correct file writing method with the modern variations in character sets (UTF, Unicode, etc).

    After changing Write-Output to Out-File, the script worked immediately!

    I appreciate your assistance and now know a little bit more about the low level differences in file writing methods!

    Tuesday, January 14, 2014 4:31 AM
  • Thank you so much for this. It's pretty infuriating how much time that cost me.
    Tuesday, July 30, 2019 12:28 AM