Skip to content

Commit

Permalink
Fix #2 reuse open PSSessions
Browse files Browse the repository at this point in the history
  • Loading branch information
DexterPOSH committed Jun 21, 2016
1 parent 604d98b commit 1f9926a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
20 changes: 12 additions & 8 deletions Remotely/private/RemoteSession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,26 @@ 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' {

foreach($Node in $Nodes)
{
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)
}
Expand Down
51 changes: 29 additions & 22 deletions Remotely/private/bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 1f9926a

Please sign in to comment.