locked
Generating Message on Empty Input in CSV RRS feed

  • Question

  • Hi Folks,

    I am writing a PS script that imports values from a Csv file. I need a way to generate a message if the Input value is not right or is empty.

    Can you please suggest what code should I write for that -

    The testfile.csv has 3 headings namely name, description, title

    Here's an example code - 

    $Input = Import-Csv "C:\testfile.csv"

    ForEach ($Item in $Input)

    $Name = $Item.name

    $Desc = $Item.description

    $Title = $Item.title

    if($Title -contains "" -or $Title -isnot "Director" -or $Title -isnot "VP")
    {
        Write-Host "User is a Normal Employee"
    }

    So on and so forth. The exception that gets generated says something like -

    "Cannot convert the "Agent   " value of type "System.String" to type "System.Type""

    I gave it an input with spaces at the end in the title column of the Csv file.

    Please advise.


    Thursday, November 15, 2018 10:38 AM

Answers

  • So if I add more values to 'Director|VP' it would work right ?

    Yes.  The pipe is OR in regular expression patterns.


    \_(ツ)_/

    • Marked as answer by MoNeY2TechNet Sunday, November 18, 2018 8:44 AM
    Friday, November 16, 2018 1:06 PM

All replies

  • Hi Folks,

    I am writing a PS script that imports values from a Csv file. I need a way to generate a message if the Input value is not right or is empty.

    Can you please suggest what code should I write for that -

    The testfile.csv has 3 headings namely name, description, title

    Here's an example code - 

    $Input = Import-Csv "C:\testfile.csv"

    ForEach ($Item in $Input)

    $Name = $Item.name

    $Desc = $Item.description

    $Title = $Item.title

    if($Title -contains "" -or $Title -isnot "Director" -or $Title -isnot "VP")
    {
        Write-Host "User is a Normal Employee"
    }

    So on and so forth. The exception that gets generated says something like -

    "Cannot convert the "Agent   " value of type "System.String" to type "System.Type""

    I gave it an input with spaces at the end in the title column of the Csv file.

    Please advise.

    • Merged by jrv Thursday, November 15, 2018 10:47 AM DUPLICATE
    Thursday, November 15, 2018 10:40 AM
  • Please do not post the same question in multiple forums.  It will not get you a better answer.


    \_(ツ)_/

    Thursday, November 15, 2018 10:44 AM
  • Please edit your post and use the code posting tool to post correct formatted code.   The way you have posted is unreadable as code.


    \_(ツ)_/

    Thursday, November 15, 2018 10:49 AM
  • "-isnot" is not a test for strings it is a Boolean operator.  You need to use like, match or equals operators (-like,-match-eq ) to test strings.

    $string -eq 'some string'
    $string -ne 'some string'

    help about_operators


    \_(ツ)_/

    Thursday, November 15, 2018 10:52 AM
  • Hello,

    So I removed the -isnot and used -ne and it sort of works. But here's the problem now.

    Name, Description, Title

    John,Father,Director

    Jack,Son,Agent

    When I do this

    if($Title -contains "" -or $Title -ne 'Director' -or $Title -ne 'VP')

    {
        Write-Host "User is a Normal Employee"
    }

    It writes the Output User is a Normal Employee 2 times and does the else too (declared later) since it checks the title in a loop for 2 times.

    How do we correct this ?

    Friday, November 16, 2018 7:44 AM
  • "-I" and "-contains are only for use with collections.  You need to use "-like" or "-match" with strings.

    Please take the time to learn basic programming.. Your issue are all due to lack of understanding basic programming.  Also please carefully read the help for operators.

    YOU cannot learn PowerShell from forums or by asking questions.  Please do a tutorial or a book on PowerShell.  We cannot give you custom personal training in PowerShell.  That is beyond the scope of this forum.


    \_(ツ)_/

    Friday, November 16, 2018 9:09 AM
  • I appreciate that you feel my issues stem from not knowing basics of programming. Thanks for letting me know.

    Do one thing, Delete this forum post if you think I'm looking for Training on basics of PowerShell. I will ask my doubts on other forums.

    While you're at it. Do remember that there's a way to offer suggestion and not blatantly point out lack of knowledge as the cause of issue. Even the brightest minds need to be directed to the right path.

    Thanks for your time.

    Friday, November 16, 2018 12:01 PM
  • There's several ways to go about this...but based on your snippet a quick fix would be:

    $Input = Import-Csv "C:\testfile.csv"
    ForEach ($Item in $Input)
    {
        $Name = $Item.name
        $Desc = $Item.description
        $Title = $Item.title
    
        if (-not($title) -and ($title -ne 'Director') -and ($Title -ne "VP"))
        {
            Write-Host "User is a Normal Employee"
        }
        else
        {
            Write-Host "User is a $title"
        }
    }
    

    But as Jrv pointed out, please use the code formatting tool when posting to keep code readable.
    Friday, November 16, 2018 12:06 PM
  • Thanks,

    Can you tell me what (-not($title) does ? To my knowledge I think it checks if it is not empty I guess ?

    Friday, November 16, 2018 12:15 PM
  • That is correct.
    IF you run that, it will output either $true or $false.
    Friday, November 16, 2018 12:17 PM
  • This line will always be false when $title is null.

    if (-not($title) -and ($title -ne 'Director') -and ($Title -ne "VP")){

    This is the easier way to do this:

    if ($title -and $title -notmatch 'Director|VP'){

    Even easier is this:

    if($title -notmatch '^(?![\s\S])|Director|VP'){The pattern matches both null and empty strings.


    \_(ツ)_/

    Friday, November 16, 2018 12:34 PM
  • I appreciate that you feel my issues stem from not knowing basics of programming. Thanks for letting me know.

    Do one thing, Delete this forum post if you think I'm looking for Training on basics of PowerShell. I will ask my doubts on other forums.

    While you're at it. Do remember that there's a way to offer suggestion and not blatantly point out lack of knowledge as the cause of issue. Even the brightest minds need to be directed to the right path.

    Thanks for your time.

    We need to tell the truth when advising others.  If they cannot tolerate knowing what they need to do then they should not work in technology.  You need to get a book or take a tutorial to learn how to learn PowerShell.  You cannot learn this from forums or by guessing beyond simple one line commands.  PowerShell scripting IS programming and that is a requirement for learning PowerShell.  A book/tutorial would teach you basic programming along with PowerShell.

    There is no need to delete tis.  I a not complaining about your post.  I am just trying to advise you as to how to learn PowerShell.

    If you want to fly a plane you must first pass ground school.  If you want to play a musical instrument you must first learn the scale, chords and keys. The same is true of PowerShell.

    Here is a free tutorial that will help you get started correctly.

    1. Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    \_(ツ)_/


    • Edited by jrv Friday, November 16, 2018 12:42 PM
    Friday, November 16, 2018 12:41 PM
  • edit: jrv's code is correct. My example was based on using a null value, which wasn't really null.

    $titles = 'vp','director','janitor','coffeecreamerstealer'
    
    foreach ($title in $titles){
        if ($title -and $title -notmatch 'Director|VP|Janitor')
        { 
            "we are a: [$title]"
        } 
        else
        {
            "we have a title: [$title]" 
        }
    }

    outputs:

    we have a title: [vp]
    we have a title: [director]
    we have a title: [janitor]
    we are a: [coffeecreamerstealer]


    Friday, November 16, 2018 12:57 PM
  • Sometimes you've got to run before you can walk.
    Friday, November 16, 2018 1:00 PM
  • So if I add more values to 'Director|VP' it would work right ?
    Friday, November 16, 2018 1:02 PM
  • So if I add more values to 'Director|VP' it would work right ?

    Yes.  The pipe is OR in regular expression patterns.


    \_(ツ)_/

    • Marked as answer by MoNeY2TechNet Sunday, November 18, 2018 8:44 AM
    Friday, November 16, 2018 1:06 PM