Writing properties of an object to file

Welcome Forums General PowerShell Q&A Writing properties of an object to file

This topic contains 9 replies, has 4 voices, and was last updated by

 
Participant
2 years, 5 months ago.

  • Author
    Posts
  • #51876

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 21
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 21
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

  • #51902

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 21
    Rank: Member

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

  • #51968

    Participant
    Points: 502
    Helping Hand
    Rank: Major Contributor

    Add-KillList -Name ertuu85

    🙂

  • #51994

    Participant
    Points: 21
    Rank: Member

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

The topic ‘Writing properties of an object to file’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort