Last active
December 11, 2023 11:58
-
-
Save gonzalc/ab4b0ffa7aea7eead801805a05a57033 to your computer and use it in GitHub Desktop.
Run PowerShell script without displaying a window
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ErrorActionPreference = 'SilentlyContinue' | |
$fs = 'fileserver' | |
$drive = @{ | |
'Personal' = '\\{0}\{1}' -f $fs, $env:USERNAME | |
'Training' = '\\{0}\Training' -f $fs | |
'Transfer' = '\\{0}\Transfer' -f $fs | |
} | |
New-PSDrive -Name 'P' -PSProvider FileSystem -Root $drive.Personal -Description 'Personal Files' -Persist | |
New-PSDrive -Name 'T' -PSProvider FileSystem -Root $drive.Training -Description 'Training Files' -Persist | |
New-PSDrive -Name 'X' -PSProvider FileSystem -Root $drive.Transfer -Description 'Transfer Files' -Persist | |
Function Invoke-BalloonTip { | |
<# | |
https://mcpmag.com/articles/2017/09/07/creating-a-balloon-tip-notification-using-powershell.aspx | |
https://github.com/proxb/PowerShell_Scripts/blob/master/Invoke-BalloonTip.ps1 | |
#> | |
[CmdletBinding()] | |
Param ( | |
[Parameter(Mandatory=$True,HelpMessage="The message text to display. Keep it short and simple.")] | |
[string]$Message, | |
[Parameter(HelpMessage="The message title")] | |
[string]$Title="Attention $env:username", | |
[Parameter(HelpMessage="The message type: Info,Error,Warning,None")] | |
[System.Windows.Forms.ToolTipIcon]$MessageType="Info", | |
[Parameter(HelpMessage="The path to a file to use its icon in the system tray")] | |
[string]$SysTrayIconPath='C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe', | |
[Parameter(HelpMessage="The number of milliseconds to display the message.")] | |
[int]$Duration=1000 | |
) | |
Add-Type -AssemblyName System.Windows.Forms | |
If (-NOT $global:balloon) { | |
$global:balloon = New-Object System.Windows.Forms.NotifyIcon | |
#Mouse double click on icon to dispose | |
[void](Register-ObjectEvent -InputObject $balloon -EventName MouseDoubleClick -SourceIdentifier IconClicked -Action { | |
#Perform cleanup actions on balloon tip | |
Write-Verbose 'Disposing of balloon' | |
$global:balloon.dispose() | |
Unregister-Event -SourceIdentifier IconClicked | |
Remove-Job -Name IconClicked | |
Remove-Variable -Name balloon -Scope Global | |
}) | |
} | |
#Need an icon for the tray | |
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path | |
#Extract the icon from the file | |
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($SysTrayIconPath) | |
#Can only use certain TipIcons: [System.Windows.Forms.ToolTipIcon] | Get-Member -Static -Type Property | |
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]$MessageType | |
$balloon.BalloonTipText = $Message | |
$balloon.BalloonTipTitle = $Title | |
$balloon.Visible = $true | |
#Display the tip and specify in milliseconds on how long balloon will stay visible | |
$balloon.ShowBalloonTip($Duration) | |
Write-Verbose "Ending function" | |
} | |
Invoke-BalloonTip -Title 'Logon Script' -Message 'Mapped Drives' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rem https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/rem-statement | |
Rem https://ss64.com/vb/run.html | |
set shell = CreateObject("WScript.Shell") | |
cmd = "powershell.exe -nologo -executionpolicy bypass -file .\logon.ps1 | |
shell.Run cmd,0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment