Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - Respond to a Command response

I am trying to write a simple script that will execute a console command in Windows, which will always ask for a confirmation for overwriting the existing data. I thought something like the below would work, but it appears I was mistaken (and no surprise, write-host doesn't interact with the command line, I believe it simply puts text on screen). Can anyone provide some advice on how to handle this? I need to be able to execute the script through task scheduler on a weekly basis with no interaction.

Script:

Start-Process -NoNewWindow -FilePath pw -ArgumentList @"
    threshold checkpoint create "WeeklyBackup" "WeeklyBackup"
"@
sleep -Seconds 3
$confirm = Select-String -pattern "About to over-write existing CheckPoint 'WeeklyBackup'." -Quiet

if ($confirm)
{
   Write-Host "Y`r" 
}

What I expect to see in the console:

D:\BMC_Software\ProactiveNet>pw threshold checkpoint create "WeeklyBackup" "Week
lyBackup"
About to over-write existing CheckPoint 'WeeklyBackup'. Do you want to proceed?
(y/n)

Then a user would hit Y and carriage-return and the process would be done. But I want this automated.

Thanks in advance for any advice.

like image 494
billhubb84 Avatar asked Aug 06 '14 18:08

billhubb84


Video Answer


1 Answers

echo "Y`r" | pw

This is typically used from batch files but it usually works just as well from PowerShell.

like image 160
briantist Avatar answered Sep 30 '22 07:09

briantist