Created
July 12, 2020 17:00
-
-
Save ffalor/827a9768706e5a4075f2129c5435491c to your computer and use it in GitHub Desktop.
Powershell script to convert CSV to Bolt Inventory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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