build 7bbbddf7 | content blog-content@c8490fa · 338 posts | profiles 20 · corpus 267 | 0 skipped | | format
apiVersion: soultec.ch/v1kind: Postmetadata: name: powercli-adding-a-disk-to-a-vm locale: en labels: author: dario-doerflinger series: scripting capability/automation: 1.78 vendor/vmware: 0.88 annotations: source: blog-content/posts/en/powercli-adding-a-disk-to-a-vm.md route: /en/insights/powercli-adding-a-disk-to-a-vm/ schema: /nerd/schema/posts.json markdown: /en/insights/powercli-adding-a-disk-to-a-vm.mdspec: title: Adding a disk to a VM date: 2017-10-04 author: dario-doerflinger locale: en summary: >- Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM. capabilities: [automation] vendors: [vmware] series: scripting legacySlug: powercli-adding-a-disk-to-a-vm migrated: 2026-08-24 draft: false sections: - body: | Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM. The error message was very generic (and was in the end fixed with a reboot) so I had to find a alternative: Here comes PowerCLI. The requirements were as follows: - size of the disk in GB must be specified - it should be possible to add a new scsi controller if one chooses to do so - it should be possible to specify which already present scsi controller should be used - if no option is specified always add the disk to the last scsi controller It was easy enough to get the script working. One thing I learned was that "new-scsicontroller" requires the VM to be in the "PoweredOff" State. Update: this script is now published at [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts). Here is the script: ```powershell <# .EXAMPLE PS> Add-DiskToVm.ps1 -vMName reg-belairi81 -vCenter virtualfrogvc.virtual.frog -diskGB 10 This will attach a 10 GB disk to the VM on the last of its scsi controllers .EXAMPLE PS> Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -addController:$true -controllerType paravirtual This will attach a 10 GB disk to the VM and attach it to a new scsi controller of type paravirtual .EXAMPLE PS> Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -controllerNumber 2 This will attach a 10 GB disk to the VM and attach it to the second controller on the VM #> ################################################################################## # Script: Add-DiskToVm.ps1 # Datum: 04.10.2017 # Author: Bechtle Steffen Schweiz AG (c) 2017 # Version: 1.0 # History: Check VMs current set of SCSI Controllers when adding Disks ################################################################################## [CmdletBinding(SupportsShouldProcess=$true)] Param( [parameter()] [string]$vCenter = "virtualfrogvc.virtual.frog", # Change to default VM for testing [string]$vMName = "reg-belairi82", # Change default Disk Size [decimal]$diskGB = 2.5, # Hardcode the SCSI Controller # (like 3 for the third controller) [int]$controllerNumber = 2000, # Add new SCSI Controller while you're at it [boolean]$addController = $false, # Type of SCSI Controller to add (paravirtual|VirtualLsiLogicSAS) [string]$controllerType = "paravirtual" ) function get-scsiCount ($vm) { try { return ($vm | get-scsicontroller -ErrorAction Stop).count } catch { Write-Host "Could not count Scsi Controller of $vm" exit } } function get-scsiID ($vm, $number) { try { return ($vm |get-scsicontroller |select -skip ($number-1) -first 1).ID } catch { Write-Host "Could not get scsi controller number $number from $vm" exit } } function get-scsiType ($vm, $id) { try { return ($vm |get-scsicontroller -ID $id -ErrorAction Stop).Type } catch { Write-Host "Could not determine type of SCSI Controller on vm ($vm)" exit } } function add-DiskToVmOnController ($vm, $controller) { try { New-Harddisk -Controller $controller -CapacityGB $diskGB -VM $vm -Whatif -ErrorAction Stop } catch { Write-Host "Could not add disk to VM ($vm)" exit } } function shutDownVm ($vm) { try { Stop-VMGuest -VM $vm -confirm:$false -ErrorAction Stop Write-Host "Successfully send the shutdown command over VMware Tools" } catch { Write-Host -Foregroundcolor:red "The VM did not respond to a VMware tools shutdown command" $switch = Read-Host -Prompt "Would you like to Power off the VM $vm ? (yes/no)" if ($switch -match "yes") { Stop-VM -VM $vm -confirm:$false } else { Write-Host "You chose not to power off the VM. Stopping the script.." exit } } while ((get-vm $vMName).PowerState -notmatch "PoweredOff") { Write-Host "Waiting for $vm to shut down..." sleep -s 5 } $vmHasShutDown = $true } ####### Main Program ###### try { Import-Module -Name VMware.VimAutomation.Core -ErrorAction Stop | Out-Null } catch { Write-Host "Could not add VMware PowerCLI Modules" exit } try { Connect-VIServer $vCenter -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null } catch { Write-Host "Could not connect to vCenter $vCenter" exit } Write-Host "Connected to $vCenter. Starting script" try { $vm = Get-VM $vMName -ErrorAction Stop } catch { Write-Host "Could not find VM with Name $vMName in vCenter $vCenter" exit } if ($addController) { if ($vm.PowerState -match "PoweredOn") { Write-Host -Foregroundcolor:red "The VM is still powered On." $switch = Read-Host -Prompt "Would you like to shut down the VM ($vm)? (yes/no)" if ($switch -match "yes") { shutDownVm $vm } else { Write-Host "You chose not to shutdown the VM ($vm). Stopping the script now" exit } } try { $vm |New-Harddisk -CapacityGB $diskGB |new-scsicontroller -type $controllerType -ErrorAction Stop if ($vmHasShutDown) { $switch = Read-Host -Prompt "The VM was shut down for this operation. Power it back on? (yes/no)" if ($switch -match "yes") { Start-VM $vm -confirm:$false } } } catch { Write-Host "could not add scsi controller with new disk to $vm" exit } } elseif ($controllerNumber -ne 2000) { $numberOfControllers = get-scsiCount $vm if ($numberOfControllers -gt $controllerNumber) { Write-Host "You specified controller number $controllerNumber but the VM ($vm) only has $numberOfControllers controllers" exit } else { $scsiID = get-scsiID $vm $controllerNumber Write-Host "The VM ($vm) has $numberOfControllers SCSI Controller(s) attached. You chose to attach a new disk to the $controllerNumber. adapter" Write-Host "The VM ($vm) has a "(get-scsiType $vm $scsiID)" Controller for the number you provided" add-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID) Write-Host "Added a disk of $diskGB GB to $vm on controller "($vm | get-scsicontroller -ID $scsiID).Name } } else { $numberOfControllers = get-scsiCount $vm $scsiID = get-scsiID $vm $numberOfControllers Write-Host "The VM ($vm) has $numberOfControllers SCSI Controller(s) attached" Write-Host "The VM ($vm) has a "(get-scsiType $vm $scsiID)" Controller as the last one" add-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID) Write-Host "Added a disk of $diskGB GB to $vm on controller "($vm | get-scsicontroller -ID $scsiID).Name } ```status: corpus: 267 alsoLike: - {ref: posts/vmware-explore-las-vegas-hackathon-2025, score: 1.00} - {ref: posts/vmware-hackathon-2024-project, score: 1.00} - {ref: solutions/vmware/vmware-cloud-foundation/addon/application-services, score: 0.60}
{ "apiVersion": "soultec.ch/v1", "kind": "Post", "metadata": { "name": "powercli-adding-a-disk-to-a-vm", "locale": "en", "labels": { "author": "dario-doerflinger", "series": "scripting", "capability/automation": "1.78", "vendor/vmware": "0.88" }, "annotations": { "source": "blog-content/posts/en/powercli-adding-a-disk-to-a-vm.md", "route": "/en/insights/powercli-adding-a-disk-to-a-vm/", "schema": "/nerd/schema/posts.json", "markdown": "/en/insights/powercli-adding-a-disk-to-a-vm.md" } }, "spec": { "title": "Adding a disk to a VM", "date": "2017-10-04", "author": "dario-doerflinger", "locale": "en", "summary": "Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM.", "capabilities": [ "automation" ], "vendors": [ "vmware" ], "series": "scripting", "legacySlug": "powercli-adding-a-disk-to-a-vm", "migrated": "2026-08-24", "draft": false }, "sections": [ { "body": "Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM. The error message was very generic (and was in the end fixed with a reboot) so I had to find a alternative: Here comes PowerCLI.\n\nThe requirements were as follows:\n\n- size of the disk in GB must be specified\n- it should be possible to add a new scsi controller if one chooses to do so\n- it should be possible to specify which already present scsi controller should be used\n- if no option is specified always add the disk to the last scsi controller\n\nIt was easy enough to get the script working. One thing I learned was that \"new-scsicontroller\" requires the VM to be in the \"PoweredOff\" State.\n\nUpdate: this script is now published at [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).\n\nHere is the script:\n\n```powershell\n<#\n.EXAMPLE\nPS> Add-DiskToVm.ps1 -vMName reg-belairi81 -vCenter virtualfrogvc.virtual.frog -diskGB 10\n\nThis will attach a 10 GB disk to the VM on the last of its scsi controllers\n.EXAMPLE\nPS> Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -addController:$true -controllerType paravirtual\n\nThis will attach a 10 GB disk to the VM and attach it to a new scsi controller of type paravirtual\n.EXAMPLE\nPS> Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -controllerNumber 2\n\nThis will attach a 10 GB disk to the VM and attach it to the second controller on the VM\n#>\n\n##################################################################################\n# Script: Add-DiskToVm.ps1\n# Datum: 04.10.2017\n# Author: Bechtle Steffen Schweiz AG (c) 2017\n# Version: 1.0\n# History: Check VMs current set of SCSI Controllers when adding Disks\n##################################################################################\n\n[CmdletBinding(SupportsShouldProcess=$true)]\nParam(\n[parameter()]\n[string]$vCenter = \"virtualfrogvc.virtual.frog\",\n# Change to default VM for testing\n[string]$vMName = \"reg-belairi82\",\n# Change default Disk Size\n[decimal]$diskGB = 2.5,\n# Hardcode the SCSI Controller # (like 3 for the third controller)\n[int]$controllerNumber = 2000,\n# Add new SCSI Controller while you're at it\n[boolean]$addController = $false,\n# Type of SCSI Controller to add (paravirtual|VirtualLsiLogicSAS)\n[string]$controllerType = \"paravirtual\"\n\n)\nfunction get-scsiCount ($vm)\n{\ntry {\nreturn ($vm | get-scsicontroller -ErrorAction Stop).count\n}\ncatch {\nWrite-Host \"Could not count Scsi Controller of $vm\"\nexit\n}\n}\n\nfunction get-scsiID ($vm, $number)\n{\ntry {\nreturn ($vm |get-scsicontroller |select -skip ($number-1) -first 1).ID\n}\ncatch {\nWrite-Host \"Could not get scsi controller number $number from $vm\"\nexit\n}\n}\n\nfunction get-scsiType ($vm, $id)\n{\ntry {\nreturn ($vm |get-scsicontroller -ID $id -ErrorAction Stop).Type\n}\ncatch {\nWrite-Host \"Could not determine type of SCSI Controller on vm ($vm)\"\nexit\n}\n}\n\nfunction add-DiskToVmOnController ($vm, $controller)\n{\ntry {\nNew-Harddisk -Controller $controller -CapacityGB $diskGB -VM $vm -Whatif -ErrorAction Stop\n} catch {\nWrite-Host \"Could not add disk to VM ($vm)\"\nexit\n}\n}\n\nfunction shutDownVm ($vm)\n{\ntry {\nStop-VMGuest -VM $vm -confirm:$false -ErrorAction Stop\nWrite-Host \"Successfully send the shutdown command over VMware Tools\"\n}\ncatch {\nWrite-Host -Foregroundcolor:red \"The VM did not respond to a VMware tools shutdown command\"\n$switch = Read-Host -Prompt \"Would you like to Power off the VM $vm ? (yes/no)\"\nif ($switch -match \"yes\") {\nStop-VM -VM $vm -confirm:$false\n} else {\nWrite-Host \"You chose not to power off the VM. Stopping the script..\"\nexit\n}\n}\n\nwhile ((get-vm $vMName).PowerState -notmatch \"PoweredOff\")\n{\nWrite-Host \"Waiting for $vm to shut down...\"\nsleep -s 5\n}\n$vmHasShutDown = $true\n}\n\n####### Main Program ######\ntry {\nImport-Module -Name VMware.VimAutomation.Core -ErrorAction Stop | Out-Null\n} catch {\nWrite-Host \"Could not add VMware PowerCLI Modules\"\nexit\n}\n\ntry {\nConnect-VIServer $vCenter -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null\n}\ncatch {\nWrite-Host \"Could not connect to vCenter $vCenter\"\nexit\n}\n\nWrite-Host \"Connected to $vCenter. Starting script\"\n\ntry {\n$vm = Get-VM $vMName -ErrorAction Stop\n} catch {\nWrite-Host \"Could not find VM with Name $vMName in vCenter $vCenter\"\nexit\n}\nif ($addController) {\nif ($vm.PowerState -match \"PoweredOn\") {\nWrite-Host -Foregroundcolor:red \"The VM is still powered On.\"\n$switch = Read-Host -Prompt \"Would you like to shut down the VM ($vm)? (yes/no)\"\nif ($switch -match \"yes\") {\nshutDownVm $vm\n\n} else {\nWrite-Host \"You chose not to shutdown the VM ($vm). Stopping the script now\"\nexit\n}\n}\ntry {\n$vm |New-Harddisk -CapacityGB $diskGB |new-scsicontroller -type $controllerType -ErrorAction Stop\nif ($vmHasShutDown) {\n$switch = Read-Host -Prompt \"The VM was shut down for this operation. Power it back on? (yes/no)\"\nif ($switch -match \"yes\") {\nStart-VM $vm -confirm:$false\n}\n}\n} catch {\nWrite-Host \"could not add scsi controller with new disk to $vm\"\nexit\n}\n} elseif ($controllerNumber -ne 2000) {\n$numberOfControllers = get-scsiCount $vm\nif ($numberOfControllers -gt $controllerNumber) {\nWrite-Host \"You specified controller number $controllerNumber but the VM ($vm) only has $numberOfControllers controllers\"\nexit\n} else {\n$scsiID = get-scsiID $vm $controllerNumber\nWrite-Host \"The VM ($vm) has $numberOfControllers SCSI Controller(s) attached. You chose to attach a new disk to the $controllerNumber. adapter\"\n\nWrite-Host \"The VM ($vm) has a \"(get-scsiType $vm $scsiID)\" Controller for the number you provided\"\nadd-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID)\nWrite-Host \"Added a disk of $diskGB GB to $vm on controller \"($vm | get-scsicontroller -ID $scsiID).Name\n}\n}\nelse {\n$numberOfControllers = get-scsiCount $vm\n$scsiID = get-scsiID $vm $numberOfControllers\nWrite-Host \"The VM ($vm) has $numberOfControllers SCSI Controller(s) attached\"\n\nWrite-Host \"The VM ($vm) has a \"(get-scsiType $vm $scsiID)\" Controller as the last one\"\nadd-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID)\nWrite-Host \"Added a disk of $diskGB GB to $vm on controller \"($vm | get-scsicontroller -ID $scsiID).Name\n}\n```" } ], "status": { "corpus": 267, "alsoLike": [ { "ref": "posts/vmware-explore-las-vegas-hackathon-2025", "score": "1.00" }, { "ref": "posts/vmware-hackathon-2024-project", "score": "1.00" }, { "ref": "solutions/vmware/vmware-cloud-foundation/addon/application-services", "score": "0.60" } ] }}
apiVersion = "soultec.ch/v1"kind = "Post"[metadata]name = "powercli-adding-a-disk-to-a-vm"locale = "en"[metadata.labels]author = "dario-doerflinger"series = "scripting""capability/automation" = "1.78""vendor/vmware" = "0.88"[metadata.annotations]source = "blog-content/posts/en/powercli-adding-a-disk-to-a-vm.md"route = "/en/insights/powercli-adding-a-disk-to-a-vm/"schema = "/nerd/schema/posts.json"markdown = "/en/insights/powercli-adding-a-disk-to-a-vm.md"[spec]title = "Adding a disk to a VM"date = 2017-10-04author = "dario-doerflinger"locale = "en"summary = "Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM."capabilities = ["automation"]vendors = ["vmware"]series = "scripting"legacySlug = "powercli-adding-a-disk-to-a-vm"migrated = 2026-08-24draft = false[[sections]]body = '''Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM. The error message was very generic (and was in the end fixed with a reboot) so I had to find a alternative: Here comes PowerCLI.The requirements were as follows:- size of the disk in GB must be specified- it should be possible to add a new scsi controller if one chooses to do so- it should be possible to specify which already present scsi controller should be used- if no option is specified always add the disk to the last scsi controllerIt was easy enough to get the script working. One thing I learned was that "new-scsicontroller" requires the VM to be in the "PoweredOff" State.Update: this script is now published at [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).Here is the script:```powershell<#.EXAMPLEPS> Add-DiskToVm.ps1 -vMName reg-belairi81 -vCenter virtualfrogvc.virtual.frog -diskGB 10This will attach a 10 GB disk to the VM on the last of its scsi controllers.EXAMPLEPS> Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -addController:$true -controllerType paravirtualThis will attach a 10 GB disk to the VM and attach it to a new scsi controller of type paravirtual.EXAMPLEPS> Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -controllerNumber 2This will attach a 10 GB disk to the VM and attach it to the second controller on the VM#>################################################################################### Script: Add-DiskToVm.ps1# Datum: 04.10.2017# Author: Bechtle Steffen Schweiz AG (c) 2017# Version: 1.0# History: Check VMs current set of SCSI Controllers when adding Disks##################################################################################[CmdletBinding(SupportsShouldProcess=$true)]Param([parameter()][string]$vCenter = "virtualfrogvc.virtual.frog",# Change to default VM for testing[string]$vMName = "reg-belairi82",# Change default Disk Size[decimal]$diskGB = 2.5,# Hardcode the SCSI Controller # (like 3 for the third controller)[int]$controllerNumber = 2000,# Add new SCSI Controller while you're at it[boolean]$addController = $false,# Type of SCSI Controller to add (paravirtual|VirtualLsiLogicSAS)[string]$controllerType = "paravirtual")function get-scsiCount ($vm){try {return ($vm | get-scsicontroller -ErrorAction Stop).count}catch {Write-Host "Could not count Scsi Controller of $vm"exit}}function get-scsiID ($vm, $number){try {return ($vm |get-scsicontroller |select -skip ($number-1) -first 1).ID}catch {Write-Host "Could not get scsi controller number $number from $vm"exit}}function get-scsiType ($vm, $id){try {return ($vm |get-scsicontroller -ID $id -ErrorAction Stop).Type}catch {Write-Host "Could not determine type of SCSI Controller on vm ($vm)"exit}}function add-DiskToVmOnController ($vm, $controller){try {New-Harddisk -Controller $controller -CapacityGB $diskGB -VM $vm -Whatif -ErrorAction Stop} catch {Write-Host "Could not add disk to VM ($vm)"exit}}function shutDownVm ($vm){try {Stop-VMGuest -VM $vm -confirm:$false -ErrorAction StopWrite-Host "Successfully send the shutdown command over VMware Tools"}catch {Write-Host -Foregroundcolor:red "The VM did not respond to a VMware tools shutdown command"$switch = Read-Host -Prompt "Would you like to Power off the VM $vm ? (yes/no)"if ($switch -match "yes") {Stop-VM -VM $vm -confirm:$false} else {Write-Host "You chose not to power off the VM. Stopping the script.."exit}}while ((get-vm $vMName).PowerState -notmatch "PoweredOff"){Write-Host "Waiting for $vm to shut down..."sleep -s 5}$vmHasShutDown = $true}####### Main Program ######try {Import-Module -Name VMware.VimAutomation.Core -ErrorAction Stop | Out-Null} catch {Write-Host "Could not add VMware PowerCLI Modules"exit}try {Connect-VIServer $vCenter -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null}catch {Write-Host "Could not connect to vCenter $vCenter"exit}Write-Host "Connected to $vCenter. Starting script"try {$vm = Get-VM $vMName -ErrorAction Stop} catch {Write-Host "Could not find VM with Name $vMName in vCenter $vCenter"exit}if ($addController) {if ($vm.PowerState -match "PoweredOn") {Write-Host -Foregroundcolor:red "The VM is still powered On."$switch = Read-Host -Prompt "Would you like to shut down the VM ($vm)? (yes/no)"if ($switch -match "yes") {shutDownVm $vm} else {Write-Host "You chose not to shutdown the VM ($vm). Stopping the script now"exit}}try {$vm |New-Harddisk -CapacityGB $diskGB |new-scsicontroller -type $controllerType -ErrorAction Stopif ($vmHasShutDown) {$switch = Read-Host -Prompt "The VM was shut down for this operation. Power it back on? (yes/no)"if ($switch -match "yes") {Start-VM $vm -confirm:$false}}} catch {Write-Host "could not add scsi controller with new disk to $vm"exit}} elseif ($controllerNumber -ne 2000) {$numberOfControllers = get-scsiCount $vmif ($numberOfControllers -gt $controllerNumber) {Write-Host "You specified controller number $controllerNumber but the VM ($vm) only has $numberOfControllers controllers"exit} else {$scsiID = get-scsiID $vm $controllerNumberWrite-Host "The VM ($vm) has $numberOfControllers SCSI Controller(s) attached. You chose to attach a new disk to the $controllerNumber. adapter"Write-Host "The VM ($vm) has a "(get-scsiType $vm $scsiID)" Controller for the number you provided"add-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID)Write-Host "Added a disk of $diskGB GB to $vm on controller "($vm | get-scsicontroller -ID $scsiID).Name}}else {$numberOfControllers = get-scsiCount $vm$scsiID = get-scsiID $vm $numberOfControllersWrite-Host "The VM ($vm) has $numberOfControllers SCSI Controller(s) attached"Write-Host "The VM ($vm) has a "(get-scsiType $vm $scsiID)" Controller as the last one"add-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID)Write-Host "Added a disk of $diskGB GB to $vm on controller "($vm | get-scsicontroller -ID $scsiID).Name}```'''[status]corpus = 267[[status.alsoLike]]ref = "posts/vmware-explore-las-vegas-hackathon-2025"score = "1.00"[[status.alsoLike]]ref = "posts/vmware-hackathon-2024-project"score = "1.00"[[status.alsoLike]]ref = "solutions/vmware/vmware-cloud-foundation/addon/application-services"score = "0.60"
<?xml version="1.0" encoding="UTF-8"?><manifest kind="Post"> <apiVersion>soultec.ch/v1</apiVersion> <metadata> <name>powercli-adding-a-disk-to-a-vm</name> <locale>en</locale> <labels> <author>dario-doerflinger</author> <series>scripting</series> <entry key="capability/automation">1.78</entry> <entry key="vendor/vmware">0.88</entry> </labels> <annotations> <source>blog-content/posts/en/powercli-adding-a-disk-to-a-vm.md</source> <route>/en/insights/powercli-adding-a-disk-to-a-vm/</route> <schema>/nerd/schema/posts.json</schema> <markdown>/en/insights/powercli-adding-a-disk-to-a-vm.md</markdown> </annotations> </metadata> <spec> <title>Adding a disk to a VM</title> <date>2017-10-04</date> <author>dario-doerflinger</author> <locale>en</locale> <summary>Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM.</summary> <capabilities> <item>automation</item> </capabilities> <vendors> <item>vmware</item> </vendors> <series>scripting</series> <legacySlug>powercli-adding-a-disk-to-a-vm</legacySlug> <migrated>2026-08-24</migrated> <draft>false</draft> </spec> <sections> <section> <body>Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM. The error message was very generic (and was in the end fixed with a reboot) so I had to find a alternative: Here comes PowerCLI.The requirements were as follows:- size of the disk in GB must be specified- it should be possible to add a new scsi controller if one chooses to do so- it should be possible to specify which already present scsi controller should be used- if no option is specified always add the disk to the last scsi controllerIt was easy enough to get the script working. One thing I learned was that "new-scsicontroller" requires the VM to be in the "PoweredOff" State.Update: this script is now published at [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).Here is the script:```powershell&lt;#.EXAMPLEPS&gt; Add-DiskToVm.ps1 -vMName reg-belairi81 -vCenter virtualfrogvc.virtual.frog -diskGB 10This will attach a 10 GB disk to the VM on the last of its scsi controllers.EXAMPLEPS&gt; Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -addController:$true -controllerType paravirtualThis will attach a 10 GB disk to the VM and attach it to a new scsi controller of type paravirtual.EXAMPLEPS&gt; Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -controllerNumber 2This will attach a 10 GB disk to the VM and attach it to the second controller on the VM#&gt;################################################################################### Script: Add-DiskToVm.ps1# Datum: 04.10.2017# Author: Bechtle Steffen Schweiz AG (c) 2017# Version: 1.0# History: Check VMs current set of SCSI Controllers when adding Disks##################################################################################[CmdletBinding(SupportsShouldProcess=$true)]Param([parameter()][string]$vCenter = "virtualfrogvc.virtual.frog",# Change to default VM for testing[string]$vMName = "reg-belairi82",# Change default Disk Size[decimal]$diskGB = 2.5,# Hardcode the SCSI Controller # (like 3 for the third controller)[int]$controllerNumber = 2000,# Add new SCSI Controller while you're at it[boolean]$addController = $false,# Type of SCSI Controller to add (paravirtual|VirtualLsiLogicSAS)[string]$controllerType = "paravirtual")function get-scsiCount ($vm){try {return ($vm | get-scsicontroller -ErrorAction Stop).count}catch {Write-Host "Could not count Scsi Controller of $vm"exit}}function get-scsiID ($vm, $number){try {return ($vm |get-scsicontroller |select -skip ($number-1) -first 1).ID}catch {Write-Host "Could not get scsi controller number $number from $vm"exit}}function get-scsiType ($vm, $id){try {return ($vm |get-scsicontroller -ID $id -ErrorAction Stop).Type}catch {Write-Host "Could not determine type of SCSI Controller on vm ($vm)"exit}}function add-DiskToVmOnController ($vm, $controller){try {New-Harddisk -Controller $controller -CapacityGB $diskGB -VM $vm -Whatif -ErrorAction Stop} catch {Write-Host "Could not add disk to VM ($vm)"exit}}function shutDownVm ($vm){try {Stop-VMGuest -VM $vm -confirm:$false -ErrorAction StopWrite-Host "Successfully send the shutdown command over VMware Tools"}catch {Write-Host -Foregroundcolor:red "The VM did not respond to a VMware tools shutdown command"$switch = Read-Host -Prompt "Would you like to Power off the VM $vm ? (yes/no)"if ($switch -match "yes") {Stop-VM -VM $vm -confirm:$false} else {Write-Host "You chose not to power off the VM. Stopping the script.."exit}}while ((get-vm $vMName).PowerState -notmatch "PoweredOff"){Write-Host "Waiting for $vm to shut down..."sleep -s 5}$vmHasShutDown = $true}####### Main Program ######try {Import-Module -Name VMware.VimAutomation.Core -ErrorAction Stop | Out-Null} catch {Write-Host "Could not add VMware PowerCLI Modules"exit}try {Connect-VIServer $vCenter -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null}catch {Write-Host "Could not connect to vCenter $vCenter"exit}Write-Host "Connected to $vCenter. Starting script"try {$vm = Get-VM $vMName -ErrorAction Stop} catch {Write-Host "Could not find VM with Name $vMName in vCenter $vCenter"exit}if ($addController) {if ($vm.PowerState -match "PoweredOn") {Write-Host -Foregroundcolor:red "The VM is still powered On."$switch = Read-Host -Prompt "Would you like to shut down the VM ($vm)? (yes/no)"if ($switch -match "yes") {shutDownVm $vm} else {Write-Host "You chose not to shutdown the VM ($vm). Stopping the script now"exit}}try {$vm |New-Harddisk -CapacityGB $diskGB |new-scsicontroller -type $controllerType -ErrorAction Stopif ($vmHasShutDown) {$switch = Read-Host -Prompt "The VM was shut down for this operation. Power it back on? (yes/no)"if ($switch -match "yes") {Start-VM $vm -confirm:$false}}} catch {Write-Host "could not add scsi controller with new disk to $vm"exit}} elseif ($controllerNumber -ne 2000) {$numberOfControllers = get-scsiCount $vmif ($numberOfControllers -gt $controllerNumber) {Write-Host "You specified controller number $controllerNumber but the VM ($vm) only has $numberOfControllers controllers"exit} else {$scsiID = get-scsiID $vm $controllerNumberWrite-Host "The VM ($vm) has $numberOfControllers SCSI Controller(s) attached. You chose to attach a new disk to the $controllerNumber. adapter"Write-Host "The VM ($vm) has a "(get-scsiType $vm $scsiID)" Controller for the number you provided"add-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID)Write-Host "Added a disk of $diskGB GB to $vm on controller "($vm | get-scsicontroller -ID $scsiID).Name}}else {$numberOfControllers = get-scsiCount $vm$scsiID = get-scsiID $vm $numberOfControllersWrite-Host "The VM ($vm) has $numberOfControllers SCSI Controller(s) attached"Write-Host "The VM ($vm) has a "(get-scsiType $vm $scsiID)" Controller as the last one"add-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID)Write-Host "Added a disk of $diskGB GB to $vm on controller "($vm | get-scsicontroller -ID $scsiID).Name}``` </body> </section> </sections> <status> <corpus>267</corpus> <alsoLike> <item> <ref>posts/vmware-explore-las-vegas-hackathon-2025</ref> <score>1.00</score> </item> <item> <ref>posts/vmware-hackathon-2024-project</ref> <score>1.00</score> </item> <item> <ref>solutions/vmware/vmware-cloud-foundation/addon/application-services</ref> <score>0.60</score> </item> </alsoLike> </status></manifest>
Scripting · 2017-10-04

Adding a disk to a VM

Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM.

2017-10-04Date
Dario DörflingerAuthor
4Min read
Topics Automation 1.78
Vendors VMware 0.88

This post is from 2017. It stays online because people still look for it, but it describes the products as they were then.

Recently I stumbled upon a stubborn vSphere Client at a customer. It just would not let me add a disk a VM. The error message was very generic (and was in the end fixed with a reboot) so I had to find a alternative: Here comes PowerCLI.

The requirements were as follows:

  • size of the disk in GB must be specified
  • it should be possible to add a new scsi controller if one chooses to do so
  • it should be possible to specify which already present scsi controller should be used
  • if no option is specified always add the disk to the last scsi controller

It was easy enough to get the script working. One thing I learned was that “new-scsicontroller” requires the VM to be in the “PoweredOff” State.

Update: this script is now published at GitHub.

Here is the script:

<#
.EXAMPLE
PS> Add-DiskToVm.ps1 -vMName reg-belairi81 -vCenter virtualfrogvc.virtual.frog -diskGB 10

This will attach a 10 GB disk to the VM on the last of its scsi controllers
.EXAMPLE
PS> Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -addController:$true -controllerType paravirtual

This will attach a 10 GB disk to the VM and attach it to a new scsi controller of type paravirtual
.EXAMPLE
PS> Add-DiskToVm.ps1 -vCenter virtualfrogvc.virtual.frog -vMName reg-belairi81 -diskGB 10 -controllerNumber 2

This will attach a 10 GB disk to the VM and attach it to the second controller on the VM
#>

##################################################################################
# Script: Add-DiskToVm.ps1
# Datum: 04.10.2017
# Author: Bechtle Steffen Schweiz AG (c) 2017
# Version: 1.0
# History: Check VMs current set of SCSI Controllers when adding Disks
##################################################################################

[CmdletBinding(SupportsShouldProcess=$true)]
Param(
[parameter()]
[string]$vCenter = "virtualfrogvc.virtual.frog",
# Change to default VM for testing
[string]$vMName = "reg-belairi82",
# Change default Disk Size
[decimal]$diskGB = 2.5,
# Hardcode the SCSI Controller # (like 3 for the third controller)
[int]$controllerNumber = 2000,
# Add new SCSI Controller while you're at it
[boolean]$addController = $false,
# Type of SCSI Controller to add (paravirtual|VirtualLsiLogicSAS)
[string]$controllerType = "paravirtual"

)
function get-scsiCount ($vm)
{
try {
return ($vm | get-scsicontroller -ErrorAction Stop).count
}
catch {
Write-Host "Could not count Scsi Controller of $vm"
exit
}
}

function get-scsiID ($vm, $number)
{
try {
return ($vm |get-scsicontroller |select -skip ($number-1) -first 1).ID
}
catch {
Write-Host "Could not get scsi controller number $number from $vm"
exit
}
}

