locked
Importing CSV Returns Error RRS feed

  • Question

  • $Path = 'C:\users\user\adusers.csv'
    
    $users = Import-CSV $Path
    
    foreach ($user in $users) {
    $Sam = Get-ADUser $user | fl samaccountname
    $RPP = Get-ADUserResultantPasswordPolicy $sam
    if ($RPP -ne $null)
    {
    write-host "$user has a PSO"
    }
    else {
    write-host "$user doesn't have a PSO"
    }
    }

    I am having issues importing this CSV. If I manually build the array with users, it works fine. If I import the CSV I get an error as seen below:

    Get-ADUserResultantPasswordPolicy : Cannot bind parameter 'Identity". Cannot convert value "@{samaccountname=user}" to type "Microsoft.ActiveDirectory.Management.ADUser".

    The objective of the script is to import users stored like in individual rows, determine if they have any return when Get-ADUserResultantPasswordPolicy runs with their username (auser) and then to eventually put it into a new csv for viewing.

    Like I said, if I create my own array #$users = $("test", "user", "bill", "john") it will work. It only breaks on import of the CSV.

    Thursday, February 2, 2017 10:06 PM

Answers

  • Get-AdUser -filter * -SearchBase $OU | 
    	%{
    		if(Get-ADUserResultantPasswordPolicy){
    			write-host "$($_.Name) has a PSO"
        	        }else{
    		        Out-File "$($_.Name) doesn't have a PSO" -append
    	       }
    	}


    \_(ツ)_/


    • Edited by jrv Friday, February 3, 2017 5:15 PM
    • Marked as answer by ydde Friday, February 3, 2017 7:42 PM
    Friday, February 3, 2017 5:14 PM
  • $user | Out-File <filepath> -Append


    \_(ツ)_/

    • Marked as answer by ydde Friday, February 3, 2017 9:50 PM
    Friday, February 3, 2017 8:37 PM

