PowerShell的 - 响应命令响应 [英] PowerShell - Respond to a Command response

查看:270
本文介绍了PowerShell的 - 响应命令响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个简单的脚本,将执行在Windows控制台命令,它会一直询问覆盖现有数据的确认。我觉得下面有点像会的工作,但现在看来我错了(没有惊喜,写主机不使用命令行交互,我相信这只是把文字在屏幕上)。任何人都可以提供关于如何处理这一一些建议吗?我需要能够执行没有任何交互每周通过任务调度的脚本。

脚本:

 开始处理-NoNewWindow -FilePath PW -ArgumentList @
    门槛检查站打造WeeklyBackupWeeklyBackup
@
睡眠-Seconds 3
$确认=选择字符串-pattern关于过度写现有的CheckPoint'WeeklyBackup'。 -安静如果($确认)
{
   写主机Y`r
}

我希望在控制台中看到:

  D:\\ BMC_Software \\ ProactiveNet> PW门槛检查站打造WeeklyBackup周
lyBackup
即将覆写现存的CheckPoint'WeeklyBackup。你想继续吗?
(Y / N)

然后,用户将达到Y和回车和过程将完成。但我想这个自动的。

在此先感谢您的任何建议。


解决方案

 回声Y`r| PW

这通常是从批处理文件中使用,但通常效果一样好,从PowerShell中。

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.

解决方案

echo "Y`r" | pw

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

这篇关于PowerShell的 - 响应命令响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