How to Remotely Initialize a New Disk in VMware?

0
2
Asked By TechWhiz42 On

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
```

3 Answers

Answered By DevBanter88 On

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.

Answered By ProblemSolver22 On

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
```

Answered By ScriptGuru99 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.