Problems with "if" statement.

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Jay Jones Jay Jones 2 years, 3 months ago.

  • Author
    Posts
  • #18035
    Profile photo of Jay Jones
    Jay Jones
    Participant

    I cant get my if statement to work within my function. What i want it to do is check the content of a variable, the variable is content from a .csv file. I want it to check whether there is any content under the 'username' column. If there is then give one output, else another.

    Function Check-UserFile {
        [CmdletBinding()]
        Param(
    
             [Parameter()][String]$Path = "C:\Holding\Scripts\Starter&LeaverScripts\NewADUser.csv"
             
             )
    
            import-csv -path $Path
            $TempContent = Get-Content $Path
            if ($TempContent | Where-Object {$_.Username -eq ""})
                        {
            Write-Host -BackgroundColor Green -ForegroundColor Black "No Users Present"
                        }
            else
            {
            Write-Host -BackgroundColor red -ForegroundColor Black "Users are present in file please run Clear-UserFile cmdlet"
            }    
                          }

    I am using -eq "" to try and validate whether there is no conten tor not but it doesnt seem to work. No matter what it always outputs the else statement. Any help greatly appreciated.

    Kind regards,

    Jay

  • #18036
    Profile photo of Raymond Slieff
    Raymond Slieff
    Participant

    I think your problem might be that you are getting the content in that variable as Get-Content instead of the Import-CSV that you call above it, I am guessing you do that just to show it, but you could do it for both.

    When you use Get-Content, it's using each line as an object without properties, where as Import-CSV is creating objects that you can manipulate better.

    From the help file of each of these:

    Get-Content
    The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a
    file. It reads the content one line at a time and returns a collection of objects , each of which represents a line
    of content.

    Import-Csv
    The Import-Csv cmdlet creates table-like custom objects from the items in CSV files. Each column in the CSV file
    becomes a property of the custom object and the items in rows become the property values. Import-Csv works on any
    CSV file, including files that are generated by the Export-Csv cmdlet.

    So I think if you changed it to this, you would get what you want.

    Function Check-UserFile {
        [CmdletBinding()]
        Param(
     
             [Parameter()][String]$Path = "C:\Holding\Scripts\Starter&LeaverScripts\NewADUser.csv"
     
             )
     
            $TempContent = import-csv -path $Path
            $TempContent
            if ($TempContent | Where-Object {$_.Username -eq ""})
                        {
            Write-Host -BackgroundColor Green -ForegroundColor Black "No Users Present"
                        }
            else
            {
            Write-Host -BackgroundColor red -ForegroundColor Black "Users are present in file please run Clear-UserFile cmdlet"
            }    
                          }
  • #18037
    Profile photo of Jay Jones
    Jay Jones
    Participant

    Hi Raymond,

    Thanks for your reply.

    That makes sense, i was previously trying to use clear-content and -filter to clear certain parts of the .csv but found another way to do it. That's why i was using get-content but i can see that it also displays it the same by using import-csv.

    Unfortunately i am still getting the 'else' output when i remove the user data from the file. Could it be something to do with the '$_.Username' value i am trying to get from the .csv? I don't fully understand how this works but i thought that it meant look at the Username column in the .csv and if its empty then write out the first output. I have attached the .csv file if you would like to take a look.

    Kind regards,

    Jay

  • #18039
    Profile photo of Raymond Slieff
    Raymond Slieff
    Participant

    Jay,

    I might not understand, exactly the logic you are doing, or exactly what you are looking for.
    From what I can gather, if you have any users in the file, you want the second block, and if no users are in the file, you want the first block correct?
    If so, then I think what you really want is

    $TempContent = import-csv -path $Path
    if ($TempContent.Count -eq 0)

    Otherwise it feels like you are looking to do a foreach process, and skip over lines in the CSV file that do not have a username entry.

    Does the above help or did I assume something incorrectly?

  • #18042
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    what if you try it like this?

    Function Check-UserFile
    {
    [CmdletBinding()]
    Param( 
            [Parameter()][String]$Path = "C:\Holding\Scripts\Starter&LeaverScripts\NewADUser.csv" 
            )
     
        # import-csv -path $Path
        $TempContent = import-csv -path $Path
        if ($TempContent.Username -eq "")
            {
            Write-Host -BackgroundColor Green -ForegroundColor Black "No Users Present"
            }
        else
            {
            Write-Host -BackgroundColor red -ForegroundColor Black "Users are present in file please run Clear-UserFile cmdlet"
            }
    }
     Check-UserFile C:\Holding\Scripts\Starter&LeaverScripts\NewADUser.csv

    -VERN

  • #18043
    Profile photo of Jay Jones
    Jay Jones
    Participant

    Hi Raymond,

    Great thanks that worked!

    Maybe its my logic, i cant get my head around how it can distinguish between the headings and the content in the .csv. I thought that a .count -eq 0 wouldn't work as the .csv is never intended to have nothing in, i thought it would 'count' the headings. I always wanted it to remain with the headings so that i can append information to them later on.

    Kind regards,

    Jay

  • #18044
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    I was also concerned about the ampersand char in your path

  • #18045
    Profile photo of Jay Jones
    Jay Jones
    Participant

    Thanks Vern, that method works too. It doesn't seem to mind the ampersand.

You must be logged in to reply to this topic.