2

So i have a script I've written to pull down scope information from DHCP. It works fine drawing the information into the PowerShell window. I can pipe Out-GridView or Export-ToCSV to the process, but I was trying to find a way that when someone use's these functions, they can choose either of those by using a parameter in the function.

Example: Get-ScopeByDescription -Description "Site" -Property "Hostname" -ToCSV or -ToGridView

Function Get-ScopeByDescription
{
 [cmdletBinding()]
    Param
    (
        [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [String] $Description,

        [Parameter(ValueFromPipelineByPropertyName)]
        [ValidateSet('IPAddress','Hostname','ClientID']
        [String] $Property = 'Hostname'

        ###This is where I'm looking to add functionality of Export-ToCSV and Out-GridView as a Parameter.

    )
    Begin
    {
        Write-Warning "This process will take a minute. Ctrl+C to cancel."
    }
    Process
    {
        $ScopeDesc = Get-DhcpServerv4Scope -ComputerName DHCPsvr |
                     Where-Object -Property Description -Like *"$Description"*

        $ScopeDesc | Get-DhcpServerv4Lease -ComputerName DHCPsvr -AllLeases |
                     Select IPAddress, Hostname, ClientID, LeaseExpiryTime, AddressState, ScopeID |
                     Sort-Object -Property $Property

    } 

This script is meant to pull down the DHCP scope from the server by description, which is an easier to find/remember parameter for the persons using this to remember.

n4mlx
  • 21
  • 2
  • 8
    I wouldn't recommend doing this. One of PowerShell's amazing abilities is it's Pipeline so you don't have to inherit all possible I/O, filtering, sorting, functions on every object and function. To try and draw a line to this, why stop at CSV and GridView? What about XML and JSON? If they can understand -ToCsv, they should be able to understand Get-ScopeByDescription | Export-Csv 'somefile.csv' – thepip3r Mar 17 '20 at 18:47
  • That's actually a really good point. I ended up just piping the GridView into the basic code and for CSV. I'll just teach them to pipe it in. Thanks! – n4mlx Mar 19 '20 at 17:57

0 Answers0