Home > Software engineering >  How to run a powershell script without displaying a window and timeout issue
How to run a powershell script without displaying a window and timeout issue

Time:12-04

I have this script to check for file creation and give me a balloon notification. How do I get this to run without having a powershell console/window open? Ideally would like to be able to run this from task scheduler at log on, and run continuously - which is the other problem, it seems to stop running after a while and I'm not sure why.

Any solutions?

Note. I have strung this together from snippets copied off chats, I am not a programmer.

### FOLDER WATCHER
    $filewatcher = New-Object System.IO.FileSystemWatcher
    #SET FOLDER TO WATCH
    $filewatcher.Path = "\\dc1\Harrows CNC_Full Access\"
    $filewatcher.Filter = "*.pg_"
    #include subdirectories
    $filewatcher.IncludeSubdirectories = $true
    $filewatcher.EnableRaisingEvents = $true
###Define Actions after event is raised
    $writeaction = {
                    $path = $Event.SourceEventArgs.FullPath
                    $filename = [System.IO.Path]::GetFileNameWithoutExtension($path) 
                    $ownerwithdomain = Get-Acl $path | Select-Object -ExpandProperty Owner
                    $domain,$owner = $ownerwithdomain.split('\')
                        Add-Type -AssemblyName  System.Windows.Forms 
                        $global:balloon = New-Object System.Windows.Forms.NotifyIcon 
                        Get-Member -InputObject  $Global:balloon 
                        [void](Register-ObjectEvent  -InputObject $balloon  -EventName MouseDoubleClick  -SourceIdentifier IconClicked  -Action {
                          #Perform  cleanup actions on balloon tip
                          $global:balloon.dispose()
                          Unregister-Event  -SourceIdentifier IconClicked
                          Remove-Job -Name IconClicked
                          Remove-Variable  -Name balloon  -Scope Global
                        }) 
                        $path = (Get-Process -id $pid).Path

                        $balloon.Icon  = [System.Drawing.Icon]::ExtractAssociatedIcon($path) 
                        [System.Windows.Forms.ToolTipIcon] | Get-Member -Static -Type Property
                        $balloon.BalloonTipIcon  = [System.Windows.Forms.ToolTipIcon]::Info
                        $balloon.BalloonTipText  = $owner, ' just opened ', $filename

                        $balloon.BalloonTipTitle  = "Attention:"
                        $balloon.Visible  = $true
                        $balloon.ShowBalloonTip(1000)
    }
###Decide which events should be watched
Register-ObjectEvent $filewatcher “Created” -Action $writeaction
while ($true) {sleep 5}

CodePudding user response:

In the Task Scheduler you can start the script with with: powershell.exe -windowstyle hidden -File "path to script" There you can also make a trigger, that it should be executed at logon. In each Task there is an option, that the task ends after a specific amount of time. You can disable that.

  • Related