PowerShell script exchange
Post Reply
lmky1234
Novice
Posts: 5
Liked: never
Joined: Jul 09, 2021 12:39 am
Full Name: YONGKWAN KIM
Contact:

How do I automatically answer “yes” to a prompt in Powershell?

Post by lmky1234 »

Hello, I'm Veeam Backup and powershell scripts Beginner.

How can I input "yes" as an answer to an interactive question in a PowerShell session?

I run the recovery image creation script.

if there is an 192.168.100.xx existing iso file, do you want to replace it? message appears.

I want to automatically say Yes(y) here, is there any way?

And is it possible to automatically enter the ISO file name as the HostName or backup policy name?

This is my script.


# Get computer names from text file
$names = Get-Content -Path 'C:\Temp\computers.txt'

# Get the most current list of physical computers managed by VBR
$computers = Get-VBRDiscoveredComputer

# Determine the centralised path for ISO
$path = 'C:\Temp\'

foreach ($name in $names) {

# Find physical computers with the corresponding names
$computer = $computers | where {$_.Name -eq $name}
if ($computer) {
# If such computer is managed by VBR, construct custom path for ISO image and compose ISO target object
$ISOPath = "$($path)$($computer.Name).iso"
$ISOtarget = New-VBRRecoveryMediaISOTarget -Path $ISOPath

# Create recovery media for the computer in question
Add-VBRDiscoveredComputerRecoveryMedia -DiscoveredComputer $computer -RecoveryMediaTarget $ISOtarget

}
else {
Write-Host "Computer with name $name wasn't found"
}
}
Andreas Neufert
VP, Product Management
Posts: 7076
Liked: 1510 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by Andreas Neufert »

https://helpcenter.veeam.com/docs/backu ... ml?ver=110

Please check the -force option here.
lmky1234
Novice
Posts: 5
Liked: never
Joined: Jul 09, 2021 12:39 am
Full Name: YONGKWAN KIM
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by lmky1234 »

Then, can I just type -Force true after $ISOtarget?
Andreas Neufert
VP, Product Management
Posts: 7076
Liked: 1510 times
Joined: May 04, 2011 8:36 am
Full Name: Andreas Neufert
Location: Germany
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by Andreas Neufert »

-force
is enough
Natalia Lupacheva
Veteran
Posts: 1143
Liked: 302 times
Joined: Apr 27, 2020 12:46 pm
Full Name: Natalia Lupacheva
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by Natalia Lupacheva »

Hi Yongkwan,

Just to clarify - if I understand your doubts correctly - yes, you are right, you can put "-Force" option right after $ISOtarget parameter, after this:
Add-VBRDiscoveredComputerRecoveryMedia -DiscoveredComputer $computer -RecoveryMediaTarget $ISOtarget

Thanks!
lmky1234
Novice
Posts: 5
Liked: never
Joined: Jul 09, 2021 12:39 am
Full Name: YONGKWAN KIM
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by lmky1234 »

thx, but i type -force after $ISOtarget, The same message appear.
However, even if you input No(N) here, the ISO file is received.
I want to automatically get ISO recovery image without N or Y input.
nielsengelen
Product Manager
Posts: 5796
Liked: 1215 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by nielsengelen »

I believe u should use -force:$true in this case.
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
lmky1234
Novice
Posts: 5
Liked: never
Joined: Jul 09, 2021 12:39 am
Full Name: YONGKWAN KIM
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by lmky1234 »

Thanks everyone for the replies. But when I use -force:$true it keeps saying "do you want to replace it?" this comes out
nielsengelen
Product Manager
Posts: 5796
Liked: 1215 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by nielsengelen »

How about echoing "y" before u run the command?

Code: Select all

echo y | $ISOtarget = New-VBRRecoveryMediaISOTarget -Path $ISOPath
or add the -Confirm option:

Code: Select all

$ISOtarget = New-VBRRecoveryMediaISOTarget -Path $ISOPath -Confirm:$false
I can't test it right now so I'm relying on typical solutions to say "y" in PowerShell :-)
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
soncscy
Veteran
Posts: 643
Liked: 312 times
Joined: Aug 04, 2019 2:57 pm
Full Name: Harvey
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by soncscy »

Hello Yongkwan Kim,

I took a peek with a decompiler at the method used for this cmdlet, and unless a Veeam person wants to say otherwise, the dependent method accepts no parameters besides a target path for the proposed Recovery ISO File. CheckTargetIsoFileExistanceSafe is the method backing this part of the cmdlet and it has a simple workflow:

1. Check if the target file already exists
2. Ask the user to overwrite.
3. If No to 2, then break and fail (the method returns $false)
4. If Yes to 2, then continue on as it's safe to overwrite.


So as I see it, you cannot handle this with the cmdlet itself.

I would propose you write a logic check before the RecoveryMedia creation and handle the file checking before processing the cmdlet, something like:

Code: Select all

If(Test-Path $TargetISOFile){
do Action-on-ISO file
}
You can delete it, rename the file, append something to $TargetISOFile (I actually like this personally, but that's just me)

But seems you will need to do this processing yourself.
lmky1234
Novice
Posts: 5
Liked: never
Joined: Jul 09, 2021 12:39 am
Full Name: YONGKWAN KIM
Contact:

Re: How do I automatically answer “yes” to a prompt in Powershell?

Post by lmky1234 »

:D Thank you all very much!! :D
Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests