Although I read carefully "PowerShell Studio: Creating Responsive Forms" and the two topics concerning this issue "Job Tracker help needed" and "Button - Start Job, how to pass arguments" I can not figure out how to pass my working code (click event) to your Button - Start Job click event.
I need to pass a bunch of (gloabl) variables + I need to build a new SMObackup object. I figured that I need to fill -ArgumentList and use Param ($Argument1) #Pass any arguments using the ArgumentList parameter. I am lost here. Any help would be greatly appreciated.
My working Code. But it is synchronous, which is not of any help when a db backup takes hours:
- $buttonBackupDB_Click= {
- $Global:FileName = $textbox1_FN.Text
- $Server = $global:MSSQLServer
- $Database = $Global:SelectedDBName
- $Path = $Global:Path
- $Folder = $Global:CC + '\'
- $FileName = $Global:FileName
- $BackupSetName = $Global:BackupSetName
- $Extension = '.bak'
- $FilePath = $Path + $Folder + $FileName + $Extension
- $labelPathToBackupFile.Text = $FilePath #Debug welchen Pfad habe ich zusammen gebaut
- #ab hier smoBackup Objekt
- $smoBackup = New-Object Microsoft.SqlServer.Management.Smo.Backup # SMO Backup Objekt
- $smoBackup.Action = "Database"
- $smoBackup.BackupSetDescription = "Full (copyonly)backup of $($Database)"
- $smoBackup.BackupSetName = $BackupSetName
- $smoBackup.Database = $Database
- $smoBackup.Copyonly = "True"
- $smoBackup.MediaDescription = "Disk"
- $smoBackup.CompressionOption = "1"
- $smoBackup.PercentCompleteNotification = "10"
- $smoBackup.Devices.AddDevice($FilePath, "File")
- Try
- {
- $smoBackup.SqlBackup($server)
- }
- Catch
- {
- $textbox_out.Text = $_.Exception.InnerException
- }
- <#$Global:FileName = $textbox1_FN.Text
- $Server = $global:MSSQLServer
- $Database = $Global:SelectedDBName
- $Path = $Global:Path
- $Folder = $Global:CC + '\'
- $FileName = $Global:FileName
- $Extension = '.bak'
- $FilePath = $Path + $Folder + $FileName + $Extension
- $labelPathToBackupFile.Text = $FilePath #Debug welchen Pfad habe ich zusammen gebaut#>
Not working:
- [size=85]$buttonStartJob_Click={
- $buttonStartJob.Enabled = $false
- #Create a New Job using the Job Tracker
- Add-JobTracker -Name 'BackUpDBJob' `
- -JobScript {
- #--------------------------------------------------
- #TODO: Set a script block
- #Important: Do not access form controls from this script block.
- #ab hier smoBackup Objekt
- $smoBackup = New-Object Microsoft.SqlServer.Management.Smo.Backup # SMO Backup Objekt
- $smoBackup.Action = "Database"
- $smoBackup.BackupSetDescription = "Full (copyonly)backup of $($Database)"
- $smoBackup.BackupSetName = $BackupSetName
- $smoBackup.Database = $Database
- $smoBackup.Copyonly = "True" #verändert die DatabaseBackupLSN und bringt die scheduled inkrementellen Backups nicht durcheinander
- $smoBackup.MediaDescription = "Disk"
- $smoBackup.CompressionOption = "1"
- $smoBackup.PercentCompleteNotification = "10"
- $smoBackup.Devices.AddDevice($FilePath, "File")
- $smoBackup.SqlBackup($server)
- Param ($Argument1) #Pass any arguments using the ArgumentList parameter
- #--------------------------------------------------
- }`
- $env:FileName = $textbox1_FN.Text
- $env:Server = $global:MSSQLServer
- $wnv:Database = $Global:SelectedDBName
- $Path = $Global:Path
- $Folder = $Global:CC + '\'
- $FileName = $Global:FileName
- $BackupSetName = $Global:BackupSetName
- $Extension = '.bak'
- $FilePath = $Path + $Folder + $FileName + $Extension
- $labelPathToBackupFile.Text = $FilePath #Debug welchen Pfad habe ich zusammen gebaut
- -Argumentlist #WHAT GOES HERE AND HOW?
- -CompletedScript {
- Param($Job)
- #$results = Receive-Job -Job $Job
- #Enable the Button
- $buttonStartJob.ImageIndex = -1
- $buttonStartJob.Enabled = $true
- }`
- -UpdateScript {
- Param($Job)
- #$results = Receive-Job -Job $Job -Keep
- #Animate the Button
- if($null -ne $buttonStartJob.ImageList)
- {
- if($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
- {
- $buttonStartJob.ImageIndex += 1
- }
- else
- {
- $buttonStartJob.ImageIndex = 0
- }
- }
- }`
- }
- [/size]