I have this script: Can I add a loop ; after I Enter ID & IP and add in 'db.csv' go back to ask again $inputID and a loop for ask if typed ID or IP is good (?)
I need 2 'loops' in this script first loop for the confirmation typed if info is good or not >> if not go back to add again ;; and second loop after I add ID, IP and script write in csv new ID go again in top of script where need to $inputID for type newest ID which added previous.
#### START SCRIPT #####
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$dbfile = "C:\test.csv"
$db = [System.Collections.Generic.List[object]]::new()
if (Test-Path -Path $dbfile -PathType Leaf) { $db.AddRange((Import-Csv -Path $dbfile)) }
$dbUpdated = $false
while ($true) {
While ( ($Null -eq $inputID) -or ($inputID -eq '') ) {
$inputID = Read-Host -Prompt "Introduceti ID sau Tastati 'Q' for exit"
}
if ($inputID -eq 'q') { break }
$entry = $db | Where-Object { $_.HostName -eq $inputID }
if ($entry) {
Write-Host "$inputID Ok!" -ForegroundColor Green
continue
}
Write-Host "$inputID nu exista in baza de date!" -ForegroundColor Red
# ask for confirmation
$title = 'Adaugare ID nou?'
$question = 'Doriti sa introduceti un ID nou in Baza de Date?'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -ne 0) {
continue
}
do {
$IP = Read-Host -Prompt "Introduceti IP pentru: $inputID"
} while (![IPAddress]::TryParse($IP, [ref]$null))
$db.Add([PsCustomObject]@{ HostName = $inputID; IP = $IP })
Write-Host "Data : $inputID,$IP adaugat cu succes in baza de date!"
$dbUpdated = $true
}
if ($dbUpdated) {
$db | Export-Csv -Path $dbfile -NoTypeInformation -Force
$dbTrimmer = Get-Content $dbfile
$dbTrimmer.Replace('","', ",").TrimStart('"').TrimEnd('"') | Set-Content -Path $dbfile -Force -Confirm:$false
}
# Script continue. If put correct ID run the script below if not Ask if want to add new ID after that return to ask ID (of course here I typed new ID which I enter before in db) and run the script for that ID.