Problems with "if" statement.

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

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

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

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?

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

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

I was also concerned about the ampersand char in your path

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