I made some quick and dirty modification of the script you referenced.
The script is supposed to support SSH, Telnet, Web, VMware, SFTP.
Currently I tested that on a .csv with 200 items, it will end up at around 500. Didn’t test SFTP part yet. Going to add RDP option.
I will still have to work on combining this with predefined credential set and some mapping of them in the CSV.
Sample input CSV:
Folder,Name,URI,Port,ObjType,CredUser,CredPass
Connections/Labs/General,vSphere,10.10.0.100,RoyalVMwareConnection,someusername,somepassword
Connections/Labs/Lab1,Core-A,10.1.1.200,
Connections/Labs/Lab1/Switches,1-Switch-A,10.1.1.11,
Connections/Labs/Lab3/Web Servers,Svr1,https://10.1.2.3:9443,RoyalWebConnection,
If ObjType is blank, SSH/Telnet is assumed.
For Port:
- If SSH/Telnet, and Port is blank → Telnet w/ port 23
- If SSH/Telnet, and Port is 22 → SSH w/ port 22
- If SSH/Telnet, and Port is custom → Telnet w/ custom port no.
Import-Module RoyalDocument.PowerShell
function CreateRoyalFolderHierarchy()
{
param(
[string]$folderStructure,
[string]$splitter,
$Folder
)
$currentFolder = $Folder
$folderStructure -split $splitter | %{
$folder = $_
$existingFolder = Get-RoyalObject -Folder $currentFolder -Name $folder -Type RoyalFolder
if($existingFolder)
{
Write-Verbose "Folder $folder already exists - using it"
$currentFolder = $existingFolder
}
else
{
Write-Verbose "Folder $folder does not exist - creating it"
$newFolder= New-RoyalObject -Folder $currentFolder -Name $folder -Type RoyalFolder
$currentFolder = $newFolder
}
}
return $currentFolder
}
$fileName = "test.rtsz" #relative to the current file-system directory
#if(Test-Path $fileName) {Remove-Item $fileName}
$store = New-RoyalStore -UserName "PowerShellUser"
$doc = New-RoyalDocument -Store $store -Name "Powershell import from CSV" -FileName $fileName
Import-CSV -Path test.csv | %{
$server = $_
Write-Host "Importing $($server.Name)"
$lastFolder = CreateRoyalFolderHierarchy -folderStructure $server.Folder -Splitter "\/" -Folder $doc
if ([string]::IsNullOrWhiteSpace($server.ObjType)) {
$newConnection = New-RoyalObject -Folder $lastFolder -Type RoyalSSHConnection -Name $server.Name
if ([string]::IsNullOrWhiteSpace($server.Port)) {
$newConnection.ConnectionType = "telnet;Telnet"
$newConnection.Port = "23"
}
else {
switch($server.Port) {
"22" {
$newConnection.ConnectionType = "ssh"
$newConnection.Port = "22"
}
default {
$newConnection.ConnectionType = "telnet;Telnet"
$newConnection.Port = $server.Port
}
}
}
}
else {
switch($server.ObjType) {
"RoyalWebConnection" {
$newConnection = New-RoyalObject -Folder $lastFolder -Type RoyalWebConnection -Name $server.Name
}
"RoyalVMwareConnection" {
$newConnection = New-RoyalObject -Folder $lastFolder -Type RoyalVMwareConnection -Name $server.Name
$newConnection.CredentialMode = 2
$newConnection.CredentialUsername = $server.CredUser
$newConnection.CredentialPassword = $server.CredPass
}
"RoyalFileTransferConnection" {
$newConnection = New-RoyalObject -Folder $lastFolder -Type RoyalFileTransferConnection -Name $server.Name
$newConnection.Protocol = 3
$newConnection.CredentialUsername = $server.CredUser
$newConnection.CredentialPassword = $server.CredPass
}
"RoyalSSHConnection" {
$newConnection = New-RoyalObject -Folder $lastFolder -Type RoyalSSHConnection -Name $server.Name
if ([string]::IsNullOrWhiteSpace($server.Port)) {
$newConnection.ConnectionType = "telnet;Telnet"
$newConnection.Port = "23"
}
else {
switch($server.Port) {
"22" {
$newConnection.ConnectionType = "ssh"
$newConnection.Port = "22"
}
default {
$newConnection.ConnectionType = "telnet;Telnet"
$newConnection.Port = $server.Port
}
}
}
}
}
}
$newConnection.URI = $server.URI
}
Out-RoyalDocument -Document $doc -FileName $fileName
Any advice on how this can be improved is more than welcome.
Kind regards,
Tomasz