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

标签 powershell

我正在尝试编写一个自动化脚本来为虚拟机创建新分区。 当我尝试使用脚本运行 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中一一运行就能成功。

我什至在我的脚本中尝试了非常基本的命令,例如“列出磁盘”,但它仍然具有相同的输出。 我认为这可能是一个非常基本的问题,但我在网上搜索了一天仍然没有得到答案。 如果有人可以提供帮助或提供任何线索,我们将不胜感激。 谢谢。

“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/

相关文章:

c# - 从 C# 以编程方式调用 CmdLet 时如何捕获 Powershell CmdLet 的对象输出

c# - Invoke-WebRequest, GET 带参数抛出异常

powershell - Powershell import-csv拆分和匹配单元格内容

powershell - Powershell让过去6个月内被禁用的AD用户感到困惑?

powershell - 如何在PowerShell中的FTP服务器上查询文件,以确定是否需要上传?

azure - 如何通过Powershell获取新创建的Azure VM的IP地址

powershell - 有条件地替换 PowerShell 对象中的值

powershell - 避免文件外出现空白行

powershell - 如何在 Powershell 中设置从 3 小时前到现在的时间范围?

powershell - 将目录的父目录与where管道中的另一个目录进行比较