VMware Cloud Community
antize
Enthusiast
Enthusiast
Jump to solution

Question for script ejecting CD drive media

Just wrote this to eject media from all my VMs:

get-vm | % {$_.Name; $_.CDDrives | % {Set-CDDrive -cd $_ -NoMedia}}

Since we use Lab Manager there are alot of VMs and the command above prompts me if I want to proceed on every VM which doesn't when i'm trying to automate this. According the Set-CDDrive documentation there is a -Confirm parameter (see below). According to it's description the default behavior should be to just do the action unless I provide the -Confirm param. This is however not the behavior i'm seeing. Any insight what I could do to make it run without prompting for every VM?

Name

Description

Required?

Pipeline Input

Default Value

CD

Specify the virtual CD drive you want to configure.

true

true (ByValue)

IsoPath

Specify the datastore path to ISO (CD image) file that backs the virtual CD

drive. Do not use this parameter at the same time with the HostDevice and

NoMedia parameters.

false

false

HostDevice

Specify the path to the CD drive on the host which backs this virtual CD

drive. Do not set this parameter at the same time with the ISOPath and NoMedia

parameters.

false

false

NoMedia

Indicate that the CD drive has no media. Do not use this parameter at the

same time with the ISOPath or HostDevice parameters.

false

false

StartConnected

Indicate that the virtual CD drive starts connected when the virtual machine

associated with it powers on.

false

false

Connected

Indicate that the virtual CD drive is connected after its creation. This

parameter can be specified only if the corresponding virtual machine is powered

on.

false

false

WhatIf

Indicate that the cmdlet is run only to display the changes that would be

made and actually no objects are modified.

false

false

Confirm

Indicate that the cmdlet asks for confirmation before running.

false

false

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Just append "-Confirm:$false" to Set-CDDrive cmdlet - this will perform the action without prompting for confirmation:

Set-CDDrive -cd $_ -NoMedia -Confirm:$false

View solution in original post

0 Kudos
7 Replies
admin
Immortal
Immortal
Jump to solution

Just append "-Confirm:$false" to Set-CDDrive cmdlet - this will perform the action without prompting for confirmation:

Set-CDDrive -cd $_ -NoMedia -Confirm:$false

0 Kudos
antize
Enthusiast
Enthusiast
Jump to solution

Thanks that worked, but it is very unintuitive since the cmdlet documentation says that -Confirm is not required. Usually a flag should signify a change in default behavior but in this case specifying the flag does not change the default behavior unless the flag's value is explicitly set with the less known powershell syntax of:

 -Flag:[$true|$false] 

.

In this case it would make sense that the default behavior is to not confirm which could be changed be specifying the -Confrm flag. This is how the out-of-box MS cmdlets like Move-Item are. If it is really intended to keep the default behavior being confirm, it would make more sense to have a -DontConfirm flag to change the default behavior, but this would not follow the standard PowerShell symantics. My vote is to change the default behavior to just do it unless the -Confirm flag is specified.

0 Kudos
admin
Immortal
Immortal
Jump to solution

The -Confirm parameter is not provided by PowerCLI, this is a native PowerShell parameter. You can get more info on PowerShell confirmation here: http://blogs.msdn.com/powershell/archive/2006/12/15/confirmpreference.aspx

0 Kudos
antize
Enthusiast
Enthusiast
Jump to solution

Right, -Confirm is one of those ubiquitous commands like -ErrorAction and -Debug that are built into to all cmdlets and it is the developers job to implement them to be in line with PowerShell standard practices.

According to Jeffrey:

With Windows PowerShell you can specify -CONFIRM on commands that have a side-effect on the system and it will ask you whether to perfmon the action or not.

So any cmdlet that has a system side effect (in this case Set-CDDrive is removing media) should implement the -Confirm parameter to ask the user if they want to go ahead with the operation. So again my vote is for the PowerCLI team to change the default behavior to just go ahead and execute unless the -Confirm parameter is used and then prompt the user if it is. This would be more in line with the default PowerShell cmdlets like Move-Item and is more intuitive since users can expect similar behavior from all cmdlets.

0 Kudos
admin
Immortal
Immortal
Jump to solution

In the article I mentioned, there is a paragraph about the confirm preference of the cmdlets and of the environment. In this case Set-CDDrive cmdlet has "High" confirm preference and by default the environment's confirm preference is set to "Medium" (e.g. $ConfirmPreference environment variable). That's why PowerShell asks for confirmation even though you did not specify -Confirm parameter (again - this is by design in PowerShell, not PowerCLI).

If you don't want the confirm impact of the cmdlets to affect whether they ask for confirmation or not - you can set $ConfirmPreference to "None". In that case none of the cmdlets will ask for confirmation unless you explicitly specify -Confirm parameter.

Hope this makes things more clear.

antize
Enthusiast
Enthusiast
Jump to solution

Ah that was more helpful. The $ConfirmPreference built in is news to me, it's not even documented in 'get-help about_automatic_variables'. So it sounds like the intent is to prompt by default which is by design as you say which is fine. What would be more helpful then is better documentation. A more explicit explaination should be documented for the -Confirm parameter provided for the Set-CDDrive cmdlet. It should specifically say that in order to prevent confirmation the aformentioned syntax should be used. This would have saved me the time of having to post in the forums.

Thanks for the info, and the quick responses Smiley Happy

0 Kudos
admin
Immortal
Immortal
Jump to solution

You're right, we should probably better document this. Thanks for the feedback.

0 Kudos