none
Help! I'm Stuck! PowerShell RRS feed

  • Question

  • I am trying to figure out the syntax to determine if a value in a field of an imported csv equals either true or false.  so for example below.. Basically this would quality check to make sure that the field only contains the values true or false..if not break the script...  I'm sure the answer is right in front of me..thanks for any help


    Import-Csv c:\test.csv | ForEach-Object {
    $First = $_.FirstName
    $Passwordneverexpires = $_.Passwordneverexpires
    
    If (($_.FirstName -eq '') -or ($_.passwordneverexpires -ne 'True' -or $_.passwordneverexpires -ne 'false'))
        {
        Break
        }
    
    
    }

    Friday, January 9, 2015 1:08 PM

Answers

  • that worked brilliantly..

    thanks!

    Be careful with that.

    'True1' -notmatch "(true|false)"
    False


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Easy enough to fix though - 

    PS C:\powershell> 'True1' -notmatch "^(true|false)$"
    True


    • Edited by Braham20 Friday, January 9, 2015 2:33 PM
    • Marked as answer by escott0699 Friday, January 9, 2015 2:55 PM
    Friday, January 9, 2015 2:29 PM

All replies

  • Hi,

    You could use the following, which parses the "passwordneverexpires" column to a boolean. If it fails, you will see a red line. You could comment out the positives to only get the red lines.

    Import-Csv "D:\Data\test.csv" | ForEach-Object {
    
    if([bool]$_.Passwordneverexpires)
    {
    Write-Host "$($_.firstName)'s password expires is a boolean"
    }
    else
    {
    write-host "$($_.firstName)'s password expires is not a boolean" -ForegroundColor red
    }
    }


    Nico Martens
    SharePoint/Office365/Azure Consultant

    Friday, January 9, 2015 1:28 PM
  • Try this - 

    Import-Csv c:\test.csv | ForEach-Object {
    $First = $_.FirstName
    $Passwordneverexpires = $_.Passwordneverexpires
    
    If ($_.FirstName -eq '' -or $_.passwordneverexpires -notmatch "(true|false)")
        {
        write-host $_ -ForegroundColor Red
        }
        else {
    
        write-host $_ -ForegroundColor Green
    
    
    }
    }

    • Marked as answer by escott0699 Friday, January 9, 2015 1:48 PM
    • Unmarked as answer by escott0699 Friday, January 9, 2015 2:55 PM
    Friday, January 9, 2015 1:39 PM
  • If (($_.FirstName -eq '') -or (-not [bool]::TryParse($_.passwordneverexpires,[ref]$null) )


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, January 9, 2015 1:42 PM
    Moderator
  • that worked brilliantly..

    thanks!

    Friday, January 9, 2015 1:48 PM
  • that worked brilliantly..

    thanks!

    Be careful with that.

    'True1' -notmatch "(true|false)"
    False


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, January 9, 2015 1:50 PM
    Moderator
  • that worked brilliantly..

    thanks!

    Be careful with that.

    'True1' -notmatch "(true|false)"
    False


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Easy enough to fix though - 

    PS C:\powershell> 'True1' -notmatch "^(true|false)$"
    True


    • Edited by Braham20 Friday, January 9, 2015 2:33 PM
    • Marked as answer by escott0699 Friday, January 9, 2015 2:55 PM
    Friday, January 9, 2015 2:29 PM
  • Thanks for the info! that seems to work well..

    just to clarify what do the ^ $ symbols do on the outside of the () to make this work?

    Friday, January 9, 2015 2:51 PM
  • It just defines the beginning and end of the string. No characters can come before or after true or false in order for it match. The brackets create a capture group and the pipe is used for OR. :)
    Friday, January 9, 2015 3:05 PM