Output just property from objects

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #6433

    by rambog at 2012-09-02 11:49:57

    For now, I am trying to get simply the domain name into a variable as follows:

    PS C:\Users\rambog> $dom=get-wmiobject -class Win32_ComputerSystem | Select D
    omain | Out-Host

    Domain
    ——
    mydomain-corp.local

    As you can see, it puts it with a "Domain" colum. I simply want $dom to equal mydomain-corp.local How can I re-do the script (preferably in one line) so it gets what I am looking for. For me this comes up quite a bit as I usually want just one of the properties of an object. If you can also recommend more reading on this subject, it would be further appreciated.

    by poshoholic at 2012-09-02 14:08:56

    You're on the right track. Instead of "Select Domain", try this approach:

    $dom = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Domain
    Here's another alternative:

    $dom = (Get-WmiObject -Class Win32_ComputerSystem).Domain

    by RichardSiddaway at 2012-09-03 04:21:30

    Hi rambog

    Don't be tempted to use the -property parameter on get-wmiobject for this as it just creates an object with a reduced property set.

    just to add to Kirk's answer you don't need out-host at the end of your pipeline. PowerShell will effectively do that for you.

    by rambog at 2012-09-03 09:53:47

    I got it to work but Kirk's way is much better. Here is what I was able to cobble together (not that I really understand what is going on),
    $Dom=get-wmiobject -class Win32_ComputerSystem -Computer $CName | Select -Property Domain| %{$_.Domain}
    Note: the $CName changes as this statement is nested in a foreach loop.
    Thank you Kirk and Richard. Richard, I am constantly hitting up against objects when I try to work with Powershell when I expect single values. Your point about the -property parameter still creating an object instead of a single value is something for me to keep in mind.

    by RichardSiddaway at 2012-09-03 10:20:54

    What you are doing is returning the WMI object for Win32_ComputerSystem; piping that to select to pick off the Domain property and then using foreach-object to get the value of the domain property. % is an alias of foreach-object and I would strongly recommend against using it in scripts

    You could get the same answer like this

    $dom = get-wmiobject -class Win32_ComputerSystem | %{$_.Domain}

    but the best way as Kirk stated is to do this

    $Dom=get-wmiobject -class Win32_ComputerSystem | Select -ExpandProperty Domain

    Its the approach I would recommend – its less typing and more obvious when you come back to the script in a few months

    by rambog at 2012-09-06 05:53:54

    I am now working on another script. I am having trouble understanding why the lessons you gave do not apply in the scenario below.
    Aside for the fact that I have quite a bit of work to do in order to gather the samAccountName of the user profiles that have been touched in the last 30 days on each computer account, I was simply trying to display the computer being queried along with the profile information. It does not like my line that defines the variable $CName. I thought I was working on the object, $Obj, and that the line beginning with $CName would extract the computer name from the object like the solution in my earlier program. $Obj is some form of DirectorySearcher object and it seems these rules don't help out. I have tried to transform it in other programs by doing something like $Computer=$Obj.Properties and then attempt to extract the name from $Computer- this time it doesn't seem to work. Any help would again be appreciated.

    $ObjFilter = "(objectClass=Computer)"
    $objSearch = New-Object System.DirectoryServices.DirectorySearcher
    $objSearch.PageSize = 20000
    $objSearch.Filter = $ObjFilter
    $objSearch.SearchRoot = "LDAP://OU=NLH,OU=Workstations,DC=labdomain,DC=org"
    $AllObj = $objSearch.FindAll()
    foreach ($Obj in $AllObj)
    {
    $keytype=[Microsoft.Win32.RegistryHive]::LocalMachine
    $remotekey=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($keytype,$CName)
    $regkey=$remotekey.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\ProfileList")
    #$profileList = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
    $CName=get-wmiobject -class Win32_ComputerSystem -Computer $CName | Select-Object -ExpandProperty Name
    Write-Host "Computer Name:`t$CName"
    #Get-WmiObject win32_userprofile -Computer $CName |select @{LABEL="last used";EXPRESSION={$_.ConvertToDateTime($_.lastusetime)}},LocalPath, SID | ft -a |Out-Host
    Get-childItem 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' | % {Get-ItemProperty $_.pspath } | Select profileImagePath, sid |Out-Host
    }

You must be logged in to reply to this topic.