VMware Cloud Community
wprather
Contributor
Contributor
Jump to solution

-Confirm $false breaks Set-HardDisk

I'm working on a script to automatically extend the hard disk in vsphere and then go into the guest OS and extend it there as well.  I want this script to be completely automated and I don't want the confirmation message to show up.  My issue comes on this line.

Get-Harddisk -vm $hostname|where {$_.name -eq "Hard disk 1"}|Set-Harddisk -CapacityKB $value -Confirm $false

The error thrown is

Set-HardDisk : Cannot bind parameter 'HardDisk'. Cannot convert value "False" t
o type "VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.HardDisk". Error: "I
nvalid cast from 'System.Boolean' to 'VMware.VimAutomation.ViCore.Types.V1.Virt
ualDevice.HardDisk'."
At C:\users\prat294\Documents\test2.ps1:34 char:80
+     $temp = get-harddisk -vm $compname|where {$_.name  -eq $harddisk}|Set-Har
dDisk <<<<  -CapacityKB $newValue -Confirm $false
    + CategoryInfo          : InvalidArgument: (:) [Set-HardDisk], ParameterBi
   ndingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
   ion.ViCore.Cmdlets.Commands.VirtualDevice.SetHardDisk

It is interpreting the $false as a harddisk reference.  I have tried this:

$temp = Get-Harddisk -vm $hostname|where {$_.name -eq "Hard disk 1"}

Set-Harddisk -HardDisk $temp -CapacityKB $value -Confirm $false

The error i get here is different.

Set-HardDisk : Cannot bind parameter 'Persistence' to the target. Exception set
ting "Persistence": "Invalid value passed for the Persistence parameter. The va
lid values are: Persistent, NonPersistent, Undoable, IndependentPersistent, Ind
ependentNonPersistent, Unknown."
At C:\users\prat294\Documents\test2.ps1:35 char:14
+     Set-HardDisk <<<<  -HardDisk $temp -CapacityKB $newValue -Confirm $false
    + CategoryInfo          : WriteError: (:) [Set-HardDisk], ParameterBinding
   Exception
    + FullyQualifiedErrorId : ParameterBindingFailed,VMware.VimAutomation.ViCo
   re.Cmdlets.Commands.VirtualDevice.SetHardDisk

I have then added a persistence flag, but then it asks for a datastore flag.  I added that and then it says there is no positional parameter for False.

I am beginning the think that the -confirm flag is broken, but I'd like to get some additional input.  Thanks for your help.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I think you should use

Get-Harddisk -vm $hostname|where {$_.name -eq "Hard disk 1"}|Set-Harddisk -CapacityKB $value -Confirm:$false

That's how you pass a value to a switch parameter.

If you have a blank in between, the cmdlet will interprete this as a new parameter value ($false in this case), but it doesn't know to which parameter the value should go.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

I think you should use

Get-Harddisk -vm $hostname|where {$_.name -eq "Hard disk 1"}|Set-Harddisk -CapacityKB $value -Confirm:$false

That's how you pass a value to a switch parameter.

If you have a blank in between, the cmdlet will interprete this as a new parameter value ($false in this case), but it doesn't know to which parameter the value should go.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, wprather-

I think that this is just a syntax issue.  To use the -Confirm parameter in cmdlets, you set the value by using a colon (:) and then the boolean, like "-Confirm:$false".  So, your line would be:

Get-Harddisk -vm $hostname|where {$_.name -eq "Hard disk 1"}|Set-Harddisk -CapacityKB $value -Confirm:$false

Does that do better for you?

Message edit by mattboren at 14:20:  doh -- I see now that LucD already answered

wprather
Contributor
Contributor
Jump to solution

Thank you for the quick response.  That worked tons better.  It's amazing what doing things right will do for you.

0 Kudos