Public/func_Import-SSHServer.ps1
function Import-SSHServer { [CmdletBinding()] param ( [Parameter(Position = 1, Mandatory = $true)][string]$Json, [Parameter(Position = 1, Mandatory = $false)][Switch]$Force ) [Collections.Generic.List[SSHServer]]$actualSSHServers = Get-SSHServersFile if((ConvertFrom-Json -InputObject $Json).Count -le 1) { [SSHServer]$obj = (ConvertFrom-Json -InputObject $Json)[0] [Collections.Generic.List[SSHServer]]$newSSHServers = New-Object Collections.Generic.List[SSHServer] [SSHServer]$sshServer = New-Object SSHServer $sshServer.Name = $($obj.Name) $sshServer.IP = $($obj.IP) $sshServer.Tags = $($obj.Tags) $newSSHServers.Add($sshServer) } else { [Collections.Generic.List[SSHServer]]$newSSHServers = (ConvertFrom-Json -InputObject $Json) } foreach ($newSSHServer in $newSSHServers) { if ($actualSSHServers.Where({($_.Name).ToLower() -eq $newSSHServer.Name}).Count -eq 0){ $actualSSHServers.Add($newSSHServer) } elseif (($actualSSHServers.Where({($_.Name).ToLower() -eq $newSSHServer.Name}).Count -gt 0) -and (!$Force.IsPresent)) { Write-Host "[MERGE]::An entry with the same name already exist" -ForegroundColor DarkYellow Write-Host "[MERGE]::Old entry" -ForegroundColor DarkYellow $toReplace = $actualSSHServers.Where({($_.Name).ToLower() -eq $newSSHServer.Name})[0] Out-Default -InputObject $toReplace Write-Host "[MERGE]::New entry" -ForegroundColor DarkYellow Out-Default -InputObject $newSSHServer $action = Read-Host -Prompt "Do you want to replace with the new entry ?[Y/N]" if ($action.ToLower() -eq "y") { Write-Host "[MERGE]::Replace with new entry" -ForegroundColor DarkYellow $actualSSHServers.Remove($toReplace) | Out-Null $actualSSHServers.Add([SSHServer]$newSSHServer) | Out-Null } else { Write-Host "[MERGE]::Keep old entry" -ForegroundColor DarkYellow } } elseif(($Force.IsPresent) -and $actualSSHServers.Where({($_.Name).ToLower() -eq $newSSHServer.Name}) -gt 0) { $toReplace = $actualSSHServers.Where({($_.Name).ToLower() -eq $newSSHServer.Name})[0] $actualSSHServers.Remove($toReplace) $actualSSHServers.Add($newSSHServer) } } Set-SSHServersFile -SSHServers $actualSSHServers } |