What is the best practice to separate Debug/Production environments

Hello,

Wondering what would be the “best practice” to separate code for running in debug/production environment. For example I want to default value for email to be different when I deploy to production vs when I develop powershell scripts.
Will it be environment value, relying on machine name, switch parameter?

Hi there!

This really depends on your organization’s practices and policies, you might have trouble finding a ‘best practice’. You might consider a very simple configuration file, perhaps based on JSON (if humans need to modify if).

I’m sure it has its weaknesses, but I find the configuration complexity clock worth considering.

Cheers!

If you are actually using Write-Debug for debugging, you could do something simple like this:

[CmdLetBinding()]
param()

Write-Debug "Starting script"

$mailParams = @{
    To="productionemail@mycomp.com" 
    From="admin@mycomp.com"
    Subject="Hello"
    Body="Hello"
    BodyAsHtml=$true
    SmtpServer="smtprelay.mycomp.com"
}


if ($PSBoundParameters.ContainsKey('Debug')) {
    Write-Debug "Updating recipient..."
    $mailParams.Set_Item("To", "Testing@mycomp.com")
}

$mailParams
Send-MailMessage @mailParams

If you just call the script, it sends to your default params…

Name                           Value                                                                                                                                                                        
----                           -----                                                                                                                                                                        
Body                           Hello                                                                                                                                                                        
SmtpServer                     smtprelay.mycomp.com                                                                                                                                                         
Subject                        Hello                                                                                                                                                                        
To                             productionemail@mycomp.com                                                                                                                                                   
From                           admin@mycomp.com                                                                                                                                                             
BodyAsHtml                     True          

However, if you pass the -Debug switch to the script we detect that it’s been passed and update the TO…

PS C:\Windows> C:\Users\rsimmers\Desktop\test.ps1 -Debug
DEBUG: Starting script
DEBUG: Updating recipient...

Name                           Value                                                                                                                                                                        
----                           -----                                                                                                                                                                        
Body                           Hello                                                                                                                                                                        
SmtpServer                     smtprelay.mycomp.com                                                                                                                                                         
Subject                        Hello                                                                                                                                                                        
To                             Testing@mycomp.com                                                                                                                                                           
From                           admin@mycomp.com                                                                                                                                                             
BodyAsHtml                     True               

You could do the same thing with Verbose so don’t have to answer prompts all day long.