none
Get-CimInstance for Array of Computer Objects RRS feed

  • Question

  • Hi Folks,

    Relatively new to PowerShell and am most of the way through Ed Wilson's 'First Steps' book. I'm attempting to use Get-CimInstance; I expect I'm doing something that's very blatantly wrong.

    I've used Get-ADComputer to get several computer objects which I've placed into an array called $itcomputers. I'd like to see the InstallDate from win32_bios for all of these computers. When typing in the names manually, it works fine, but when it's supplied with the variable to -ComputerName I'm shown an error.

    Even changing the objects to string using Out-String doesn't seem to work. Do you have any thoughts on what I'm doing wrong? I expect perhaps I'm just simply going the wrong way about getting this cimcmdlet to get the information from an array. In regards to the below script, $itcomputers is populated at this stage already.

    foreach ($computer in $itcomputers)
    {
        Get-CimInstance -ClassName win32_bios -Property InstallDate -ComputerName $computer
    }

    The error says the below, with the Distinguished Name as the value:

    Get-CimInstance : WS-Management could not connect to the specified destination:

    Many thanks.

    Monday, June 23, 2014 3:33 PM

Answers

All replies

  • CIM does not use AD names. It uses either NetBIOS or DNS names and addresses.


    ¯\_(ツ)_/¯

    Monday, June 23, 2014 3:36 PM
  • Hi,

    Try using the Name property of the $computer object:

    foreach ($computer in $itcomputers) {
    
        Get-CimInstance -ClassName Win32_BIOS -Property InstallDate -ComputerName $computer.Name
    
    }


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

    • Marked as answer by Kirk Watts Tuesday, June 24, 2014 8:25 AM
    Monday, June 23, 2014 3:40 PM
  • Hi jrv,

    Thanks for your reply. If I Select-Object -Property DNSHostName and put that into the array instead, I'm shown this error (which seems in essence to be the same:

    Get-CimInstance : WS-Management could not connect to the specified destination: (@{DNSHostName=computer.company.com}:5985).
    At line:4 char:5
    +     Get-CimInstance -ClassName win32_bios -Property InstallDate -ComputerName $c ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-CimInstance], CimException
        + FullyQualifiedErrorId : HRESULT 0x80072ee5,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand

    I'm keen to learn if there's something specific I could have checked to understand why this wouldn't have worked.

    Using the computer object name to -computername works without issue.

    Thanks.


    • Edited by Kirk Watts Monday, June 23, 2014 3:42 PM More info
    Monday, June 23, 2014 3:41 PM
  • The first thing any book will do is tell you how to use "HELP".  Help is always the best first sop when you have an issue that you do not understand.  Here is the help for your issue and how to get it:

    PS C:\scripts> help Get-CimInstance -param ComputerName

    -ComputerName <String[]>
        Specifies computer on which you want to run the CIM operation. You can specify a fully qualified domain name
        (FQDN), a NetBIOS name, or an IP address.

        If you do not specify this parameter, the cmdlet performs the operation on the local computer using Component
        Object Model (COM).

        If you specify this parameter, the cmdlet creates a temporary session to the specified computer using the WsMan
        protocol.

        If multiple operations are being performed on the same computer, using a CIM session gives better performance.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       True (ByPropertyName)
        Accept wildcard characters?  false

    Quick Help:

    Get-CimInstance -?

    You can juts add the question mark to the end of any command line you have in the stack:

     Get-CimInstance -ClassNamewin32_bios -Property InstallDate -ComputerName$computer -?


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, June 23, 2014 3:41 PM
    Monday, June 23, 2014 3:41 PM
  • Hi jrv,

    Thanks for your reply. If I Select-Object -Property DNSHostName and put that into the array instead, I'm shown this error (which seems in essence to be the same:

    Get-CimInstance : WS-Management could not connect to the specified destination: (@{DNSHostName=computer.company.com}:5985).
    At line:4 char:5
    +     Get-CimInstance -ClassName win32_bios -Property InstallDate -ComputerName $c ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-CimInstance], CimException
        + FullyQualifiedErrorId : HRESULT 0x80072ee5,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand

    I'm keen to learn if there's something specific I could have checked to understand why this wouldn't have worked.

    Using the computer object name to -computername works without issue.

    Thanks.



    As Mike tried to show you.  You do not have an array of strings.  You have a hash.  a hash must be referenced as MIke has shown above and CIM cannot use 'DistinguishedName'.

    ¯\_(ツ)_/¯

    Monday, June 23, 2014 3:46 PM
  • $itcomputers='firstpc','secondpc','thirdpc'
    foreach ($computer in $itcomputers) {
        Get-CimInstance -ClassName Win32_BIOS -Property InstallDate -ComputerName $computer
    }
    
    $itcomputers=@{Name='firstpc'},@{Name='secondpc'},@{Name='thirdpc'}
    foreach ($computer in $itcomputers) {
        Get-CimInstance -ClassName Win32_BIOS -Property InstallDate -ComputerName $computer
    }

    Watch these:

    PS C:\scripts> $itcomputers=@{Name='firstpc'},@{Name='secondpc'},@{Name='thirdpc'}
    PS C:\scripts> foreach ($computer in $itcomputers) {$computer}
    
    Name                           Value
    ----                           -----
    Name                           firstpc
    Name                           secondpc
    Name                           thirdpc
    
    
    PS C:\scripts> foreach ($computer in $itcomputers) {$computer.Name}
    firstpc
    secondpc
    thirdpc
    PS C:\scripts>


    ¯\_(ツ)_/¯

    Monday, June 23, 2014 3:50 PM
  • With PowerShell 3 and later and CIM CmdLets we can also use these combined shortcuts:

    $itcomputers=@{Name='firstpc'},@{Name='secondpc'},@{Name='thirdpc'}
    Get-CimInstance -ClassName Win32_BIOS -Property InstallDate -ComputerName $computer.Name


    ¯\_(ツ)_/¯

    Monday, June 23, 2014 4:08 PM
  • We can leverage all other accelerators and shortcuts available in PowerShell.

    Here we are converting the hashes into full objects which can be fed into the pipeline.

    # we can use objects buit with teh computer name proeprty as 'ComputerName'
    $itcomputers=@{ComputerName='pc1'},@{ComputerName='pc2'},@{ComputerName='pc3'}
    $computers=$itcomputers|%{New-Object PsObject -Property $_}
    $computers| Get-CimInstance -ClassName Win32_BIOS -Property InstallDate
    
    # or we can use any alias that is defined
    $itcomputers=@{CN='PC1'},@{CN='PC2'},@{CN='PC3'}
    $computers=$itcomputers|%{New-Object PsObject -Property $_}
    $computers| Get-CimInstance -ClassName Win32_BIOS -Property InstallDate
    



    ¯\_(ツ)_/¯

    Monday, June 23, 2014 4:21 PM
  • Hi jrv,

    Thanks for your reply. If I Select-Object -Property DNSHostName and put that into the array instead, I'm shown this error (which seems in essence to be the same:

    Get-CimInstance : WS-Management could not connect to the specified destination: (@{DNSHostName=computer.company.com}:5985).
    At line:4 char:5
    +     Get-CimInstance -ClassName win32_bios -Property InstallDate -ComputerName $c ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-CimInstance], CimException
        + FullyQualifiedErrorId : HRESULT 0x80072ee5,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand

    I'm keen to learn if there's something specific I could have checked to understand why this wouldn't have worked.

    Using the computer object name to -computername works without issue.

    Thanks.


    As to the exact answer to this exact issue I cannot say as we cannot see your code.  From the error you are doing something quite odd.

    Have you resolved this now?


    ¯\_(ツ)_/¯

    Monday, June 23, 2014 6:48 PM
  • Hi jrv, Mike,

    Thanks for the input. '$computer.DNSHostName' seemed effective with this, and wasn't something I had covered as possible in the First Steps book yet. I was attempting to select and limit the output of my Get-ADComputer line up-front so that the script block could only use DNSHostName, seems that I wasn't able to do this.

    Get-ADComputer -SearchBase 'OU=IT support,OU=oufolder,DC=computers,DC=computer,DC=com' -Filter '*' -Properties DNSHostName | Select-Object DNSHostName
    foreach ($computer in $itcomputers)
    {
        Get-CimInstance -ClassName win32_bios -Property InstallDate -ComputerName $computer
    }

    Thanks again for your help. Will keep reading!

    Tuesday, June 24, 2014 8:41 AM
  • (Get-ADComputer -filter * -Property *).DNSHostName

    OR

    Get-ADComputer -filter * -Property * | Select -expand DNSHostName

    This is not the question you originally asked.

    The issue is that PowerShell uses objects almost everywhere.  Your attempt wanted simple strings.  Your code does not produce simple strings.

    Start on page one of book.  Do every example until you fully understand it.  Do not go to next chapter or skip ahead.  If you can complete the whole book and understand what is being shown you will learn this.  Guessing and jumping ahead will lead to confusion.


    ¯\_(ツ)_/¯

    Tuesday, June 24, 2014 11:15 AM
  • Hi jrv, 

    Apologies, I'm not entirely clear I understand the intended outcome from your post, it seems there has been a misunderstanding that my last post was meant as 2nd question - this was simply me sharing my findings to close the question down as answered and saying thanks.

    I haven't finished the book yet but feel I've understood what I've read; I'll intend to read the step-by-step and more recent best practices book as follow-ons.

    Thanks.
    Tuesday, June 24, 2014 12:21 PM
  • Read you original question very carefully You never state how your get $itcomputers.  As far as anyone can tell it is a list of strings.  THe error you posted shows that it is a hash of values.

    The issue is not what to do in the loop but how to get a list OR how to access an object property in a collection.

    TO get a list of strings:

    $itcomputers=(Get-AdComputer ....).DNSHostNames
    OR
    $itcomputers=Get-AdComputer .... | Select -expand DNSHostNames

    Now you can use your loop:
    foreach ($computer in $itcomputers){
        Get-CimInstance -ClassName Win32_BIOS -Property InstallDate -ComputerName $computer
    }

    My point about the book is that you are not understanding what you have read.  You seem to be mixing up certain bits.  No where does the book tell you to use DistinguishedName as a computer name in CIM or WMI.  In fact there is no indication that you can use anything but a computer name anywhere there is a reference to a computer.

    If you go back to the first page and carefully read and do all exercises again with a clear mind you will see what I mean.  Once the brain adjusts to technology things will look very different.  All of this will become quite obvious.

    Thinking and using objects is counter-intuitive.  You must let your mind make the paradigm shift in order to see how it works.

    Anyway - good luck - you will eventually figure it out.


    ¯\_(ツ)_/¯

    Tuesday, June 24, 2014 12:35 PM
  • I think this is a case of my early understanding of things. When I said "I've used Get-ADComputer to get several computer objects which I've placed into an array called $itcomputers.", I was unaware there was an additional layer of detail that would have been more helpful and that the output from Get-ADComputer can be manipulated as such. The ommission of this info wasn't deliberate :).

    I found on a few occassions in the 'First Steps' book that something new is shown, and very little explaining is done and I assume more detail will come later to explain the context and logic, but some things don't seem to be coming. Such an example is (The-Cmdlet Blabla).Property - it's mentioned as an aside when learning about the environment provider, but hasn't been covered since and doesn't explain the output is a string, so I didn't think to use it. I'm also not sure where to start in the help for this type of stuff (e.g. knowing the output is a string in accessing things in that way).

    Things like Select -expand, http://technet.microsoft.com/en-us/library/hh849895.aspx even states expandpropety has no alias. There's still a lot for me to learn and I definitely intend of reading the books more than once! 

    Thanks again.
    Tuesday, June 24, 2014 12:57 PM
  • Not saying you posted incorrectly.  It is clear you are trying to sort all of this out.  I just wanted to note that your last post You said: "I was attempting to select and limit the output of my Get-ADComputer line up-front so that the script block could only use DNSHostName, seems that I wasn't able to do this."

    This shows that you haven't understaood the early steps in the book.  By going back and carefully rereading with this in mind I think you will see where your perceptipn is misleading you.

    As I posted before, you need a paradigm shift in  thinking to occur before all of this becomes clear.

    Ask yourself what an object is and what a property is.   Try to understand that CmdLets return objects.  Select-Object returns objects.  Look up the "-expand" parameter.  Read the help topics starting with "about_"

    As noted above - it will click and after that it will be all downhill.


    ¯\_(ツ)_/¯

    Tuesday, June 24, 2014 1:07 PM