Author Posts

February 16, 2018 at 3:53 pm

I built a power shell script to automate adding disk to server 2012 r2 via VMro and VMra. The script runs and correctly adds the disk. The script also runs commands to search for new disk, initialize, assign drive letter and format with NTFS. The issue I am having is when I log onto the server. I can see the disk was added, but not formated. UAC and disk format opens up and ask If I want to allow format to run and the I have to click on format for it to complete. How do I get around this? Below is the script I am running. Note we are running this under a domain admin account.

[int] $addGb,

### import VMware PowerCLI module
Get-Module -Name VMware* -ListAvailable | Import-Module

### Connect to the vCenter instance
Connect-VIServer -User $username -Password $password
Connect-VIServer -User $username -Password $password

### Retrieve server object
$server = Get-VM -Name $servername

### Add new drive
$server | New-HardDisk -CapacityGB $addGb

#region –> Call an invoke to extend the OS with loop (no reboot required)
Invoke-Command -ComputerName $server -ScriptBlock {
$VolumeNumber = (Get-Disk | where {$_.partitionstyle -eq 'raw'}).Number
Get-Disk -Number $VolumeNumber | Initialize-Disk -PartitionStyle GPT
New-Partition -DiskNumber $VolumeNumber -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -Confirm:$false -Force
"Guest OS Disk successfully Added and partioned"

February 16, 2018 at 9:14 pm

You are missing ' | ' between commands Initialize-Disk and New-Partition
Probably New-Partition is not getting executed