Function wont run, no error message

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

}

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
}

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
}

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

}

After you “defined” the function you have to “call” it. You know that, don’t you?

Don’t I “call” the function by typing the name of the function?

If so, I did.

… and nothing happened? … nothing?

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>

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>

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

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>

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

function Test {“ComputerName ‘$($env:COMPUTERNAME)’”}; Test

Works great!

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

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

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