Reputation: 33
I am trying to write a automated script to create new partitions for VM. It always fails when I am trying to run diskpart with script.
I am using powershell to call the diskpart. I write my script txt file and pass it to diskpart as input:
diskpart /s script.txt
Here is the content of my script.txt:
select disk 0
select partition 1
extend size=10240
create partition extended
create partition logical size=10240
assign letter=E
FORMAT FS = NTFS QUICK
create partition logical size=3072
assign letter=p
FORMAT FS = NTFS QUICK
create partition logical
assign letter=y
FORMAT FS = NTFS QUICK
it did nothing but print out all the commands like I type the commands have syntax error. I have checked my scripts again and again, it should be correct, and they can be run successfully if I run them one by one in diskpart.
I have even tried very basic command like 'list disk' in my script but it still have the same output. I think it's probably a very basic question, but I have searched internet for one day still haven't got the answer. It will be very appreciated if anyone can help or give any clues. Thank you.
output for 'diskpart /s script.txt':
Microsoft DiskPart version 6.1.7600
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: AUTOHOSTNAME
Microsoft DiskPart version 6.1.7600
ACTIVE - Mark the selected partition as active.
ADD - Add a mirror to a simple volume.
ASSIGN - Assign a drive letter or mount point to the selected volume.
ATTRIBUTES - Manipulate volume or disk attributes.
ATTACH - Attaches a virtual disk file.
AUTOMOUNT - Enable and disable automatic mounting of basic volumes.
BREAK - Break a mirror set.
CLEAN - Clear the configuration information, or all information, off the
disk.
COMPACT - Attempts to reduce the physical size of the file.
CONVERT - Convert between different disk formats.
CREATE - Create a volume, partition or virtual disk.
DELETE - Delete an object.
DETAIL - Provide details about an object.
DETACH - Detaches a virtual disk file.
EXIT - Exit DiskPart.
EXTEND - Extend a volume.
EXPAND - Expands the maximum size available on a virtual disk.
FILESYSTEMS - Display current and supported file systems on the volume.
FORMAT - Format the volume or partition.
GPT - Assign attributes to the selected GPT partition.
HELP - Display a list of commands.
IMPORT - Import a disk group.
INACTIVE - Mark the selected partition as inactive.
LIST - Display a list of objects.
MERGE - Merges a child disk with its parents.
ONLINE - Online an object that is currently marked as offline.
OFFLINE - Offline an object that is currently marked as online.
RECOVER - Refreshes the state of all disks in the selected pack.
Attempts recovery on disks in the invalid pack, and
resynchronizes mirrored volumes and RAID5 volumes
that have stale plex or parity data.
REM - Does nothing. This is used to comment scripts.
REMOVE - Remove a drive letter or mount point assignment.
REPAIR - Repair a RAID-5 volume with a failed member.
RESCAN - Rescan the computer looking for disks and volumes.
RETAIN - Place a retained partition under a simple volume.
SAN - Display or set the SAN policy for the currently booted OS.
SELECT - Shift the focus to an object.
SETID - Change the partition type.
SHRINK - Reduce the size of the selected volume.
UNIQUEID - Displays or sets the GUID partition table (GPT) identifier or
master boot record (MBR) signature of a disk.
Upvotes: 2
Views: 2998
Reputation: 200473
Most likely you saved script.txt
in Unicode format. Open the file in notepad, click File → Save As..., select "ANSI" from the dropdown list Encoding at the bottom of the dialog, and click "OK".
PowerShell uses Unicode as the default encoding, so you need to explicitly set the encoding to ascii
when creating such files from PowerShell:
@"
select disk 0
select partition 1
...
"@ | Out-File 'script.txt' -Encoding ascii
Upvotes: 4