Microsoft Virtualization Discussions

echo Y |

CharlesYeung
6,928 Views

Hi,

       Anyone knows how to convert "echo Y |"  dos command in PowerShell?

Thanks

Charles

6 REPLIES 6

sizemore
6,928 Views

Without knowing what your trying to do, have you tried {'y' | your command.exe}.  Without detailes it's hard to telll why the pipline isn't working. If you tell us what your trying to do we can explore other options.

~Glenn

CharlesYeung
6,928 Views

Hi  Glenn,

                         I am new to PowerShell..    What I am trying to do is piping  Echo Y | to Remove-NaSnapshot..  

The goal is to delete orphaned snapshot(s) from all the iSCSI volumes that start with { 

Example:  {93101811-e57c-4627-939a-46580448f181} 

PS C:\Windows\system32> $VOLList = Get-NaVol

PS C:\Windows\system32> $myvar=”{*”

PS C:\Windows\system32> ForEach ($_.Name in $VOList) {Get-NaSnapshot $_.Name $myvar | Remove-NaSnapshot | write-host "Y"}

Delete snapshot

Are you sure you want to delete snapshot {93101811-e57c-4627-939a-46580448f181} from volume FFXCMGC01_SQL_DB?

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?]

PS C:\Windows\system32>>> ForEach ($_.Name in $VOLList) {Get-NaSnapshot $_.Name $myvar | Remove-NaSnapshot | echo Y}

Delete snapshot

Are you sure you want to delete snapshot {93101811-e57c-4627-939a-46580448f181} from volume FFXCMGC01_SQL_DB?

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Delete snapshot

Are you sure you want to delete snapshot {9b98763a-3e0a-4fd4-b40a-5129b787dd88} from volume FFXCMGC01_SQL_DB?

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Thanks

Charles

timothyn
6,928 Views

Hi Charles,

Great question!  Most PowerShell cmdlets (not just the toolkit) that prompt the user for confirmation have a "-Confirm" switch.  You can try something like this:

Get-NaVol | Get-NaSnapshot -SnapName "{*" | Remove-NaSnapshot -Confirm

Cheers,

Eric

beam
6,928 Views

Just to clarify, if you want to suppress the confirmation, you would use "-Confirm:$false":

Remove-NaSnapshot $volName $snapName -Confirm:$false

You can also look into the $ConfirmPreference variable, which will control the confirmation prompt for all cmdlets.  The help topic about_Preference_Variables contains information about the $ConfirmPreference variable:

help about_Preference_Variables

Hope that helps!

-Steven

sizemore
6,928 Views

Ohh Eric so close

Charles,

PowerShell uses a series of common parameters to universally controll the behavior of all cmdlet.  Any cmdlet that prompts for confirmation is doing so by upgrading the -confirm switch.  You can inturn either, make any command prompt by adding the confirm switch as Eric did.  Or you can override it by supplying a negetive confirm value.

Get-NaVol | Get-NaSnapshot -SnapName "{*"| Remove-NaSnapShot -confirm:$false

for more information on how this all works just type "help about_commonparameters" from any powershell prompt.

Hope that helps,

~Glenn

CharlesYeung
6,928 Views

Thank You All  (Glenn,  Eric, and Beam)..   

It is so amaze how powershell can do .   It turns my 45+ lines dos command into  just 3 statements

Charles

Public