[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$serversarray = @()
$cred = Get-Credential
$capture = Invoke-RestMethod -Uri "https://towerhostURL/api/v2/hosts/" -Method Get -Credential $cred
$count = $capture.results.Count
for ($i = 0; $i -lt $count; $i++) {
$servername = $capture.results[$i].name
$group = $capture.results[$i].summary_fields.groups.results[0].name
$server = New-Object System.Object
$server | Add-Member -MemberType NoteProperty -Name ServerName -Value $servername
$server | Add-Member -MemberType NoteProperty -Name Group -Value $group
$serversarray += $server
}
while ($capture.next -ne $null) {
Write-Host $capture.next
$link = $capture.next
$capture = Invoke-RestMethod -Uri "https://towerhostURL$link" -Method Get -Credential $cred
$count = $capture.results.Count
for ($i = 0; $i -lt $count; $i++) {
$servername = $capture.results[$i].name
$group = $capture.results[$i].summary_fields.groups.results[0].name
$server = New-Object System.Object
$server | Add-Member -MemberType NoteProperty -Name ServerName -Value $servername
$server | Add-Member -MemberType NoteProperty -Name Group -Value $group
$serversarray += $server
}
}
$serversarray | Export-Csv -NoTypeInformation .\Ansiblehostslist.csv
$serversarray.Clear()
No comments:
Post a Comment