lib/TMD.PowerShell.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
function Test-TMDIsRunningAction { $Global:TmdRunningAction ?? $false } Function Import-TMDProvider { param( [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)][String]$Provider ) ## Get the PS module Root folder $ModuleFolder = (Get-Module -ListAvailable TMD.Common).path | Split-Path -Parent $Provider = $Provider.Replace(' ', '-') $extension = '.ps1' $FilePath = Join-Path $ModuleFolder 'Providers' ($Provider + $extension) if (Test-Path -Path $FilePath) { . $FilePath } } Function Invoke-WindowsPowerShell { param( [Parameter(Mandatory = $true, ValueFromPipeline = $false, Position = 0)][PSObject]$Params, [Parameter(Mandatory = $true, ValueFromPipeline = $false, Position = 0)][scriptblock]$ScriptBlock ) $WpsSession = New-PSSession -ComputerName ($env:COMPUTERNAME + '.' + $env:USERDNSDOMAIN) -ConfigurationName TMD51 -EnableNetworkAccess -Authentication Kerberos $StdOut = Invoke-Command -ScriptBlock $ScriptBlock -Session $WpsSession -ArgumentList $Params if ($null -ne $StdOut) { $ReturnObjects = $StdOut | Select-Object -Last 1 | ConvertFrom-Json -Depth 100 ## Create each of the items as a Variable object with the name foreach ($obj in $ReturnObjects.PSObject.Properties) { New-Variable -Name $obj.Name -Value $obj.Value -Force -Scope Global # $obj } } } #======================================================================== # Created By: Anders Wahlqvist # Website: DollarUnderscore (http://dollarunderscore.azurewebsites.net) #======================================================================== function Connect-PSSessionWhenReady { [CmdletBinding()] param ( [Parameter(Mandatory = $True, ValueFromPipeline)][System.Management.Automation.Runspaces.PSSession]$Session, [Parameter(Mandatory = $False, ValueFromPipeline)][Int]$MaxRetries = 51 ) ## Start a loop $ConnectionAttempts = 0 $SessionStateReady = $false While ($SessionStateReady -eq $false) { ## Set a Max Connection attempt limit if ($ConnectionAttempts -eq $MaxRetries) { $Session = New-TMDPSSession -Connect return # throw ('TMD is unable to connect to the TMD PowerShell Session ' + $Session.InstanceId + '. Please restart TMD and try again.') } ## Open the session or report the status if unable. This is a combination of Session STATE and AVAILABILITY switch ($Session.State) { "Opened" { if ($Session.Availability -eq 'Available') { $SessionStateReady = $True break } } "Disconnected" { switch ($Session.Availability) { "Available" { Connect-PSSession $Session -ErrorAction 'SilentlyContinue' | Out-Null if ($Session.Availability -eq 'Available') { $SessionStateReady = $true } break } "None" { Connect-PSSession $Session -ErrorAction 'SilentlyContinue' | Out-Null if ($Session.Availability -eq 'Available') { $SessionStateReady = $true } break } "Busy" { break } Default { break } } } "Broken" { ## The session is broken and is no longer functioning Remove it and create a new one Remove-PSSession $Session $Session = New-TMDPSSession $SessionStateReady = $True break } Default { Write-Verbose ("TMD-PS-Watcher: Session State is " + $Session.State) break } } ## If SessionReadyState is not ready if (-Not $SessionStateReady) { ## Start a Sleep Delay $SleepTime = (50..150 | Get-Random) Start-Sleep -Milliseconds $SleepTime $ConnectionAttempts++ ## Log Debugging Connecting attempts if ($ConnectionAttempts -eq 5) { Write-Host "SessionManager unable to connect 5 times" } if ($ConnectionAttempts -eq 50) { Write-Host "SessionManager unable to connect 50 times" } # ## The loop above should have connected to PowerShell, but if it didn't throw an error # if ($ConnectionAttempts -eq $MaxRetries) { # # throw ('Unable to connect to PowerShell Session' + $Session.InstanceId) # } } } } ## Provide a simple interface to get/create a TMD_<username> session on the local computer Function Get-TMDPSSession { [CmdletBinding()] param ( [Parameter()][Switch]$Connect, [Parameter()][Switch]$Disconnect, [Parameter()][Switch]$Enter ) <# # Stopping and starting TMD is unpredicitble since the PowerShell session could be affected by MANY circumstances like: # Are there still active running jobs # Did the computer hybernate # Change in network connectivity (VPN changes, blah blah) # etc. #> if ($IsWindows) { $SessionManagerInstanceId = Get-ItemProperty -Path 'HKCU:\SOFTWARE\TransitionManager' | Select-Object -ExpandProperty 'SessionManagerInstanceId' ## Get any existing PS Session started earlier $TmdUserSession = Get-PSSession -ConfigurationName 'TMD' -ComputerName 'localhost' -InstanceId $SessionManagerInstanceId -ErrorAction 'SilentlyContinue' ## There may be a broken session. If there is, remove it and create a new one. if ($TmdUserSession.State -eq 'Broken') { ## The session is broken and is no longer functioning Remove it and create a new one Remove-PSSession $TmdUserSessions $TmdUserSession = New-TMDPSSession } ## If there is no TMD session Found, try creating one if (-Not $TmdUserSession) { $TmdUserSession = New-TMDPSSession } ## Check again if to see if there was a failure above if (-Not $TmdUserSession) { Throw 'Unable to create a PowerShell Session.' } ## Handle Switches to interact with the session, or just return it if ($Connect) { Write-Host "Connecting to Session" -NoNewline Connect-PSSessionWhenReady $TmdUserSession | Out-Null Write-Host '...Connected' } if ($Disconnect) { Disconnect-PSSession $TmdUserSession | Out-Null Write-Host "Session Disconnected" } elseif ($Enter) { Write-Host "Entering Session" Connect-PSSessionWhenReady $TmdUserSession | Out-Null Enter-PSSession $TmdUserSession } else { ## If the session wasn't Connected, Disconnected or Entered, Return the Session return $TmdUserSession } } ## Get a Mac Session if ($IsMacOS) { ## Get any existing PS Session started earlier $KeyFilePath = (Join-Path $userPaths.credentials '.ssh' 'id_rsa') $TmdUserSession = New-PSSession -HostName 'localhost' -UserName $ENV:USER -KeyFilePath $KeyFilePath ## There may be a broken session. If there is, remove it and create a new one. if ($TmdUserSession.State -eq 'Broken') { ## The session is broken and is no longer functioning Remove it and create a new one Remove-PSSession $TmdUserSessions $TmdUserSession = New-TMDPSSession } ## If there is no TMD session Found, try creating one if (-Not $TmdUserSession) { $TmdUserSession = New-TMDPSSession } ## Check again if to see if there was a failure above if (-Not $TmdUserSession) { Throw 'Unable to create a PowerShell Session.' } ## Handle Switches to interact with the session, or just return it if ($Connect) { Write-Host "Connecting to Session" -NoNewline Connect-PSSessionWhenReady $TmdUserSession | Out-Null Write-Host '...Connected' } if ($Disconnect) { Disconnect-PSSession $TmdUserSession | Out-Null Write-Host "Session Disconnected" } elseif ($Enter) { Write-Host "Entering Session" Connect-PSSessionWhenReady $TmdUserSession | Out-Null Enter-PSSession $TmdUserSession } else { ## If the session wasn't Connected, Disconnected or Entered, Return the Session return $TmdUserSession } } } function New-TMDPSSession { [CmdletBinding()] param ( [Parameter()][Switch]$Disconnect, [Parameter()][Switch]$Enter ) ## Connect a Windows Session if ($IsWindows) { ## Create Session Name and create the session with environment defaults $TmdSessionConfigurationName = 'TMD' $TmdUserSession = New-PSSession -ConfigurationName $TmdSessionConfigurationName -ComputerName localhost -EnableNetworkAccess # Attempt Kerberos Authentication if (-not $TmdUserSession) { $TmdUserSession = New-PSSession -ConfigurationName $TmdSessionConfigurationName -ComputerName localhost -EnableNetworkAccess -ErrorAction SilentlyContinue -Authentication Kerberos | Out-Null } ## Attempt Negotiating Authentication if (-not $TmdUserSession) { $TmdUserSession = New-PSSession -ConfigurationName $TmdSessionConfigurationName -ComputerName localhost -EnableNetworkAccess -ErrorAction SilentlyContinue -Authentication Negotiate | Out-Null } ## Revert to Explicit Default Authentication, in case the user has a custom configuration if (-not $TmdUserSession) { $TmdUserSession = New-PSSession -ConfigurationName $TmdSessionConfigurationName -ComputerName localhost -EnableNetworkAccess -ErrorAction SilentlyContinue -Authentication 'Default' | Out-Null } ## If a TMD User Session isn't connected yet, throw an error. if (-not $TmdUserSession) { Write-Host 'PSSessionManager||Status||Session Creation Failed, Restart WinRM' Throw "Unable to create a TMD User Session. Please restart TMD." } ## Record the SessionInstanceID in the Registry for other processes to use Set-ItemProperty -Path "HKCU:\Software\TransitionManager" -Name "SessionManagerInstanceId" -Value $TmdUserSession.InstanceId ## While Connected, ensure the Session has a SessionCache Variable Invoke-Command -Session $TmdUserSession -ScriptBlock { ## Import TMD Import-Module TMD.Common ## Import TransitionManager Module Import-Module TransitionManager ## Import PoshRSJobs Import-Module PoshRSJob } ## Handle Switches to interact with the session, or just return it if ($Disconnect) { Disconnect-PSSession $TmdUserSession | Out-Null Write-Host "Session Disconnected" } elseif ($Enter) { Write-Host "Entering Session" Enter-PSSession $TmdUserSession } else { ## Return the new session return $TmdUserSession } } ## Connect on a Mac if ($IsMacOS) { ## Create an SSH based session to PowerShell on the SSH endpoint $KeyFilePath = (Join-Path $env:HOME '.ssh' 'id_rsa') $TmdUserSession = New-PSSession -HostName 'localhost' -UserName $ENV:USER -KeyFilePath $KeyFilePath if (-not $TmdUserSession) { Write-Host 'PSSessionManager||Status||Session Creation Failed, Restart TMD and Try again.' Throw "Unable to create a TMD User Session. Restart TMD" } ## While Connected, ensure the Session has a SessionCache Variable Invoke-Command -Session $TmdUserSession -ScriptBlock { ## Import TMD Import-Module TMD.Common ## Import TransitionManager Module Import-Module TransitionManager ## Import PoshRSJobs Import-Module PoshRSJob } ## Return the new session return $TmdUserSession } } |