none
Hilfe benötigt: Msg-Box als Job ausführen RRS feed

  • Frage

  • Hallo zusammen,

    ich überwache mehrere Textdateien. Wenn ich nun eine bestimmte Änderung feststelle, soll eine Msg-Box erscheinen. Damit das Script aber im Hintergrund weiter arbeitet möchte ich diese Msg-Box gerne als Job ausführen! Soweit alles kein Problem.

    Ich möchte aber verhindern, das die Box mehrfach angezeigt wird! Hat hier einer eine Idee? Hier mal mein Code wie ich mir das dachte, leider wird die $Global:ReturnVariable nicht ersetzt!

    Ist mein ansatz so richtig oder gibt es sonst eine Möglichkeit?

    Vielen Dank schonmal

    Hier mal der Code:

    cls
    
    $Global:ReturnVariable = "Ok"
    
    $func = {
    	FUNCTION Show-MsgBox {
    				
    					#Region Parameter
    				 	[CmdletBinding()]
    				    param(
    						[Parameter(Position = 0, Mandatory = $true)] 
    						[ValidateNotNullorEmpty()] 										
    						[string]$Title,
    						
    				    	[Parameter(Position = 1, Mandatory = $true)] 
    						[ValidateNotNullorEmpty()] 
    						[string]$Prompt,
    						
    				    	[Parameter(Position = 2, Mandatory = $false)] 
    						[ValidateSet("Information", "Question", "Critical", "Exclamation")] 
    						[string]$Icon = "Information",
    						
    				    	[Parameter(Position = 3, Mandatory = $false)] 
    						[ValidateSet("OKOnly", "OKCancel", "AbortRetryIgnore", "YesNoCancel", "YesNo", "RetryCancel")] 
    						[string]$BoxType = "OkOnly",
    						
    				    	[Parameter(Position = 4, Mandatory = $false)] 
    						[ValidateSet(1,2,3)] 
    						[int]$DefaultButton = 1,
    						
    						[Parameter(Position = 5, Mandatory = $false)] 
    						[switch] $TopMost
    
    				    )
    					#Endregion Parameter
    					
    					BEGIN {[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic") | Out-Null}
    					
    					PROCESS {
    						SWITCH ($Icon) {
    					        "Question" 			{$vb_icon = [microsoft.visualbasic.msgboxstyle]::Question }
    					        "Critical" 			{$vb_icon = [microsoft.visualbasic.msgboxstyle]::Critical}
    					        "Exclamation" 		{$vb_icon = [microsoft.visualbasic.msgboxstyle]::Exclamation}
    					        "Information" 		{$vb_icon = [microsoft.visualbasic.msgboxstyle]::Information}
    						} #END SWITCH ($Icon)
    						
    						SWITCH ($BoxType) {
    					        "OKOnly" 			{$vb_box = [microsoft.visualbasic.msgboxstyle]::OKOnly}
    					        "OKCancel" 			{$vb_box = [microsoft.visualbasic.msgboxstyle]::OkCancel}
    					        "AbortRetryIgnore" 	{$vb_box = [microsoft.visualbasic.msgboxstyle]::AbortRetryIgnore}
    					        "YesNoCancel" 		{$vb_box = [microsoft.visualbasic.msgboxstyle]::YesNoCancel}
    					        "YesNo" 			{$vb_box = [microsoft.visualbasic.msgboxstyle]::YesNo}
    					        "RetryCancel" 		{$vb_box = [microsoft.visualbasic.msgboxstyle]::RetryCancel}
    						} #END SWITCH ($BoxType)
    						
    						SWITCH ($Defaultbutton) {
    					        1 					{$vb_defaultbutton = [microsoft.visualbasic.msgboxstyle]::DefaultButton1}
    					        2 					{$vb_defaultbutton = [microsoft.visualbasic.msgboxstyle]::DefaultButton2}
    					        3 					{$vb_defaultbutton = [microsoft.visualbasic.msgboxstyle]::DefaultButton3}
    						} #END SWITCH ($Defaultbutton)
    						
    						SWITCH ($TopMost) {
    					        "TopMost" 			{$vb_topmost = [microsoft.visualbasic.msgboxstyle]::MsgBoxSetForeground}
    						} #END SWITCH ($TopMost)
    						
    						$popuptype = $vb_icon -bor $vb_box -bor $vb_defaultbutton -bor $vb_topmost
    
    						$Global:ReturnVariable	= [Microsoft.VisualBasic.Interaction]::MsgBox($Prompt,$popuptype,$title)
    					}
    					
    					END {}
    					
    				} #END FUNCTION Show-MsgBox		
    }
    
    FUNCTION TimerStart {
    				
    				#REGION Parameters
    				[cmdletbinding()]            
    				param(
    				[parameter(Mandatory=$true)]$SourceId,
    				[parameter(Mandatory=$true)]$timername,
    				[ValidateSet("Elapsed","Tick")][string]$Event = "Elapsed",
    				[parameter(Mandatory=$true)][long]$time,
    				[parameter(Mandatory=$false)]$action)
    				#ENDREGION Parameters
    				
    				$timername = new-object timers.timer
    				$timername.Interval = ($time*1000)
    				
    				#Timer registrierung löschen
    				Register-ObjectEvent -InputObject $timername -EventName $Event –SourceIdentIFier $SourceId -Action $action | Out-Null
    
    				#Timer starten
    				$timername.start()
    			}
    			
    TimerStart -SourceId Show -Timername Timer_Show -Event Elapsed -Time 5 -Action {
    
    	IF ($Global:ReturnVariable -eq "Ok"){
    		
    		$Global:ReturnVariable = "nOk"			
    		
    		$Job = Start-Job -InitializationScript $func -scriptblock {
    					
    			#Popupfenster anzeigen
    			Show-MsgBox `
    				-Title "Titel" `
    				-Prompt "Text" `
    				-Icon "Information" `
    				-BoxType "OkOnly" `
    				-DefaultButton "1" `
    				-TopMost 
    		}		
    	}
    }

    Samstag, 27. September 2014 15:19

Antworten

Alle Antworten

  • Samstag, 27. September 2014 16:20
  • Hallo Peter,

    vielen Dank für die Antwort! Ich hab mich auch gleich mal daran versucht, dieses mit Mutex umzusetzen!

    Da ich aber noch Programmieranfänger bin, ist das alles nicht so einfach für mich...

    Es soll alle 5 Sekunden ein Popupfenster erscheinen! Wenn es schon aufgepoppt ist soll nichts weiter geschehen!

    Ich habe es nun soweit hinbekommen, dass das Popupfenster nur einmal angezeigt wird! Wenn ich dann allerdings nach einer gewissen Zeit auf OK klicke, kommt sofort das nächste Popup!

    Die Meldungen werden also irgenwie und irgendwo zwischengespeichert... Ich möchte aber, wenn ich auf ok klicke, nicht sofort wieder die nächste Meldung, sondern es soll eine Zeitspanne ablaufen...

    Ich wäre euch echt dankbar, wenn mir einer helfen könnte...

    Vielen Dank!

    FUNCTION Global:Start-Timer {
    				
    	#REGION Parameters
    	[cmdletbinding()]            
    	param (
    		[Parameter(Mandatory = $true)] [string]$SourceId,
    		[ValidateSet("Elapsed", "Tick")] [string]$Event = "Elapsed",
    		[Parameter(Mandatory = $true)] [long]$time,
    		[Parameter(Mandatory = $false)] [ScriptBlock]$action
    	)
    	#ENDREGION Parameters
    				
    	$timer = new-object timers.timer
    	$timer.Interval = ($time * 1000)
    
    	Register-ObjectEvent -InputObject $timer -EventName $Event –SourceIdentIFier $SourceId -Action $action | Out-Null
    
    	$timer.start()
    } #END FUNCTION Global:Start-Timer
    
    FUNCTION Global:Start-Mutex {
    	param
    	(
    		[String] $MutexName = $(throw "Name is required."),
    		[ScriptBlock] $Scriptblock = $(throw "Scriptblock is required."),
    		[Object[]] $ArgumentList
    	)
    	$MutexWasCreated = $false
    	$Mutex = $null
    	Write-Host "Waiting to acquire lock [$MutexName]..." -f Blue
    	[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
    	try {
    		$Mutex = [System.Threading.Mutex]::OpenExisting($MutexName)
    	} catch {
    		$Mutex = New-Object System.Threading.Mutex($true,$MutexName,[ref]$MutexWasCreated)
    	}
    	try {
    		if (!$MutexWasCreated) {
    			$Mutex.WaitOne() | Out-Null
    		}
    	} catch {}
    	Write-Host "Lock [$MutexName] acquired. Executing..." -f Red
    	Invoke-Command -ScriptBlock $Scriptblock -ArgumentList $ArgumentList  
    	# We're done, Release lock, even if we exit before releasing the mutex will be abandoned
    	Write-Host "Releasing lock [$MutexName]..." -f Green
    	try {
    		$Mutex.ReleaseMutex() | Out-Null
    	} catch {}
    } #END FUNCTION Global:Start-Mutex
    
    $MutexAction = {
    	[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
    	$Answer = [Microsoft.VisualBasic.Interaction]::MsgBox('Text', 'OKOnly, Exclamation,MsgBoxSetForeground,SystemModal', 'Title')
    }
    
    Start-Timer -SourceId Mutex_Timer -Event Elapsed -Time 5 -Action ({Start-Mutex -MutexName Mutex -Scriptblock $MutexAction})
    

    Dienstag, 30. September 2014 11:43
  • Deine Anforderungen sind sehr Konfus!

    Filesystemwatcher, Job, Timer ja wat denn nu?
    Du versuchst hier teilweise Techniken zu verwenden für die PowerShell nur sehr bedingt geeignet sind.

    Der Filesystemwatcher erzeugt Events diese Unterstützt PowerShell nur teilweise.

    Jeder PowerShell Job erzeugt einen eigenen Prozess.

    Ein Timer erzeugt seinen eigenen Thread und nutzt Events beides ist vonPowerShell nicht so vorgesehen.

    Ein Mutex ist in PowerShell auch erstmal fremd.

    PowerShell hat seine Grenzen bei diesem Wilden Mix aus .NET Techniken rate ich dir das in C# umzusetzen, all diese Techniken sollten dort Funktionieren.


    PowerShell Artikel, Buchtipps und kostenlose PowerShell Tutorials + E-Books
    auf der deutschsprachigen PowerShell Community

    Mein 21 Teiliger PowerShell Video Grundlehrgang
    Deutsche PowerShell Videos auf Youtube
    Folge mir auf:
    Twitter | Facebook | Google+

    Dienstag, 30. September 2014 14:02