Function

This topic contains 6 replies, has 3 voices, and was last updated by  js 2 months, 3 weeks ago.

  • Author
    Posts
  • #94926

    alex olynyk
    Participant

    When running this function this line doesnt appear to execute

     Get-WmiObject -Class win32_Bios -ComputerName $computername | Select-Object -Property SerialNumber 

    does not run. Any idea why?

    function Get-RegisterType {
    
         [cmdletbinding()]
    
         Param(
    
         [Parameter(Mandatory=$True)]
         [string]$computername
    
         )
    
         if (Test-Connection -ComputerName $computername -Quiet -Count 1) {
    
         Write-Host "$computername is online" -ForegroundColor Green -BackgroundColor Black
              
         Get-WmiObject -Class win32_ComputerSystem -ComputerName $computername | Select-Object -Property Model, Name
         Get-WmiObject -Class win32_Bios -ComputerName $computername | Select-Object -Property SerialNumber
    }
    
         else { Write-Host "$computername is not online.  Please look in LANDesk to determine register type." -ForegroundColor Red -BackgroundColor Black }
    
    }
    
    ; Get-RegisterType
    
  • #94929

    Don Jones
    Keymaster

    what makes you think that?

    And, if you've run it without specifying a computer name, what is your expectation for the output?

    • #94933

      alex olynyk
      Participant

      Sorry, forgot to include the output

      PS C:\Software\Scripts> .\Get-RegisterType.ps1
      cmdlet Get-RegisterType at command pipeline position 1
      Supply values for the following parameters:
      computername: 20061sure03
      20061sure03 is online

      Model Name
      —– —-
      4800E42 20061SURE03

      It outputs this

      Get-WmiObject -Class win32_ComputerSystem -ComputerName $computername | Select-Object -Property Model, Name

      but not this

      Get-WmiObject -Class win32_Bios -ComputerName $computername | Select-Object -Property SerialNumber
  • #94938

    Don Jones
    Keymaster

    And if you just run:

    Get-WmiObject -Class win32_Bios -ComputerName 20061sure03 | Select-Object -Property SerialNumber

    In the console, do you get output?

    • #94939

      alex olynyk
      Participant

      Yes.

      PS C:\Software\Scripts> Get-WmiObject -Class win32_Bios -ComputerName 20061sure03 | Select-Object -Property SerialNumber
      
      SerialNumber
      ------------
      41TGV23     
  • #94945

    Don Jones
    Keymaster

    So, when you write a function in PowerShell, there are a few standards you want to try to comply with.

    One, don't write messages directly to Host. Your Write-Hosts should _really_ be Write-Verbose; run your command with -Verbose to see that output.

    Two, you need to output *one thing, and one thing only,* to the pipeline. Right now you're trying to emit two things.

         $cs = Get-WmiObject -Class win32_ComputerSystem -ComputerName $computername 
         $bios = Get-WmiObject -Class win32_Bios -ComputerName $computername 
         $props = @{Computername = $computername
                    Model = $cs.model
                    Name = $cs.name
                    SerialNumber = $bios.serialnumber}
         New-Object -Type PSObject -Prop $props
    

    When you're querying multiple sources, you want to combine whatever you want into a single output thing. This is part of the fundamental narrative of "Learn PowerShell Scripting in a Month of Lunches," if you'd like to dive into it in more detail. I use the above example, almost exactly, throughout the entire book, in fact.

  • #94956

    js
    Participant

    Powershell is implicitly outputting to format-table. So after it has one set of columns decided on, it won't display another set of columns. This was a gotcha for me when I was learning.

You must be logged in to reply to this topic.