none
Turning an object property into a string RRS feed

  • Question

  • Hi

    I'm retrieving a list of servers from AD

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry
    $objSearcher.Filter = ("OperatingSystem=Window*Server*")
    $objSearcher.PropertiesToLoad.Add("Name") | Out-Null
    $colResults = $objSearcher.FindAll()

    I'd like to put $objResult.Properties.name into a string value but can't work out how.

    Help Please.

    Alex


    Friday, February 20, 2015 3:27 PM

Answers

  • Here is how you will need to do this to get what you want.

    $searcher=[adsisearcher]'OperatingSystem=Window*Server*'
    $searcher.FindAll() |
       ForEach-Object{
          Write-Host "ServerName is $($_.Properties['name'][0])" -fore green
       }
    
    


    ¯\_(ツ)_/¯

    • Marked as answer by Mr P Monday, February 23, 2015 3:18 PM
    Monday, February 23, 2015 1:58 PM

All replies

  • Hi Alex,

    Here's one method:

    $str = ($colResults | ForEach { $_.Properties['Name'] }) -join ','
    
    $str



    EDIT: Also, you could do something like this:

    [string]$computerNames = Get-ADComputer -Filter "OperatingSystem -like '*Window*Server*'" | 
        Select -ExpandProperty Name
    
    $computerNames


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Friday, February 20, 2015 3:43 PM
  • Hi Mike

    I'm already trying something similar

    foreach ($objResult in $colResults) {
    $server = $objResult.Properties.name

    }

    But the $server is still an object. The thing is I'm using each server name in an invoke-sqlcmd command, so I need to loop through them one at a time.

    Cheers

    Alex



    Friday, February 20, 2015 4:00 PM
  • Here's an adjustment:

    foreach ($objResult in $colResults) {
    
        [string]$server = $objResult.Properties.name
        $server
        $server.GetType().Name
    
    }


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Friday, February 20, 2015 4:05 PM
  • Sorry I've got it wrong!!!???!!

    The Line I'm having trouble with is 

    invoke-sqlcmd  -query "select @@servername, name, compatibility_level, collation_name, recovery_model_desc From sys.databases where name not in ('master', 'tempdb', 'model', 'msdb') Order by Name" -serverinstance $server

    If I manually define $server eg $server = "server1" it works fine.

    If I use

    foreach ($objResult in $colResults) {
    $server = $objResult.Properties.name

    Invoke-SQL.............

    }

    I get an error

    invoke-sqlcmd : Value cannot be null.
    Parameter name: ServerInstance

    Any help???

    Friday, February 20, 2015 4:25 PM
  • Easy way is:

    $server = $objResult.Properties.Item('name').Value

    This will fully unwrap the property.

    Here is a script that demonstrates how to unwrap properties of AD objects:

    https://gallery.technet.microsoft.com/Extract-arbitrary-list-of-6f59d3b4


    ¯\_(ツ)_/¯

    • Proposed as answer by Mike Laughlin Friday, February 20, 2015 6:03 PM
    Friday, February 20, 2015 5:47 PM
  • I find I must use $Object.Properties.Item("attribute_name").Value. Otherwise the attribute names must be in all lower case, which is annoying.

    Richard Mueller - MVP Directory Services

    Friday, February 20, 2015 8:42 PM
    Moderator
  • The disconnect seems to be that X.500 is specified as "case sensitive' and Microsoft is "case preserving" but MS has to honor the X.500 spec at certain levels.  There is a no-man's land in between.

    ¯\_(ツ)_/¯

    Friday, February 20, 2015 8:46 PM
  • Hi

    I'm not sure whats going wrong here but if I use 

    $server = $objResult.Properties.item('name').value

    it returns a blank value with either single or double quotes.

    If I remove the .value it returns the server name but the invoke-sqlcmd still fails.

    What am I getting wrong?

    Monday, February 23, 2015 11:55 AM
  • Here is how you will need to do this to get what you want.

    $searcher=[adsisearcher]'OperatingSystem=Window*Server*'
    $searcher.FindAll() |
       ForEach-Object{
          Write-Host "ServerName is $($_.Properties['name'][0])" -fore green
       }
    
    


    ¯\_(ツ)_/¯

    • Marked as answer by Mr P Monday, February 23, 2015 3:18 PM
    Monday, February 23, 2015 1:58 PM
  • That's brilliant.

    Thanks jrv


    Monday, February 23, 2015 3:18 PM