Updating values in CSV

Hello,

Can someone help me with this? I typed in 2000, and it grabbed the correct CoreID (FMD354200000), but as you can see below, it added my computer name to a 1000 core ID. I want it to add it to the FMD354200000 CoreID

Branch Number,CoreID,Available
1000,FMD354100000,2UA51427CH
1000,FMD354100001,Yes
1000,FMD354100002,Yes
1000,FMD354100003,Yes
1000,FMD354100004,Yes
2000,FMD354200000,Yes
2000,FMD354200001,Yes
2000,FMD354200002,Yes
2000,FMD354200003,Yes

$Workstation = $env:COMPUTERNAME
$CoreIP = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\CORe\IP"
$CSV = '\\hqfs1\users\tantony\PowerShell\GetCoreID\CoreID.csv'
$ImportCSV = Import-Csv $CSV
$GetBranchNum = Read-Host "Enter branch number and press ENTER: "

if($ImportCSV | Where-Object {$_."Branch Number" -eq $GetBranchNum})
{
    Write-Host -NoNewline "Branch number found in CSV file!`t" -ForegroundColor Green

    if($ImportCSV | Where-Object {$_."Available" -eq "Yes".Trim() -eq "Y" -and $_."Branch Number".Trim() -eq $GetBranchNum})
    {       

        Write-Host "$HowManyLeft Available Core ID found!" -ForegroundColor Green

        $First = $ImportCSV | Where-Object {$_.'Branch Number'.Trim() -eq $GetBranchNum -and $_.Available.Trim() -eq "Yes"} | Select-Object -First 1

        $AddCoreID = $First.CoreID

        New-ItemProperty -path $CoreIP -name "TTable ID" -PropertyType String -Value $AddCoreID -Force | Select-Object "TTable ID" | Format-List

        $lines = Get-Content -Path $CSV -Raw

        $pattern = $First.Available
        
        $regEx = New-Object -TypeName 'System.Text.RegularExpressions.Regex' -ArgumentList $pattern
        
        $result = $regEx.Replace($lines, $Workstation, 1)

        $result | Out-File -FilePath $CSV -Encoding ascii -Force

    }

    else
    {
        Write-Host "No Available Core ID found!" -ForegroundColor Red           
    }
}

else    
{
    Write-Warning "Branch number not found in CSV file!"
}

What does this do for you when you paste it in the shell?

“Yes”.Trim() -eq “Y”

PS C:\Windows\system32> "Yes".Trim() -eq "Y"
False

False

now. “yes” -eq “Yes”.Trim() -eq “n”

PS C:\Windows\system32> "yes" -eq "Yes".Trim() -eq "n"
True
PS C:\Windows\system32> "yes" -eq "Yes".Trim() -eq "n"
True

Make sure your logic works before you continue with the rest of it.

$exit = $null
$importcsv = import-csv .\branches.csv

while ($exit -ne $true) {
	
	$GetBranchNum = Read-Host "Enter branch number and press ENTER: "
	
	$available = $ImportCSV | Where-Object { $_.Available -eq 'Yes' -and $_.'Branch Number' -eq $GetBranchNum }
	
	if ($available) {
		
		$exit = $true
		
		$available[0].available = 'Used'
		
		$importcsv
		
		$importcsv | export-csv branches.csv
		
		
	} else {
		
		'branch number not found or available'
	}
	
}

Yes, my logic is correct, just having issue with updating the csv

I used your code to test, and entered 2000 and got this below for output

Branch Number CoreID       Available 
------------- ------       --------- 
1000          FMD354100000 2UA51427CH
1000          FMD354100001 2UA51427CH
1000          FMD354100002 2UA51427CH
1000          FMD354100003 Yes       
1000          FMD354100004 Yes       
2000          FMD354200000 Used      
2000          FMD354200001 Yes       
2000          FMD354200002 Yes       
2000          FMD354200003 Yes

“yes” -eq “Yes”.Trim() -eq “kfjdlsjfdkjf” is always true in your code. take out all the trim stuff, if your csv is formatted correctly you don’t need it.

Each time you run it it’s going to take that first available assign used to it and export the new csv. All you have to do is put some error handling in to make sure the new registry item is applied, update the available column with the coreip? and export it again.

ok thank you, I’ll try that