Author Posts

January 2, 2018 at 2:30 pm

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

}

January 2, 2018 at 2:42 pm

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
}

January 2, 2018 at 2:42 pm

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
}

January 2, 2018 at 2:54 pm

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

}

January 2, 2018 at 3:20 pm

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

January 2, 2018 at 3:32 pm

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

If so, I did.

January 2, 2018 at 3:33 pm

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

January 2, 2018 at 3:38 pm

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>

January 2, 2018 at 3:40 pm

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>

January 2, 2018 at 3:41 pm

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

January 2, 2018 at 3:50 pm

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>

January 2, 2018 at 3:55 pm

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

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

January 2, 2018 at 4:00 pm

Works great!

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

January 2, 2018 at 4:04 pm

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

January 2, 2018 at 4:13 pm

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