Skip to content

Instantly share code, notes, and snippets.

@ffalor
Created July 12, 2020 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffalor/827a9768706e5a4075f2129c5435491c to your computer and use it in GitHub Desktop.
Save ffalor/827a9768706e5a4075f2129c5435491c to your computer and use it in GitHub Desktop.
Powershell script to convert CSV to Bolt Inventory
$user = Read-Host -Prompt 'Input username'
$password = Read-Host -Prompt 'Input password'
$csv_path = Read-Host -Prompt 'Input path to inventory CSV'
$yaml_module = "powershell-yaml"
if ($null -eq (Get-InstalledModule -Name $yaml_module -ErrorAction SilentlyContinue)) {
Install-Module $yaml_module
}
$csv_data = Get-Content $csv_path | ConvertFrom-Csv
[System.Collections.ArrayList]$WindowsList = @()
[System.Collections.ArrayList]$LinuxList = @()
foreach ($target in $csv_data) {
$server = $target.server
if ($target.os -eq "windows") {
$WindowsList.Add($server) | Out-Null
}
else {
$LinuxList.Add($server) | Out-Null
}
}
$yaml = ConvertTo-Yaml @{"groups" = @(
@{"name" = "linux";
"targets" = $LinuxList;
"config" = @{"transport" = "ssh";
"ssh" = @{"host-key-check" = $false; "user" = $user; "password" = $password; "port" = 22 }
}
},
@{"name" = "windows";
"targets" = $WindowsList;
"config" = @{"transport" = "winrm";
"winrm" = @{ "user" = $user; "password" = $password; "port" = 5985 }
}
}
)
}
Set-Content -Path .\inventory.yaml -Value $yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment