I have a PS script that starts a batch file, which in turn calls a script which deletes and downloads files from SFTP. Once all SFTP activities in the script are complete, the batch file then opens the next PS script to be run as a tab in PS ISE. Perfectly happy with everything so far... working 100%. What I want to do is have the second script run automatically, with no user input required. i.e. as soon as the second scripts tab is opened in PS, I want it executed.
Tried a number of things suggested by ChatGPT.. the last of which yielded no result or action and is as follows:
# Define the script to run
$ScriptToRun = "C:\Users\REDACTED\Desktop\SCRIPT_TO_AUTORUN.ps1"
# Register an event that triggers when a new tab is opened
Register-EngineEvent -SourceIdentifier PowerShell.OnIdle -Action {
# Check if the current PowerShell tab is the ISE
if ($Host.Name -eq "Windows PowerShell ISE Host") {
# Execute the script
Invoke-Expression -Command "& '$ScriptToRun'"
# Unregister the event to prevent it from firing again
Unregister-Event -SourceIdentifier PowerShell.OnIdle
}
}