Advertisement
Sirtao

$Profile

Jan 29th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.59 KB | Source Code | 0 0
  1.  
  2. $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = [Text.Encoding]::UTF8
  3. # https://seeminglyscience.github.io/powershell/2017/09/30/invocation-operators-states-and-scopes
  4. $GlobalState = [psmoduleinfo]::new($false)
  5. $GlobalState.SessionState = $ExecutionContext.SessionState
  6.  
  7. $Job = Start-ThreadJob -Name TestJob -ArgumentList $GlobalState -ScriptBlock {
  8.     $GlobalState = $args[0]
  9.     . $GlobalState {
  10.  
  11.         # We always need to wait so that Get-Command itself is available
  12.         do {
  13.             Start-Sleep -Milliseconds 200
  14.         } until (Get-Command Import-Module -ErrorAction Ignore)
  15.  
  16.        
  17.         # other dot-sourced scripts...
  18.         # . "$ProfileDirectory\CustomAlias.ps1"
  19.         # . "$ProfileDirectory\CustomConstants.ps1"
  20.         # . "$ProfileDirectory\CustomVariables.ps1"
  21.         # . "$ProfileDirectory\CustomPrompt.ps1"
  22.  
  23.         # Import-Module -Name Sirtao
  24.         Import-Module -Name Get-DirectoryItem
  25.  
  26.     }
  27. }
  28.  
  29.  
  30.  
  31. $null = Register-ObjectEvent -InputObject $Job -EventName StateChanged -SourceIdentifier Job.Monitor -Action {
  32.     # JobState: NotStarted = 0, Running = 1, Completed = 2, etc.
  33.     if ($Event.SourceEventArgs.JobStateInfo.State -eq 'Completed') {
  34.         $Result = $Event.Sender | Receive-Job
  35.         if ($Result) {
  36.             $Result | Out-String | Write-Host
  37.         }
  38.  
  39.         $Event.Sender | Remove-Job
  40.         Unregister-Event Job.Monitor
  41.         Get-Job Job.Monitor | Remove-Job
  42.     }
  43.     elseif ($Event.SourceEventArgs.JobStateInfo.State -gt 2) {
  44.         $Event.Sender | Receive-Job | Out-String | Write-Host
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement