From 1f9926a71ec33251c22347a52cc5e29e42d68a92 Mon Sep 17 00:00:00 2001 From: DexterPOSH Date: Tue, 21 Jun 2016 10:30:39 +0530 Subject: [PATCH] Fix #2 reuse open PSSessions --- Remotely/private/RemoteSession.ps1 | 20 +++++++----- Remotely/private/bootstrap.ps1 | 51 +++++++++++++++++------------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/Remotely/private/RemoteSession.ps1 b/Remotely/private/RemoteSession.ps1 index bb58ee7..0564279 100644 --- a/Remotely/private/RemoteSession.ps1 +++ b/Remotely/private/RemoteSession.ps1 @@ -65,7 +65,7 @@ function CreateSessions $PSSessionOption = New-PSSessionOption -NoMachineProfile } # try to see if there are already open PSSessions - $existingPSSession = @(Get-PSSession -Name Remotely* | Where -Property State -eq 'Opened') + $existingPSSessions = @(Get-PSSession -Name Remotely* | Where -Property State -eq 'Opened') Switch -Exact ($PSCmdlet.ParameterSetName) { 'ComputerName' { @@ -73,14 +73,18 @@ function CreateSessions { if(-not $Remotely.SessionHashTable.ContainsKey($Node)) { - $sessionName = "Remotely-" + $Node - if ($CredentialHash -and $CredentialHash[$Node]) - { - $sessionInfo = CreateSessionInfo -Session (New-PSSession -ComputerName $Node -Name $sessionName -Credential $CredentialHash[$node] -SessionOption $PSSessionOption) -Credential $CredentialHash[$node] + $sessionName = "Remotely-" + $Node + $existingPSSession = $existingPSSessions | Where-Object -Property Name -eq $SessionName + if ($existingPSSession) { + $sessionInfo = CreateSessionInfo -Session $existingPSSession } - else - { - $sessionInfo = CreateSessionInfo -Session (New-PSSession -ComputerName $Node -Name $sessionName -SessionOption $PSSessionOption) + else { + if ($CredentialHash -and $CredentialHash[$Node]) { + $sessionInfo = CreateSessionInfo -Session (New-PSSession -ComputerName $Node -Name $sessionName -Credential $CredentialHash[$node] -SessionOption $PSSessionOption) -Credential $CredentialHash[$node] + } + else { + $sessionInfo = CreateSessionInfo -Session (New-PSSession -ComputerName $Node -Name $sessionName -SessionOption $PSSessionOption) + } } $Remotely.SessionHashTable.Add($sessionInfo.session.ComputerName, $sessionInfo) } diff --git a/Remotely/private/bootstrap.ps1 b/Remotely/private/bootstrap.ps1 index bce73ac..2aad82c 100644 --- a/Remotely/private/bootstrap.ps1 +++ b/Remotely/private/bootstrap.ps1 @@ -32,7 +32,7 @@ Function TestRemotelyNodeBootStrapped { $SessionInfo ) if ($Remotely.NodeMap.Count -gt 0) { - if($Remotely.NodeMap.ContainsKey($($SessionInfo.Session.ComputerName))) { + if($Remotely.NodeMap.NodeName -contains $($SessionInfo.Session.ComputerName)) { $True } else { @@ -61,28 +61,35 @@ Function BootstrapRemotelyNode { # Add the above information to the Remotely var UpdateRemotelyNodeMap -ModuleStatus $ModuleStatus -PathStatus $PathStatus -NodeName $session.ComputerName - foreach ($hash in $moduleStatus.GetEnumerator()) { + if ($pathStatus -and $($moduleStatus.Values -notcontains $False)) { + # Node is already bootstrapped, no need to take action + } + else { + # Node is missing some of the configs, bootstrap it + foreach ($hash in $moduleStatus.GetEnumerator()) { if ($hash.Value) { - # module present on the remote node - } - else { - # module not present on the remote node - CopyRemotelyNodeModule -Session $session -FullyQualifiedName $($FullyQualifiedName | Where -Property Name -EQ $hash.Name) - } - } - - if ($pathStatus) { - # remotely node path created - } - else { - CreateRemotelyNodePath -session $session -Path $remotelyNodePath - } - - # at the end update the status of the node - $ModuleStatus, $pathStatus = TestRemotelyNode @PSBoundParameters - # Add the above information to the Remotely var - UpdateRemotelyNodeMap -ModuleStatus $ModuleStatus -PathStatus $PathStatus + # module present on the remote node + } + else { + # module not present on the remote node + CopyRemotelyNodeModule -Session $session -FullyQualifiedName $($FullyQualifiedName | Where -Property Name -EQ $hash.Name) + } + } + + if ($pathStatus) { + # remotely node path created + } + else { + CreateRemotelyNodePath -session $session -Path $remotelyNodePath + } + + # at the end update the status of the node + $ModuleStatus, $pathStatus = TestRemotelyNode @PSBoundParameters + # Add the above information to the Remotely var + UpdateRemotelyNodeMap -ModuleStatus $ModuleStatus -PathStatus $PathStatus -NodeName $session.ComputerName + } + } Function TestRemotelyNode { @@ -195,7 +202,7 @@ Function CopyModuleFolderToRemotelyNode { [Parameter(Mandatory)] [String]$Destination ) - Invoke-Command -Session $Session -ScriptBlock {$null = New-Item -Path $Using:Destination} -ErrorAction SilentlyContinue + Invoke-Command -Session $Session -ScriptBlock {$null = New-Item -Path $Using:Destination -ItemType Directory} -ErrorAction SilentlyContinue #$folderName = Split-Path -Path $Path -Leaf Copy-Item -Path "$Path\*" -Destination $Destination -ToSession $session -Force }