function get-scsiType ($vm, $id)
{
try {
return ($vm |get-scsicontroller -ID $id -ErrorAction Stop).Type
}
catch {
Write-Host "Could not determine type of SCSI Controller on vm ($vm)"
exit
}
}

function add-DiskToVmOnController ($vm, $controller)
{
try {
New-Harddisk -Controller $controller -CapacityGB $diskGB -VM $vm -Whatif -ErrorAction Stop
} catch {
Write-Host "Could not add disk to VM ($vm)"
exit
}
}

function shutDownVm ($vm)
{
try {
Stop-VMGuest -VM $vm -confirm:$false -ErrorAction Stop
Write-Host "Successfully send the shutdown command over VMware Tools"
}
catch {
Write-Host -Foregroundcolor:red "The VM did not respond to a VMware tools shutdown command"
$switch = Read-Host -Prompt "Would you like to Power off the VM $vm ? (yes/no)"
if ($switch -match "yes") {
Stop-VM -VM $vm -confirm:$false
} else {
Write-Host "You chose not to power off the VM. Stopping the script.."
exit
}
}

while ((get-vm $vMName).PowerState -notmatch "PoweredOff")
{
Write-Host "Waiting for $vm to shut down..."
sleep -s 5
}
$vmHasShutDown = $true
}

####### Main Program ######
try {
Import-Module -Name VMware.VimAutomation.Core -ErrorAction Stop | Out-Null
} catch {
Write-Host "Could not add VMware PowerCLI Modules"
exit
}

try {
Connect-VIServer $vCenter -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null
}
catch {
Write-Host "Could not connect to vCenter $vCenter"
exit
}

Write-Host "Connected to $vCenter. Starting script"

try {
$vm = Get-VM $vMName -ErrorAction Stop
} catch {
Write-Host "Could not find VM with Name $vMName in vCenter $vCenter"
exit
}
if ($addController) {
if ($vm.PowerState -match "PoweredOn") {
Write-Host -Foregroundcolor:red "The VM is still powered On."
$switch = Read-Host -Prompt "Would you like to shut down the VM ($vm)? (yes/no)"
if ($switch -match "yes") {
shutDownVm $vm

} else {
Write-Host "You chose not to shutdown the VM ($vm). Stopping the script now"
exit
}
}
try {
$vm |New-Harddisk -CapacityGB $diskGB |new-scsicontroller -type $controllerType -ErrorAction Stop
if ($vmHasShutDown) {
$switch = Read-Host -Prompt "The VM was shut down for this operation. Power it back on? (yes/no)"
if ($switch -match "yes") {
Start-VM $vm -confirm:$false
}
}
} catch {
Write-Host "could not add scsi controller with new disk to $vm"
exit
}
} elseif ($controllerNumber -ne 2000) {
$numberOfControllers = get-scsiCount $vm
if ($numberOfControllers -gt $controllerNumber) {
Write-Host "You specified controller number $controllerNumber but the VM ($vm) only has $numberOfControllers controllers"
exit
} else {
$scsiID = get-scsiID $vm $controllerNumber
Write-Host "The VM ($vm) has $numberOfControllers SCSI Controller(s) attached. You chose to attach a new disk to the $controllerNumber. adapter"

Write-Host "The VM ($vm) has a "(get-scsiType $vm $scsiID)" Controller for the number you provided"
add-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID)
Write-Host "Added a disk of $diskGB GB to $vm on controller "($vm | get-scsicontroller -ID $scsiID).Name
}
}
else {
$numberOfControllers = get-scsiCount $vm
$scsiID = get-scsiID $vm $numberOfControllers
Write-Host "The VM ($vm) has $numberOfControllers SCSI Controller(s) attached"

Write-Host "The VM ($vm) has a "(get-scsiType $vm $scsiID)" Controller as the last one"
add-DiskToVmOnController $vm ($vm | get-scsicontroller -ID $scsiID)
Write-Host "Added a disk of $diskGB GB to $vm on controller "($vm | get-scsicontroller -ID $scsiID).Name
}

You might also like