Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = [Text.Encoding]::UTF8
- # https://seeminglyscience.github.io/powershell/2017/09/30/invocation-operators-states-and-scopes
- $GlobalState = [psmoduleinfo]::new($false)
- $GlobalState.SessionState = $ExecutionContext.SessionState
- $Job = Start-ThreadJob -Name TestJob -ArgumentList $GlobalState -ScriptBlock {
- $GlobalState = $args[0]
- . $GlobalState {
- # We always need to wait so that Get-Command itself is available
- do {
- Start-Sleep -Milliseconds 200
- } until (Get-Command Import-Module -ErrorAction Ignore)
- # other dot-sourced scripts...
- # . "$ProfileDirectory\CustomAlias.ps1"
- # . "$ProfileDirectory\CustomConstants.ps1"
- # . "$ProfileDirectory\CustomVariables.ps1"
- # . "$ProfileDirectory\CustomPrompt.ps1"
- # Import-Module -Name Sirtao
- Import-Module -Name Get-DirectoryItem
- }
- }
- $null = Register-ObjectEvent -InputObject $Job -EventName StateChanged -SourceIdentifier Job.Monitor -Action {
- # JobState: NotStarted = 0, Running = 1, Completed = 2, etc.
- if ($Event.SourceEventArgs.JobStateInfo.State -eq 'Completed') {
- $Result = $Event.Sender | Receive-Job
- if ($Result) {
- $Result | Out-String | Write-Host
- }
- $Event.Sender | Remove-Job
- Unregister-Event Job.Monitor
- Get-Job Job.Monitor | Remove-Job
- }
- elseif ($Event.SourceEventArgs.JobStateInfo.State -gt 2) {
- $Event.Sender | Receive-Job | Out-String | Write-Host
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement