Weird question

Welcome Forums General PowerShell Q&A Weird question

This topic contains 6 replies, has 5 voices, and was last updated by

1 year, 7 months ago.

  • Author
  • #82723

    Topics: 73
    Replies: 165
    Points: 65
    Rank: Member

    I have the code below where it grabs info from a CSV file. The problem is that, not everyone have a title, or a desk phone, or a cell phone. I'm able to write if title is NULL, don't read at line. This is working.

    I was wondering if there's a way in PowerShell to tell it if any of these (Name, Title, Street, etc) is NULL, don't read that line?

    I hope I'm being clear on what I'm asking.

      (Get-Content $html_files_location\$txt_file_names) `
         -creplace ("NAME",$employees.Name) `
          -creplace ("TITLE",$employees.Title) `
           -creplace ("STREET", $employees.Street) `
            -creplace ("ADDRESS_INFO", $employees.Address_info) `
             -creplace ("PHONE", $employees.Phone) `
              -creplace ("CELL", $employees.Cell) `
               -creplace ("WEBSITE", $web) `
                -creplace ("ADURL", $web.Trim("http://")) `
                 -creplace ("LOGO", $logo) ` |
        Set-Content $html_files_location\$txt_file_names -Force
  • #82727

    Topics: 1
    Replies: 23
    Points: 22
    Rank: Member

    Why aren't you using Import-Csv cmdlet?

    • #82730

      Topics: 73
      Replies: 165
      Points: 65
      Rank: Member

      I am using that command. The above code is just a portion of what I have. I didn't copy the whole thing.

  • #82742

    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    If you're using Import-CSV, then each row is an object. Assuming you're enumerating the rows and using a variable like $row (since you didn't post that part, I can only provide a basic example) I'd probably do something like...

    if ($ -and $ -and $row.title) {
    # all three fields contain something

  • #82744

    Topics: 1
    Replies: 284
    Points: 41
    Rank: Member

    Just be careful whether the value is NULL or just an empty string, they're not the same thing. You should pipe your row to Get-Member to check.

    As for skipping rows that contain an empty cell, this should work:

    $csv = Import-Csv test.csv
    foreach ($row in $csv) {
        if (-not($row.PSObject.Properties.Value -contains '')) {
            Write-Output $row
        } #end if
    } #end foreach
  • #82748

    Topics: 73
    Replies: 165
    Points: 65
    Rank: Member

    Thank you

  • #82753

    Topics: 18
    Replies: 244
    Points: 109
    Rank: Participant

    This should also work

    $stripedcsv = Import-Csv C:\names\names.csv | ? { ($_.PSObject.Properties | % {$_.Value}) -notcontains "}

The topic ‘Weird question’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort