Home > OS >  Loop PS from specific line
Loop PS from specific line

Time:10-31

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 witch I enter before in db) and run the scrit for that ID.

CodePudding user response:

I would do this inside one endless while {..} loop where another do{..} while() is used for the second question about the IP.

Also, you can do all from memory and only write the data to file after the user quits the while loop (and only if an update was made)

Something like this:

$dbfile =  "C:\test.csv"

# create a List object to store and update the data in memory
$db = [System.Collections.Generic.List[object]]::new()

# if the file can be found, import the data and add to the $db List 
if (Test-Path -Path $dbfile -PathType Leaf) { $db.AddRange((Import-Csv -Path $dbfile)) }

# create a flag to check later if an new entry was made
$dbUpdated = $false  

# enter an endless loop
while ($true) {
    $inputID = Read-Host -Prompt "Introduceti ID. Type 'Q' to exit"
    # break the loop if the user wants to stop
    if ($inputID -eq 'q') { break }  

    # test if the id is already in the $db collection
    $entry = $db | Where-Object { $_.HostName -eq $inputID }
    if ($entry) {
        Write-Host "$inputID Ok!" -ForegroundColor Green
        continue  # go back to asking for a new ID
    }

    Write-Host "$inputID nu exista in baza de date!" -ForegroundColor Red
    # ask for confirmation
    $title    = 'Add New ID?'
    $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  # go back to asking for a new ID
    }

    # ask for IP for this new entry
    do {
        $IP = Read-Host -Prompt "Introduceti IP for $inputID"
    } while ([string]::IsNullOrWhiteSpace($IP))
    # you may want to change the 'while' condition above to 
    #   while (![IPAddress]::TryParse($IP, [ref]$null))
    # to ensure a valid IP address is entered.

    # add the given ID and IP to the $db collection in memory
    $db.Add([PsCustomObject]@{ HostName = $inputID; IP = $IP })
    Write-Host "Data : $inputID,$IP adaugat cu succes in baza de date!"
    $dbUpdated = $true
}

# here we can test if new entries have been added
if ($dbUpdated) {
    # overwrite the file with the updated info
    $db | Export-Csv -Path $dbfile -NoTypeInformation -Force

    # Apparently you don't want quotes in the CSV file, and in this case it would be safe enough to do
    $dbTrimmer = Get-Content $dbfile
    $dbTrimmer.Replace('","', ",").TrimStart('"').TrimEnd('"') | Set-Content -Path $dbfile -Force -Confirm:$false

    # but please be VERY careful with that because in other csv files, the fields may contain
    # Newlines, the delimiter character itself or quotes and if that is the case, the csv file 
    # will become unusable.. 
    # See https://stackoverflow.com/a/69705776/9898643 to do that safely. 
}
  • Related