使用 PowerShell 和 SMO 恢复数据库时显示进度

发布于 2024-09-13 22:10:50 字数 423 浏览 1 评论 0原文

我有一个用于使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮子 2024-09-20 22:10:50

经过更深入的搜索,我终于在文档中找到了它。要添加事件处理程序,您需要执行以下操作:

导入相关程序集;

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null 
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SmoExtended') | out-null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.ConnectionInfo') | out-null

现在要创建事件处理程序,您需要使用内联函数声明它;

$percentEventHandler = [Microsoft.SqlServer.Management.Smo.PercentCompleteEventHandler] { Write-Host "Restored " $_.Percent "%" }
$completedEventHandler = [Microsoft.SqlServer.Management.Common.ServerMessageEventHandler] { Write-Host "Database " $databasename " Created Successfuly!" }

现在最后一步是将事件处理程序添加到您正在使用的对象中。通常在 C# 中你只需执行以下操作;

restore.PercentComplete += new PercentCompleteEventHandler(restore_PercentComplete);
restore.Complete += new Microsoft.SqlServer.Management.Common.ServerMessageEventHandler(restore_Complete);

这在 PowerShell 脚本中不起作用,您需要做的是使用生成的函数来添加事件。函数名称是 EventHandlerName,其开头附加有“add_”,如下所示;

$dbRestore.add_PercentComplete($percentEventHandler)
$dbRestore.add_Complete($completedEventHandler)

希望这可以帮助其他尝试这样做的人!

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;

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null 
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SmoExtended') | out-null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.ConnectionInfo') | out-null

Now to create the event handler you need to declare it with an inline function;

$percentEventHandler = [Microsoft.SqlServer.Management.Smo.PercentCompleteEventHandler] { Write-Host "Restored " $_.Percent "%" }
$completedEventHandler = [Microsoft.SqlServer.Management.Common.ServerMessageEventHandler] { Write-Host "Database " $databasename " Created Successfuly!" }

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;

restore.PercentComplete += new PercentCompleteEventHandler(restore_PercentComplete);
restore.Complete += new Microsoft.SqlServer.Management.Common.ServerMessageEventHandler(restore_Complete);

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;

$dbRestore.add_PercentComplete($percentEventHandler)
$dbRestore.add_Complete($completedEventHandler)

Hope this helps anyone else trying to do this!

电影里的梦 2024-09-20 22:10:50

您可以使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文