Welcome › Forums › General PowerShell Q&A › Function
This topic contains 6 replies, has 3 voices, and was last updated by
-
AuthorPosts
-
March 1, 2018 at 9:04 pm #94926
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
-
March 1, 2018 at 9:05 pm #94929
what makes you think that?
And, if you've run it without specifying a computer name, what is your expectation for the output?
-
March 1, 2018 at 9:20 pm #94933
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 onlineModel Name
—– —-
4800E42 20061SURE03It 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
-
-
March 1, 2018 at 9:33 pm #94938
And if you just run:
Get-WmiObject -Class win32_Bios -ComputerName 20061sure03 | Select-Object -Property SerialNumber
In the console, do you get output?
-
March 1, 2018 at 9:34 pm #94939
Yes.
PS C:\Software\Scripts> Get-WmiObject -Class win32_Bios -ComputerName 20061sure03 | Select-Object -Property SerialNumber SerialNumber ------------ 41TGV23
-
-
March 1, 2018 at 9:39 pm #94945
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.
-
March 1, 2018 at 10:28 pm #94956
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.
-
AuthorPosts
The topic ‘Function’ is closed to new replies.