How to Remotely Initialize a New Disk in VMware?

May 3, 2025 3 answers
Asked by TechWhiz42
I'm trying to automate the process of adding a new hard disk to a VMware virtual machine. The plan is to online the disk, initialize it, partition it, and format itโ€”all remotely. The command below works perfectly when run locally, but it seems to get stuck at the initialize-disk step when I use invoke-command. The disk does go online, but after that, nothing happens. I've tried various ways to structure the commands, including putting everything in one line and using pipelines, but I'm still facing the same issue. Any suggestions on what might be going wrong? Here's the script I'm using: ```powershell $scriptblock = { param($driveletter) $disk = Get-Disk | Where-Object { $_.Partitionstyle -eq 'RAW' -and $_.operationalstatus -eq "Offline" } $disk | Set-Disk -IsOffline $False $disk | Initialize-Disk -PartitionStyle GPT -PassThru $partition = $disk | New-Partition -driveletter $driveletter -UseMaximumSize $partition | Format-Volume -FileSystem NTFS -NewFileSystemLabel "" -allocationunitsize $allocationunitsize -Confirm:$False } $session = New-PSSession -Computername $computername invoke-command -Session $Session -scriptblock $scriptblock -argumentlist $driveletter Remove-PSSession -Computername $computername ```

Answers

ScriptGuru99
It might help to hardcode some parameters in your script block instead of using `params`. Sometimes `params` act weird in script blocks. Also, make sure to check the disk's status separately before the onlining process, as this can also affect the operations, instead of relying on the `Where` clause. Just an if-then check might work better and give you more control over what's going on.
Reply to this answer
ProblemSolver22
You guys pointed me in the right direction! I realized that I needed to update the disk information repeatedly. Just running `Get-Disk` once wasnโ€™t enough; it needed updates to refresh the info. Now that I've included `Update-Disk` multiple times, the whole process is working smoothly. Hereโ€™s my adjusted script for reference: ```powershell $scriptblock = { param([string]$driveletter, [int]$allocationunitsize) $dn = (get-disk | sort-object number | select-object -last 1).number get-disk -number $dn | Set-Disk -IsOffline $False update-disk -number $dn get-disk -number $dn | Initialize-Disk -PartitionStyle GPT -PassThru update-disk -number $dn get-disk -number $dn | New-Partition -driveletter $driveletter -UseMaximumSize update-disk -number $dn get-partition -driveletter $driveletter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "" -allocationunitsize $allocationunitsize -Confirm:$False } $session = New-PSSession -Computername $computername invoke-command -Session $Session -scriptblock $scriptblock -argumentlist $driveletter,$allocationunitsize | out-null Remove-PSSession -Computername $computername ```
Reply to this answer
DevBanter88
I suspect you might need to refresh the disk object's state. Try running `Update-Disk` right before you attempt to initialize it. It sounds like the cached information might be the issue. You definitely need to get a fresh look at the disk properties because `Get-Disk` does not always show the latest status.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions