为什么用脚本运行diskpart无法获得正确的结果?

时间:2014-11-20 06:30:59

标签: powershell

我正在尝试编写一个自动脚本来为VM创建新分区。 当我尝试使用脚本运行diskpart时,它总是失败。

我使用powershell来调用diskpart。 我编写脚本txt文件并将其作为输入传递给diskpart:

diskpart /s script.txt

以下是我的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

它什么也没做,只打印出所有命令,比如我输入命令有语法错误。 我一次又一次地检查了我的脚本,它应该是正确的,如果我在diskpart中逐个运行它们,它们就可以成功运行。

我甚至在我的脚本中尝试了非常基本的命令,例如'list disk',但它仍然具有相同的输出。 我认为这可能是一个非常基本的问题,但我已经搜索了互联网一天仍然没有得到答案。 如果有人能提供帮助或提供任何线索,我们将非常感激。 谢谢。

输出'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.

1 个答案:

答案 0 :(得分:3)

您很可能以Unicode格式保存script.txt。在记事本中打开文件,单击文件→另存为... ,选择" ANSI"从对话框底部的下拉列表编码中,单击"确定"。

PowerShell使用Unicode作为默认编码,因此在从PowerShell创建此类文件时,您需要将编码显式设置为ascii

@"
select disk 0
select partition 1
...
"@ | Out-File 'script.txt' -Encoding ascii