Function wont run, no error message

Welcome Forums General PowerShell Q&A Function wont run, no error message

This topic contains 14 replies, has 3 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #90614

    Participant
    Points: 1
    Rank: Member

    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

    Participant
    Points: 26
    Rank: Member

    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

    Participant
    Points: 740
    Helping Hand
    Rank: Major Contributor

    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

    Participant
    Points: 1
    Rank: Member

    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

      Participant
      Points: 740
      Helping Hand
      Rank: Major Contributor

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

  • #90629

    Participant
    Points: 1
    Rank: Member

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

    If so, I did.

    • #90632

      Participant
      Points: 740
      Helping Hand
      Rank: Major Contributor

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

  • #90635

    Participant
    Points: 1
    Rank: Member

    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

    Participant
    Points: 1
    Rank: Member

    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

      Participant
      Points: 740
      Helping Hand
      Rank: Major Contributor

      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

    Participant
    Points: 1
    Rank: Member

    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

      Participant
      Points: 740
      Helping Hand
      Rank: Major Contributor

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

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

    Participant
    Points: 1
    Rank: Member

    Works great!

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

  • #90644

    Participant
    Points: 1
    Rank: Member

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

    • #90646

      Participant
      Points: 740
      Helping Hand
      Rank: Major Contributor

      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

The topic ‘Function wont run, no error message’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort