Function wont run, no error message

This topic contains 14 replies, has 3 voices, and was last updated by  Olaf Soyk 4 months, 2 weeks ago.

  • Author
    Posts
  • #90614

    alex olynyk
    Participant

    When I run the function from the script pane it appears in the console pane but nothing happens
    Same issue when I run from the console pane

    function Get-RegisterType {
    
    [cmdletbinding()]
    Param(
    
    [Parameter(Mandatory=$True,Position=1)]
    [string]$computername
    
    )
    
    Get-WmiObject -Class win32_computersystem $computername | Select-Object -Property model
    
    }
    
  • #90616

    Jeremy Corbello
    Participant

    Your issue is with this line:

    Get-WmiObject -Class win32_computersystem $computername | Select-Object -Property model

    If you don't specify the -ComputerName parameter before placing your $computername variable, it thinks that you're trying to perform this query:
    "select DESKTOP01 from win32_computersystem"

    Try adding the -Computername parameter, like so:

    function Get-RegisterType {
        [cmdletbinding()]
        Param(
            [Parameter(Mandatory=$True,Position=1)]
            [string]$computername
        )
        Get-WmiObject -Class win32_computersystem -ComputerName $computername | Select-Object -Property model
    }
  • #90619

    Olaf Soyk
    Participant

    As you can see in the help of Get-WMIObject the parameter -ComputerName is not allowed to be a positional paramter. And even for the readability it is recommended that you write all paramter names in scripts. Instead of Get-WMIObject I would recommend Get-CimInstance like this:

    function Get-RegisterType {
        [cmdletbinding()]
        Param(
            [Parameter(Mandatory=$True,Position=1)]
            [string]$computername
        )
        Get-CimInstance -ClassName CIM_ComputerSystem -ComputerName $computername | Select-Object -Property model
    }
  • #90620

    alex olynyk
    Participant

    I added the computer name parameter and removed the mandatory positional parameter from the param block.
    When I run the function from the ISE I see it in the console but nothing happens.

    function Get-RegisterType {
    
    [cmdletbinding()]
    Param(
    
    [Parameter(Mandatory=$True)]
    [string]$computername
    
    )
    
    Get-CimInstance -ClassName CIM_ComputerSystem -ComputerName $computername | Select-Object -Property model
    
    }
    
    • #90626

      Olaf Soyk
      Participant

      After you "defined" the function you have to "call" it. You know that, don't you?

  • #90629

    alex olynyk
    Participant

    Don't I "call" the function by typing the name of the function?

    If so, I did.

    • #90632

      Olaf Soyk
      Participant

      ... and nothing happened? ... nothing?

  • #90635

    alex olynyk
    Participant

    It loads the function in the console pane and takes me back to a prompt. It doesn't ask for a parameter like it should.

    PS C:\Users\aolynyk\Desktop> function Get-RegisterType {

    [cmdletbinding()]
    Param(

    [Parameter(Mandatory=$True)]
    [string[]]$computername

    )

    Get-CimInstance -ClassName CIM_ComputerSystem -ComputerName $computername | Select-Object -Property model

    }

    PS C:\Users\aolynyk\Desktop>

  • #90637

    alex olynyk
    Participant

    PS C:\Users\aolynyk\Desktop> Get-RegisterType
    Get-RegisterType : The term 'Get-RegisterType' is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + Get-RegisterType
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Get-RegisterType:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    PS C:\Users\aolynyk\Desktop>

    • #90638

      Olaf Soyk
      Participant

      You have to define the function where you want to use it. Try this:

      function Get-RegisterType {
          [cmdletbinding()]
          Param(
              [Parameter(Mandatory=$True)]
              [string]$computername
          )
          Get-CimInstance -ClassName CIM_ComputerSystem -ComputerName $computername | Select-Object -Property model
      }
      Get-RegisterType
  • #90640

    alex olynyk
    Participant

    when I run Get-RegisterType I get:

    PS C:\Users\aolynyk\Desktop> Get-RegisterType
    Get-RegisterType : The term 'Get-RegisterType' is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + Get-RegisterType
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Get-RegisterType:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    PS C:\Users\aolynyk\Desktop>

    • #90641

      Olaf Soyk
      Participant

      ... sounds strange .... what happens when you run following code?

      function Test {"ComputerName '$($env:COMPUTERNAME)'"}; Test
  • #90643

    alex olynyk
    Participant

    Works great!

    PS C:\Users\aolynyk\Desktop> .\Test.ps1
    ComputerName 'TECHSUPPORT05'

  • #90644

    alex olynyk
    Participant

    I added a ";" followed by the function name outside the script block. Its working now

    • #90646

      Olaf Soyk
      Participant

      Hmmm .... ok .... you define the function once like this:

      function blabla{ ... code ...}

      and later you use it as often as you need like this:

      blabla
      ... some other code ...
      blabla

      and all that in the same console or ise session

You must be logged in to reply to this topic.