Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
April 19, 2022

Parameterizing the Basic Function

In the last article, I showed you how to create a basic PowerShell function from a script block. A function at its core is nothing more than a script block with a name. Having a name makes it easier to run and, as we’ll see over the course of this article series, makes it possible to partake in the PowerShell pipeline.

The PowerShell function should be written as a building block that does one thing and writes one object type to the pipeline. The function I demonstrated last time meets these criteria.

Function Get-EventlogInfo {
    $logs = Get-CimInstance win32_nteventlogfile -Filter "NumberofRecords>0"
    foreach ($log in $logs) {
        [pscustomobject]@{
            Log          = $log.LogfileName
            Count        = $log.NumberOfRecords
            SizeMB       = $log.filesize / 1MB
            MaxSizeMB    = $log.MaxFileSize / 1MB
            Utilization  = ($log.FileSize / $log.MaxFileSize) * 100
            LastUpdate   = $log.LastModified
            Computername = $log.CSName
        }
    }
}

A function’s purpose is to be re-usable and flexible. You want to write a function that offers some variability while adhering to the principles of a PowerShell function. That’s what we’re going to look at next.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.