Writing properties of an object to file

This topic contains 9 replies, has 4 voices, and was last updated by Profile photo of Dan Potter Dan Potter 3 months, 1 week ago.

  • Author
    Posts
  • #51876
    Profile photo of ertuu85
    ertuu85
    Participant

    How can I pull, say, the name property to file from 'search-adaccount' as it appears on the object?

    Instead of writing 'Aaron' it will write out like cn='Aaron',cn='users',cn='whatever..'

    I tried just doing a split/replace but it yields the same results...

    Add-Content out.txt (Search-ADAccount -LockedOut) | % {$($_.name.split(",")[0] -replace "cn=","")}
    
  • #51882
    Profile photo of ertuu85
    ertuu85
    Participant

    I'm trying to add 2 properties to a listbox using:

    function Load-ListBox 
    	{
    	
    		Param (
    			[ValidateNotNull()]
    			[Parameter(Mandatory=$true)]
    			[System.Windows.Forms.ListBox]$ListBox,
    			[ValidateNotNull()]
    			[Parameter(Mandatory=$true)]
    			$Items,
    		    [Parameter(Mandatory=$false)]
    			[string]$DisplayMember,
    			[switch]$Append
    		)
    		
    		if(-not $Append)
    		{
    			$listBox.Items.Clear()	
    		}
    		
    		if($Items -is [System.Windows.Forms.ListBox+ObjectCollection] -or $Items -is [System.Collections.ICollection])
    		{
    			$listBox.Items.AddRange($Items)
    		}
    		elseif ($Items -is [System.Collections.IEnumerable])
    		{
    			$listBox.BeginUpdate()
    			foreach($obj in $Items)
    			{
    				$listBox.Items.Add($obj)
    			}
    			$listBox.EndUpdate()
    		}
    		else
    		{
    			$listBox.Items.Add($Items)	
    		}
    	
    		$listBox.DisplayMember = $DisplayMember	
    	}
    
    
    $xyz = (Search-ADAccount -LockedOut) | % {"$($_.name), $($_.samaccountname)"}
    Load-ListBox $LockedList $xyz
    

    its loading the objects in the list box as

    [ ] cn=aaron, cn=users, cn=amunson, cn=users

    when i want it to look like..

    [ ] Aaron, amunson

  • #51884
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Use the name? The distinguishename is cn=.. why are you trying to split the name property for characters it doesn't include?

    (Search-ADAccount -LockedOut | select -First 5).name |tee lockedout.txt

  • #51886
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    Why not do something like this:

    Search-ADAccount -LockedOut -SearchBase "OU=xxx,OU=xx,OU=xxx,OU=xxx,DC=xxx,DC=com" | select name | Out-File C:\Test\ADlockout.txt
    
  • #51888
    Profile photo of Dan Potter
    Dan Potter
    Participant

    You're going to have a really difficult time building gui's before learning powershell. Practice getting the results you want in the shell first.

    $xyz = Search-ADAccount -LockedOut | select -First 5 | % { $_.name + ' —– ' + $_.samaccountname }
    Load-ListBox $listbox1 $xyz

  • #51893
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    I agree with Dan. Learn and understand PowerShell first before moving on.

  • #51902
    Profile photo of ertuu85
    ertuu85
    Participant

    Well looks like I did have it all along...

    		$xyz = (Search-ADAccount -LockedOut) | % {"$($_.name), $($_.samaccountname)"}
    		Load-ListBox $LockedList $xyz
    

    This worked, but only after closing/reopening my PS window

  • #51904
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Note: there are some people that will put you on their kill list for using % in script:D

  • #51968
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Add-KillList -Name ertuu85

    🙂

  • #51994
    Profile photo of Dan Potter
    Dan Potter
    Participant

    😀 using it twice moves you to the top of the list.

You must be logged in to reply to this topic.