All replies

  • $Path = 'C:\users\user\adusers.csv'
    $users = Import-CSV $Path
    foreach ($user in $users) {
        $Sam = Get-ADUser $user | Select-Object -ExpandProperty samaccountname
        $RPP = Get-ADUserResultantPasswordPolicy $sam
        if ($RPP -ne $null){
            write-host "$user has a PSO"
        }
        else {
            write-host "$user doesn't have a PSO"
        }
    }
    Try this .... what's in your csv? Looks like it's already the samaccountname.

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, February 2, 2017 10:19 PM
  • When it pulls from the CSV it reads it as @{SamAccountName=uname} not as just uname.

    The error reads:

    Get-ADUser : Cannot bind parameter 'Identity'. Cannot convert value "@{SamAccountName=uname}" to type "Microsoft.ActiveDirectory.Management.ADUser". Error: "Cannot convert the "@{SamAccountName=uname}" value of type 
    "System.Management.Automation.PSCustomObject" to type "Microsoft.ActiveDirectory.Management.ADUser"."
    At C:\Users\user\test - Copy.ps1:4 char:23
    +     $Sam = Get-ADUser $user | Select-Object -ExpandProperty samaccountname
    +                       ~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    The CSV looks like 

    "auser"

    "buser"

    "cuser"

    "duser"

    Thursday, February 2, 2017 10:55 PM
  • Not a real CSV:

    $Path = 'C:\users\user\adusers.csv'
    $users = Import-CSV $Path -Header SamAccountName
    foreach ($user in $users) {
    	if(Get-ADUserResultantPasswordPolicy $user.SamAccountName){
    		write-host "$user has a PSO"
    	} else {
    		write-host "$user doesn't have a PSO"
    	}
    }


    \_(ツ)_/

    Thursday, February 2, 2017 11:29 PM
  • You can also just do this:

    Get-AdUser -filter * -SearchBase $OU | 
    	%{
    		if(Get-ADUserResultantPasswordPolicy){
    			write-host "$($_.Name) has a PSO"
        	}else{
    			write-host "$($_.Name) doesn't have a PSO"
    		}
    	}
    


    \_(ツ)_/

    • Proposed as answer by Richard MuellerMVP Friday, February 3, 2017 3:10 PM
    • Unproposed as answer by ydde Friday, February 3, 2017 5:39 PM
    Thursday, February 2, 2017 11:35 PM
  • This works exactly how I need it. Thanks! How do I export the "else" into a CSV or push it there? The final part of this is to have a CSV with each user without a PSO.


    Thank you so much for your help!

    Friday, February 3, 2017 1:47 PM
  • $_.Name | Out-File <filename> -append

    \_(ツ)_/

    Friday, February 3, 2017 4:16 PM
  • When I use that, it creates the file but it's empty with no data.

    $Path = 'C:\users\user\adusers.csv'
    $users = Import-CSV $Path -Header SamAccountName
    foreach ($user in $users) {
    	if(Get-ADUserResultantPasswordPolicy $user.SamAccountName){
    		write-host "$user has a PSO"
    	} else {
    		write-host "$user doesn't have a PSO" 
            $_.user | Out-File 'C:\users\user\adusers2.csv' -append
    	}
    }

    Friday, February 3, 2017 4:42 PM
  • Get-AdUser -filter * -SearchBase $OU | 
    	%{
    		if(Get-ADUserResultantPasswordPolicy){
    			write-host "$($_.Name) has a PSO"
        	        }else{
    		        Out-File "$($_.Name) doesn't have a PSO" -append
    	       }
    	}


    \_(ツ)_/


    • Edited by jrv Friday, February 3, 2017 5:15 PM
    • Marked as answer by ydde Friday, February 3, 2017 7:42 PM
    Friday, February 3, 2017 5:14 PM
  • I was using the:

    $Path = 'C:\users\user\adusers.csv'
    $users = Import-CSV $Path -Header SamAccountName
    foreach ($user in $users) {
    	if(Get-ADUserResultantPasswordPolicy $user.SamAccountName){
    		write-host "$user has a PSO"
    	} else {
            $_.user | Out-File 'C:\users\user\adusers2.csv' -append
    	}
    }

    As it is functioning the best out of what you gave me. The Get-ADUser -filter kept asking for me to input a parameter for each entry. I just need the above script to output to a csv. 

    This outputs to a blank file still.

    • Marked as answer by ydde Friday, February 3, 2017 7:41 PM
    • Unmarked as answer by ydde Friday, February 3, 2017 7:42 PM
    Friday, February 3, 2017 5:54 PM
  • I suggest learning PowerShell instead of asking others to write scripts for you.  Any PowerShell user would know how to fix that.

    Get-AdUser -Filter *

    You are just asking for incremental write.  The assumption and rules of the forum clearly state that it is not for writing custom scripts.


    \_(ツ)_/

    Friday, February 3, 2017 6:21 PM
  • I was able to fix the issue. I do apologize if I was abusing the forum. It wasn't my intention. I am new to PS and I'm trying to piece it together as I go.

    Thanks again for your help!

    Friday, February 3, 2017 7:41 PM
  • I was able to fix the issue. I do apologize if I was abusing the forum. It wasn't my intention. I am new to PS and I'm trying to piece it together as I go.

    Thanks again for your help!

    I am not saying that you are abusing the forum.  I am just trying to provoke you into thinking.  When asking questions we nee to be collaborative.  You cannot abandon your question or you will make it harder for others to assist you without running on for days.

    I knew a little pushback would make you look and see.

    Good luck.


    \_(ツ)_/

    Friday, February 3, 2017 7:53 PM
  • Thanks :)

    I'm still having an issue with:

    $Path = 'C:\users\user\adusers.csv'
    $users = Import-CSV $Path -Header SamAccountName
    foreach ($user in $users) {
    	if(Get-ADUserResultantPasswordPolicy $user.SamAccountName){
    		#write-host "$user has a PSO"
    	} else {
    		write-host "$user"
            
    	}
    }
    

    If I use this, it works and outputs it to console, but no matter if I export-csv, tee, or >, it won't output any data to a file. I will continue to play with it and if I continue to struggle, I may ping back next week.

    Thanks again for your help.

    Friday, February 3, 2017 8:20 PM
  • $user | Out-File <filepath> -Append


    \_(ツ)_/

    • Marked as answer by ydde Friday, February 3, 2017 9:50 PM
    Friday, February 3, 2017 8:37 PM
  • Thanks! I wasn't expecting a reply back. I figured it out and came back to let you know.

    Have a great weekend!

    Friday, February 3, 2017 9:51 PM