Wissen/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 $profile.AllUsersCurrentHost $profile.CurrentUserAllHosts $profile.CurrentUserCurrentHost # * # * 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\Library.ps1" # Der enthaltene Code gehört zum akt. Ausführungscontext => Für ausgelagerten Code & "c:\temp\Library.ps1" # Der enthaltene Code wird in einem eigenen 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 #region ScriptBlock-Code über eine .EXE-Datei ausführen Set-Location C:\temp Remove-Item .\start.* -Force $csharpCode = @" using System; using System.Diagnostics; namespace WindowsFormsApplication1 { static class Program { [STAThread] static void Main() { var filename = "PowerShell.exe"; var parameter = "-NoExit -ExecutionPolicy ByPass -Command \"& {Write-Host -ForegroundColor Yellow -Object 'No Limits!'}\""; Process.Start(filename, parameter); } } } "@ $csharpCode | Set-Content start.cs -Encoding UTF8 -Force Start-Process -FilePath "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" ` -ArgumentList "/target:exe", ` "/out:start.exe", ` "start.cs" # * s.a. https://msdn.microsoft.com/en-us/library/78f4aasd.aspx .\start.exe # Testen... #endregion |