我正在尝试编写一个自动化脚本来为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.

最佳答案

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

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

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

关于powershell - 为什么用脚本运行diskpart无法获得正确的结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27033203/

10-13 05:50