开发者

Help with Powershell eventing

开发者 https://www.devze.com 2023-03-13 12:04 出处:网络
I have a function to restore sql server database using \"Microsoft.SqlServer.Management.Smo.Restore. There is an event called completed and within the code I put

I have a function to restore sql server database using "Microsoft.SqlServer.Management.Smo.Restore. There is an event called completed and within the code I put

 Register-ObjectEvent -InputObject $restore  -EventName "Complete" -SourceIdentifier CompleteRestore -Action { Write-Host "Works"} | Out-Null

It works perfectly and the message appears when the restore finishes.

When I run the function by Powershell Job, The restore is done,but the message does not appear.

Can You Help me guys ?

Thanks

I am using this code to run the function as a job and the Register-Object is inside the Invoke-SqlRestoreEventing Functi开发者_如何学Con

$server = "."
$dbname = "TestPoshEventing_6"
$filepath = "c:\temp\backup\TestPoshEventing.bak"
$Realocatefiles = @{TestPoshEventing='c:\temp\restore\TestPoshEventing_6.mdf';TestPoshEventing_log='c:\temp\restore\TestPoshEventing_6.ldf'}
Start-Job -Name "Restore1" -InitializationScript  {Import-Module c:\temp\testes\PoshTest.psm1 -Force} -scriptblock { Invoke-SqlRestoreEventing -sqlserver $args[0] -dbname $args[1]  -filepath $args[2] -relocatefiles $args[3] -force } -ArgumentList $server, $Dbname ,$filepath ,$Realocatefiles


Background jobs run in a different runspace. So, anything you output there won't appear at the console. You can modify your code to do that. Check this for example,

$server = "."
$dbname = "TestPoshEventing_6"
$filepath = "c:\temp\backup\TestPoshEventing.bak"
$Realocatefiles = @{TestPoshEventing='c:\temp\restore\TestPoshEventing_6.mdf';TestPoshEventing_log='c:\temp\restore\TestPoshEventing_6.ldf'}
$job = Start-Job -Name "Restore1" -InitializationScript  {Import-Module c:\temp\testes\PoshTest.psm1 -Force} -scriptblock { Invoke-SqlRestoreEventing -sqlserver $args[0] -dbname $args[1]  -filepath $args[2] -relocatefiles $args[3] -force } -ArgumentList $server, $Dbname ,$filepath ,$Realocatefiles

Wait-Job $job | Receive-Job

By using Receieve-Job when the background job completes, you can see the output of your register-ObjectEvent.


The Out-Null cmdlet sends output to NULL, in effect, deleting it.


I'm pretty sure it is a scope issue. When you run the Register-ObjectEvent in the job, you are creating a new scope, a new runspace in fact. The scope terminates as soon as the command completes. Based on what you posted I think your job is kicking off the backup but the scope that registers the event terminates before it finishes so when you receive the job results you get nothing. To see the write host you need to keep the scope alive long enough to receive the event. Or do something other than Write-Host such as writing a log file. Here's an example from one of my demos:

Register-ObjectEvent -InputObject $watcher -Eventname "Created" -SourceIdentifier "FolderChange" ` -MessageData "A new file was created" -Action { "$(Get-Date) A new file was created: $($event.sourceEventArgs.fullpath)" | Out-File $env:temp\log.txt -append }

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号