Updated on 2024-09-06 GMT+08:00

Initializing Windows Data Disks Using a Script

Scenarios

This section uses Windows Server 2008 R2 Enterprise 64bit to describe how to automatically initialize data disks attached to a server running Windows.

The formatting procedure varies depending on the server OS. This section is for reference only.

When using a disk for the first time, if you have not initialized it, including creating partitions and file systems, the additional space added to this disk in an expansion later may not be normally used.

Prerequisites

  • You have attached a data disk to a server but not initialized the disk.
  • You have logged in to the server.

Notes and Constraints

  • This script uses the NTFS file system and MBR partition style by default.
  • This script does not support the initialization of disks that form Windows logical volume groups.
  • This script supports the initialization of up to 23 Windows data disks.
  • Only data disks in Windows simplified Chinese and English can be initialized.

Manually Executing the Script to Initialize Windows Data Disks

  1. Log in to the server, visit https://ecs-instance-driver.obs.cn-north-1.myhuaweicloud.com/datadisk/WinVMDataDiskAutoInitialize.ps1 to obtain the automatic disk initialization script, and save it to C:\Windows\System32.
  2. On the server desktop, click Start, right-click Windows PowerShell, and choose Run as administrator from the shortcut menu.

    Figure 1 Run Windows PowerShell as administrator

  3. In the displayed dialog box, run the following command to set the script execution policy:

    Set-ExecutionPolicy -ExecutionPolicy Bypass -Force

  4. Run the following command in the dialog box to execute the initialization script:

    C:\Windows\System32\WinVMDataDiskAutoInitialize.ps1

    Information similar to the following is displayed:

    If the command output shows that PowerShell cannot load the file, the reason is that the Windows system policy does not allow the PowerShell script downloaded from the network to be executed when the script is not signed.

    To solve this problem, run the set-ExecutionPolicy RemoteSigned command and restart PowerShell.

  5. (Optional) On the Disk Management page, view the initialization result.

    Figure 2 Disk Management

Configuring Automatic Initialization of Windows Data Disks

You can configure automatic initialization for Windows data disks. After the configuration is complete, new disks purchased and attached to this server will be automatically initialized after the server is logged in to again.

To enable automatic initialization of Windows data disks on multiple servers, configure automatic initialization on one server, create a private image for this server, and use the image to batch create servers.

  1. On the server desktop, click Start, right-click Computer in the list, and choose Manage from the shortcut menu.

    The Server Manager window is displayed.

  2. In the navigation tree on the left, choose Configuration > Task Scheduler > Task Scheduler Library, then right-click and choose Create Task from the shortcut menu.

    The Create Task page is displayed.

  3. On the displayed page, click the General tab, enter WinVMDataDiskInitialize in the Name text box, and click Change User or Group.

    The Select User or Group page is displayed.

  4. Click Advanced in the lower left corner of the page.
  5. Click Find Now to the right of the page. In the search result list, select SYSTEM and click OK.

    The Select User or Group page is displayed.

  6. Click OK.

  7. Click the Triggers tab and click Create in the lower left corner of the page.
  8. On the New Trigger page, choose At log on for Begin the task, retain the default settings for other parameters, and click OK.

  9. Click the Actions tab and click Create in the lower left corner of the page.
  10. On the Edit Action page, set the following parameters:

    • Action: Choose Start a program.
    • Program/Script: Select powershell.exe.
    • Add arguments (optional): Enter -ExecutionPolicy Bypass C:\Windows\System32\WinVMDataDiskAutoInitialize.ps1.

    Click OK.

  11. On the Create Task page, click OK.