PowerShell ignoring lines?

I have a script that is dead simple. It gets system info. But when I execute it it’s only running like 3 of the lines and not outputting anything else to the screen?

This is the code

Do you really need this:

?
All Windows versions coming with less than PowerShell version 3.0 are out of support. :wink:

Regardless of that is your code a little cumbersome, redundand and confusing. You query every individual computer 6 times. 3 times of wich you’re querying the same CIM class. That’s very inefficient.

I’d start with something like this:

function Get-CoreInfo {
    Param(
        [Parameter(
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [String[]]$ComputerName = $Env:COMPUTERNAME
    )
    process {
        foreach ($Computer in $ComputerName) {
            $CimSession = New-CimSession -ComputerName $Computer

            $ComputerSystem = 
                Get-CimInstance -ClassName CIM_ComputerSystem -CimSession $CimSession
            $OperatingSystem = 
                Get-CimInstance -ClassName CIM_OperatingSystem -CimSession $CimSession
            $BIOS = 
                Get-CimInstance -ClassName CIM_BIOSElement -CimSession $CimSession
    
            [PSCustomObject]@{
                ComputerName       = $ComputerSystem.Name
                Manufacturer       = $ComputerSystem.Manufacturer
                Model              = $ComputerSystem.Model
                SerialNumber       = $BIOS.SerialNumber
                OperatingSystem    = $OperatingSystem.Caption
                Version            = $OperatingSystem.Version
                SystemArchitecture = $OperatingSystem.OSArchitecture
                BIOSName           = $BIOS.Name
                BIOSManufacturer   = $BIOS.Manufacturer
                BIOSVersion        = $BIOS.Version
            }
        }
    }
}

This way you can run this function without any parameter for the local computer. With one or more computer names as parameters or computer names passed by pipeline.

Get-CoreInfo

Get-CoreInfo -ComputerName RemoteComputer_01, RemoteComputer_02

'RemoteComputer_01', 'RemoteComputer_02' | 
    Get-CoreInfo  | 
        Format-Table -AutoSize

Oh wow - so completely remove all entries for WMI ?

The idea is I want to have a ton of little functions, all of which do something on their own, they all get information.

Example- get-netinfo or get-storageinfo

But I want to package it as a module, import all those functions into one module file, then create ONE function that calls each of these ‘little’ functions individually - like a really comprehensive version of GET-COMPUTERINFO - except with like all computer info possible

Do you have any recommendations on how to do that? :slight_smile:

Thanks for your help Olaf

Yes. It is not supported anymore in PowerShell v 7.x anyway. :man_shrugging:t3:

That’s a nice idea. Especially when you share your code with colleagues. :+1:t3:

I wouldn’t do that. IMO that would make it harder to maintain. I like to keep every single function in its own file with the same name like the function. :point_up:t3:

Hmmm … I’d expect that to be a messy output since the information provided by those functions are really different from each other. :man_shrugging:t3:

Whatfor? Do you have a real world use case for such an info overkill? :wink:

What exactly?

… unsing modules ingeneral?

… creating modules in general?

… my general recommendation would be: before you start re-inventing the wheel again by writing your own code, search online for examples of code for the task you want to accomplish. For the vast majority of the cases you’re not the very first one with a given task and there are probably code snippets, scripts or modules out there you can use right away. And maybe they are even better than anything you could come up with.

A great place to start your search would be the
https://www.powershellgallery.com/
or

or

When you write your own code you may follow the

And please … always read the help for the cmdlets you’re about to use COMPLETELY INCLUDING THE EXAMPLES to learn how to use them. :point_up:t3:

Have a lot of fun!
:love_you_gesture:t3:

1 Like

Awesome man - thank you for your help Olaf as always!

Hey Olaf - I had one more question that has been bugging me and I can’t find a clear answer on?

All the PWSH books really drive this point home that you should always be outputting data to Write-Output and to avoid Write-Host like the plague lol

  • Is there ever really a reason to use Write-Host?
  • Or is it best practice to just always output using Write-Output even in the case of returning strings or just showing output to the end user who’s running the script?

If you want to output something explicitly to the console - yes. But for most of the use cases it is used out there there are usually better options like Write-Debug or Write-Verbose. Implemented correctly you can switch those messages on if needed and omit it by default.

There is barely the need to explicitly use Write-Output since everything you leave or drop from your code becomes implicitly part of the output stream.

2 Likes

You included the pscustomobject in your implementation on my script to create an object

Is that best practice to do that for commands that ‘get’ something?
Would just simply calling the objects properties suffice?

Ex

            $ComputerSystem = 
                Get-CimInstance -ClassName CIM_ComputerSystem -CimSession $CimSession

Wouldn’t I be able to just call $ComputerSystem and format it with Select-Object by making a custom property? Or is that bad practice and I should always use the pscustomobject with commands that ‘Get’ something?

It is IMO the best way to combine results from more than one query in one output.

Of course. If you want to output ONLY the properties for the CIM class CIM_ComputerSystem separately you can use a simple Select-Object. In general it is totally up to you how you use PowerShell for your particular needs. :man_shrugging:t3:

1 Like

Thank you! I will try to use New-Object more in my scripts!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.