Private/Wissen/C_Scripting/C09_ScriptAutostart.ps1

# ? TITEL Autostartdateien
# ? DESCRIPTION PowerShell-Code beim starten eine Session automatisch ausführen.
# ? TAGS profile prompt exe link
# ? VERSION 2019.10.18.0800

# ! - Code der in den u.a. Dateien steht wird automatisch ausgeführt wenn
# ! eine PowerShell-Session (Remote, Host, Anwendung) startet.
# ! - Die u.a. Ordner/Dateien müssen zu erst erstellt werden.
# ! - ZWECK: Um seine PS-Umgebung 'gemütlich' einzurichten:
$profile.AllUsersAllHosts        # * z.B. C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
$profile.AllUsersCurrentHost     # * z.B. C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.VSCode_profile.ps1
$profile.CurrentUserAllHosts     # * z.B. C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1
$profile.CurrentUserCurrentHost  # * z.B. C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1

# *
# * z.B. das ständige Ausführen einer *.ps1-Datei
# *

Get-ChildItem -Path Function:\prompt | Select-Object -ExpandProperty Definition
# ! könnte man verbessern in:
Add-Content -Force -Path $profile.CurrentUserAllHosts -Value @'
function Prompt {
    $lastCmd = Get-History -Count 1
    if ($null -ne $lastCmd) {
        $timeSpan = $lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime
        "[ {0}h {1}m {2}s {3}ms ] {4}" -f $timeSpan.Hours, $timeSpan.Minutes, $timeSpan.Seconds, $timeSpan.Milliseconds, $executionContext.SessionState.Path.CurrentLocation |
          Write-Host -NoNewline
      } else {
        "[ 0 ms ] $($executionContext.SessionState.Path.CurrentLocation)" | Write-Host -NoNewline
    }
}
'@


# !
# ! Semi-Automatisch
# !

# ... per Aufruf in der Console
. "c:\temp\Test.ps1" # Der enthaltene Code gehört zum aktuell Ausführungscontext => Für ausgelagerten Code
& "c:\temp\Test.ps1" # Der enthaltene Code wird in einem GETRENNTEN Context ausgeführt => Für das laden von Cmdlets, Aliase, etc.

# ... per Datei-Verknüpfung auf:
powershell.exe -File "c:\temp\MachWas.ps1" -WindowStyle Hidden
powershell.exe -File "c:\temp\Get-EuroExchange.ps1" -NoExit

# ... per RUN-Schlüssel
# ... per GPO
# ... per Module