DocNougat / Meraki-Powershell-Module

A PowerShell Module for the Cisco Meraki API
GNU General Public License v3.0
18 stars 3 forks source link

Use "Write-Debug" instead of "Write-Host" #7

Closed abelal83 closed 8 months ago

abelal83 commented 8 months ago

I have this code custom function that wraps functions from Meraki module

function Invoke-MyMerakiClaimDevices {

    param (
        [string] $AuthToken,
        [PSCustomObject] $Network,
        [System.Collections.IEnumerable] $DeviceSerials
    )

    try {
        $deviceSerialsConfig = New-DeviceClaimConfig -NetworkDevicesToClaim $DeviceSerials
        $deviceSerialsToClaim = $deviceSerialsConfig | ConvertTo-Json
        Invoke-MerakiNetworkClaimDevices -AuthToken $AuthToken -NetworkId $Network.id -DeviceSerials $deviceSerialsToClaim
    }
    catch {
         Write-Warning $PSItem
         Write-Warning "Retrying claim request by removing claimed devices."
        # rebuild $deviceSerials removing any that caused errors
        $deviceSerialsUnclaimed = @()
        $deviceErrors = $PSItem | ConvertFrom-Json
        $deviceErrors.errors.foreach({ 
                $serial = $PSItem.Split(' ')[3]
                if (-not $NetworkDevicesToClaim.Contains($serial)) {
                    $deviceSerialsUnclaimed += $serial
                }
            })
        $deviceSerialsConfig = New-DeviceClaimConfig -NetworkDevicesToClaim $deviceSerialsUnclaimed
        $deviceSerialsToClaim = $deviceSerialsConfig | ConvertTo-Json
        Invoke-MerakiNetworkClaimDevices -AuthToken $AuthToken -NetworkId $Network.id -DeviceSerials $deviceSerialsToClaim
    }
}

When the function call to Invoke-MerakiNetworkClaimDevices fails it writes to host { "errors": [ "Device with serial Q3AC-xxxx-xxxx is already claimed and in tst03 (network ID: XXXX)", "Device with serial Q3AC-xxxxx-xxxx is already claimed and in tst03 (network ID: XXXX)" ] } As I also have Write-Warning $PSItem the error is written twice.

I can disable my messages with $WarningPreference but not possible at all with Write-Host.

I propose all the Write-Host functions are replaced with Write-Debug allowing control of output with $DebugPreference variable. This has the benefit of error being captured and handled without polluting logs. In my example I handle it by removing devices and attempting again but errors fill up the logs.

DocNougat commented 8 months ago

Hi Abu, Thanks again for these suggestions, this is my first time writing a custom module with this large of a scope. So please keep the suggestions/best practices coming.

I went ahead and updated to 1.0.7 with this suggested change. Let me know if you think of anything else.

Thanks, Alex

abelal83 commented 8 months ago

For a first time module I commend you the brilliant work. Really is fantastic and by far the most feature rich Meraki module I've come across.

I attempted a pull request to help but I don't think the repo allows branching, so will keep suggestions coming as issues.

Thanks again for the quick turnaround!