locked
Import empty values from csv RRS feed

  • Question

  • Hi!

    My problem is, that if the second variable ($initials,$givenname etc..) is null (these are coming from a csv and there is no value for that column) the If statement return TRUE.

    So:

    $Attribs.initials empty

    $initials null

    For me it is equal(should be), but IF condition says it not equal.

    The script:

    $Attribs = Get-ADUser -Identity $Name -Properties "initials","sn","givenname","displayname","company","department","title","employeenumber","description"
    
    if (($Attribs.initials -ne $initials) -or
    ($Attribs.sn -ne $sn) -or
    ($Attribs.givenname -ne $GivenName) -or
    ($Attribs.displayname -ne $DisplayName) -or
    ($Attribs.company -ne $Company) -or
    ($Attribs.department -ne $Department) -or
    ($Attribs.title -ne $Title) -or
    ($Attribs.employeenumber -ne $EmployeeNumber) -or
    ($Attribs.description -ne $Description))
    {
    
    }
    Please help




    • Edited by SOEMS Wednesday, January 13, 2016 5:41 PM
    Wednesday, January 13, 2016 2:45 PM

Answers

  • You cannot use empty values in AD.

    A string set to "" is a null.

    [string]$null
    [string]""

    both are identical.

    In some commands you can use a null to clear a value.  in all  cases where you are using a null you can use an empty string.  MOst AD Cmdlets will not allow nulls.  To clear a value you must use Clear.

    To set a value we use -Replace:

    Set-AdUser jsmith -replace @{st=$state;l=$city;sn=$surname}

    To clear avalue we use:
    Set-AdUser jsmith -Clear st,sn,l

    You cannot use null or an empty string to do this.

    If we use the less flexible method then we can use a null but then we cannot use a hash.


    \_(ツ)_/

    • Marked as answer by SOEMS Friday, January 15, 2016 6:11 AM
    Thursday, January 14, 2016 6:43 AM

All replies

  • $null is a built in variable. So in any field you want you can specify -AND $Value -NE $Null
    Wednesday, January 13, 2016 2:52 PM
  • Hi TPC!

    I modified the Topic name, because i determined that the problem will be importing empty value from csv.

    My CSV is in this format: ;Value;Value;Value .

    The CSV doesn't contain double quotes.

    If one of the AD User attribute hasn't got value is this empty or $null?

    If I import the csv, which type of variable will be create if the column empty? $null or empty?

    I think that the if condition ($Attribs.initials -ne $initials) bad because of the variable value type mismatch.

    Isn't it?

    Thanks,

    Tamas Kiss 


    • Edited by SOEMS Wednesday, January 13, 2016 5:49 PM
    Wednesday, January 13, 2016 5:48 PM
  • Null and empty are the same thing in PowerShell.

    \_(ツ)_/

    Wednesday, January 13, 2016 5:52 PM
  • This filters out all of the null properties from a csv.

    $csv|ForEach-Object{
         $_.psobject.Properties|?{$_.Value}
    }


    \_(ツ)_/


    • Edited by jrv Wednesday, January 13, 2016 5:57 PM
    Wednesday, January 13, 2016 5:56 PM
  • Thanks JRV, you would be right. Had an issue where empty and Null are not the same in a DB setting but that doesn't apply to the question being asked.
    Wednesday, January 13, 2016 5:57 PM
  • This is all but one simple step.

    $csv|ForEach-Object{
            $hash=@{}
    	$_.psobject.Properties|?{$_.Value}|%{$hash.Add($_.Name,$_.Value)}
    $username=$hash['username'] $hash.Remove('username') Set-AdUser $username -Replace $hash }


    \_(ツ)_/


    • Proposed as answer by jrv Wednesday, January 13, 2016 6:01 PM
    • Edited by jrv Wednesday, January 13, 2016 6:04 PM
    Wednesday, January 13, 2016 6:01 PM
  • In a database null and empty are not the same.


    \_(ツ)_/

    Wednesday, January 13, 2016 6:05 PM
  • Hi!

    The value that I imported from CSV (if it is empty) : ""

    The not defined AD attribute value is $null

    I had to set all empty imported value to $null

    $Properties= $dolgozo | get-member -type NoteProperty # Collect all property
        ForEach ($prop in $Properties) #go step by step on all property
        {
        $a=$prop.Name #Save the name of property
        if ($dolgozo.$a -eq "")  #If imported property value ""
            {
            $dolgozo.$a=$null #then set it to $null
            
            
            }
        }

    After it i can compare the two variable value

    if ($Attribs.initials -ne $initials)



    • Edited by SOEMS Wednesday, January 13, 2016 9:30 PM
    Wednesday, January 13, 2016 9:28 PM
  • Last time. Null and empty are identical in PowerShell

    My script filters out all null/empty values.

    if($null){$true}else{$false}
    if(""){$true}else{$false}

    Test them for your self.

    You cannot assign emoty or null values to AD attributes.

    The following adds all non-empty properties to a hash

    $_.psobject.Properties|?{$_.Value}|%{$hash.Add($_.Name,$_.Value)}

    This is not something I invented.  This is how we have been doing it for years.


    \_(ツ)_/


    • Edited by jrv Wednesday, January 13, 2016 9:51 PM
    Wednesday, January 13, 2016 9:51 PM
  • Hi jrv!

    I see what You said, and Thank You for Your answers!

    If im right, Your script import only properties and their value if value exists.  I have to import the empty values as well.

    I think AD property contain $null value, or I misunderstand something.

    If I assign $null to an ADUser Attribute, it clears the current value.

    In this forum I found a verification for my thoughts:

    http://forums.asp.net/t/1311457.aspx?Updating+null+values+in+Active+Directory

    "You're allowed to set an AD attribute to null (which clears it) but not to an empty string."

    and:

    if ("" -eq $null){Write-host "Equal"}else{Write-host "Not Equal"}
    Not Equal

    Sorry if It all went over my head:) Please be patient.

    In this reply

    Thursday, January 14, 2016 6:25 AM
  • You cannot use empty values in AD.

    A string set to "" is a null.

    [string]$null
    [string]""

    both are identical.

    In some commands you can use a null to clear a value.  in all  cases where you are using a null you can use an empty string.  MOst AD Cmdlets will not allow nulls.  To clear a value you must use Clear.

    To set a value we use -Replace:

    Set-AdUser jsmith -replace @{st=$state;l=$city;sn=$surname}

    To clear avalue we use:
    Set-AdUser jsmith -Clear st,sn,l

    You cannot use null or an empty string to do this.

    If we use the less flexible method then we can use a null but then we cannot use a hash.


    \_(ツ)_/

    • Marked as answer by SOEMS Friday, January 15, 2016 6:11 AM
    Thursday, January 14, 2016 6:43 AM
  • Hi!

    OK!

    To clear AD Attrib value i will use -clear from now.

    Thank you for your help.

    Friday, January 15, 2016 6:10 AM
  • Basically I make two objects at the same time. A clear list (array) and a hash of updates.  THe just add them to the same Set-AdUser.  Once the code is set you can change the CSV as needed and it will continuw to work.

    The only catch is when a system send empty value because it wants themleft unchanged.  This would require a slight alteration or flag to handle.


    \_(ツ)_/

    Friday, January 15, 2016 11:52 AM