我有這個腳本:我可以添加一個回圈嗎?在我輸入 ID 和 IP 并添加 'db.csv' 后,回傳再次詢問 $inputID 并回圈詢問輸入的 ID 或 IP 是否好(?)
我需要在這個腳本第一個回圈中使用 2 個“回圈”來確認輸入的資訊是否正確 >> 如果沒有回傳再次添加;; 在我添加 ID、IP 和腳本寫入 csv 新 ID 之后的第二個回圈再次進入腳本頂部,其中需要 $inputID 以輸入先前添加的最新 ID。
#### 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.
uj5u.com熱心網友回復:
我會在一個無限while {..}回圈中執行此操作,其中另一個do{..} while()用于關于 IP 的第二個問題。
此外,您可以從記憶體中完成所有操作,并且僅在用戶退出 while 回圈后才將資料寫入檔案(并且僅在進行了更新時)
像這樣的東西:
$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.
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/343075.html
下一篇:使用for回圈向后迭代不起作用
