使用 PowerShell 和 SMO 恢复数据库时显示进度
我有一个用于使用 PowerShell 和 SMO 恢复数据库的脚本。现在我知道我可以将事件处理程序传递到恢复对象上的 PercentComplete 中,并在恢复发生时获取恢复进度。问题是我不知道如何创建事件处理程序并向其传递 PowerShell 中的函数?我可以用 C# 做到这
restore.PercentComplete += new PercentCompleteEventHandler(restore_PercentComplete);
static void restore_PercentComplete(object sender, PercentCompleteEventArgs e)
{
System.Console.WriteLine("{0}", e.Percent);
}
一点任何帮助将不胜感激。
谢谢。
I have a script for restoring a database with PowerShell and SMO. Now I know that I can pass a event handler into PercentComplete on the restore object and get the progress of the restore as it happens. The problem is I don't know how to create a event handler and pass it a function in PowerShell? I can do it in C#
restore.PercentComplete += new PercentCompleteEventHandler(restore_PercentComplete);
static void restore_PercentComplete(object sender, PercentCompleteEventArgs e)
{
System.Console.WriteLine("{0}", e.Percent);
}
Any help would be appreciated.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过更深入的搜索,我终于在文档中找到了它。要添加事件处理程序,您需要执行以下操作:
导入相关程序集;
现在要创建事件处理程序,您需要使用内联函数声明它;
现在最后一步是将事件处理程序添加到您正在使用的对象中。通常在 C# 中你只需执行以下操作;
这在 PowerShell 脚本中不起作用,您需要做的是使用生成的函数来添加事件。函数名称是 EventHandlerName,其开头附加有“add_”,如下所示;
希望这可以帮助其他尝试这样做的人!
After some deeper searching I finally found it in the documentation. To add event handlers you need to do the following:
Import the relevant assemblies;
Now to create the event handler you need to declare it with an inline function;
Now the final step is to add the event handler to the object you are working with. Normally in C# you just do the following;
This will not work in PowerShell script, what you need to do is use the generated function for adding events. The function name is the EventHandlerName with "add_" appended to the beginning of it, like so;
Hope this helps anyone else trying to do this!
您可以使用 v1 powershell 中的 pseventing 以异步方式进行操作 - http://pseventing.codeplex.com。随时查看进度,而不是等待。 v2 powershell 有自己的事件。
我在 pseventing 中包含了一个脚本,可以完全按照您所说的操作,除了以后台方式。
-奥辛
You can do it async style with pseventing in v1 powershell - http://pseventing.codeplex.com. check on progress whenever you like instead of waiting. v2 powershell has its own eventing.
i include a script in pseventing to do exactly what you say, except in a background fashion.
-Oisin