Updating values in CSV

This topic contains 10 replies, has 2 voices, and was last updated by Profile photo of Tony Antony Tony Antony 3 months ago.

  • Author
    Posts
  • #53873
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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!"
    }
    
  • #53875
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

    "Yes".Trim() -eq "Y"

    • #53877
      Profile photo of Tony Antony
      Tony Antony
      Participant
      PS C:\Windows\system32> "Yes".Trim() -eq "Y"
      False
      

      False

  • #53896
    Profile photo of Dan Potter
    Dan Potter
    Participant

    now. "yes" -eq "Yes".Trim() -eq "n"

    • #53899
      Profile photo of Tony Antony
      Tony Antony
      Participant
      PS C:\Windows\system32> "yes" -eq "Yes".Trim() -eq "n"
      True
      
    • #53900
      Profile photo of Tony Antony
      Tony Antony
      Participant
      PS C:\Windows\system32> "yes" -eq "Yes".Trim() -eq "n"
      True
      
  • #53903
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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'
    	}
    	
    }
    
    
  • #53913
    Profile photo of Tony Antony
    Tony Antony
    Participant

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

  • #53915
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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
    
  • #53917
    Profile photo of Dan Potter
    Dan Potter
    Participant

    "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.

    • #53923
      Profile photo of Tony Antony
      Tony Antony
      Participant

      ok thank you, I'll try that

You must be logged in to reply to this topic.