<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1678611822423757&amp;ev=PageView&amp;noscript=1">
Defrag This

| Read. Reflect. Reboot.

How to Cleanup Proprietary PowerShell Scripts to Share with the Community

Adam Bertram| March 14 2018

| IT insights

Before sharing code with the external world, there are a few essential questions to consider.

Involvement in some kind of technical community is a beautiful thing. Not only do you contribute your knowledge to others, but you also gain knowledge and make yourself known, which could lead to other opportunities as well! I cannot recommend enough how important it is to be involved in some kind of technical community. However, when it comes to sharing code in a community, things can sometimes get hairy. Some companies frown upon sharing for fear of exposing the company's intellectual property or an inadvertent exposure of internal details. These are valid concerns, but if your company does allow you to share, it is important to be diligent. You must ensure that any company-specific information is removed from your code before exposing it to the outside world.

There are a few questions you should ask yourself before hitting the publish button to share code (PowerShell scripts in this case) with the world outside of your company.

  1. Is my company name referenced anywhere in the script?
  2. Is any company-specific project name, department, etc. mentioned in the script?
  3. Is the script referencing any internal function or library that you won't be sharing?
  4. Does the script represent any company intellectual property (IP)?

Some of these questions are more complicated to answer than others. If you're ever unsure if you can share a particular script, it's always wise to consult with your manager or your legal team to ensure all is well.

Answering questions 1-3 can be done by looking at your code. It's possible to manually verify each of these items by reading through your code, doing lots of find/replace actions on strings like your company name, departments, names, etc. and checking each function reference to ensure it's not calling an internal module. This is possible, but it sucks. I've been there many times, and it's a task that has prevented me from sharing a lot of great work just because of the effort it takes to share it.

It is sometimes just too troublesome to clean up your code to make it "shareable." Instead, you just think to yourself "I'll get to that one day", but that day never comes around. To make that task a little easier, it's possible to create a series of tests written with code that will test your code for potential sharing blockers. By using tools like PowerShell and Pester, we can create a script that can check your PowerShell code to ensure it is in a shareable state. My solution to this problem is to build a Pester test script I call ScrubShare.

Read: Advanced PowerShell Functions: Upping Your Game

ScrubShare is the start of a tool that can simplify the task of sanitizing code. Let's say I work for a company called Acme. In Acme, I have a lot of PowerShell scripts and want to break a couple out and share them with the community. One of them may look something like this simple example.

## Acme script to do thing

Get-ChildItem -Path 'C:\stuff' | foreach {
    Do-SometInternalThing
}

Notice that I have a reference to my company and we're going to assume that the Do-SomeInternalThing isn't include with the script. Perhaps it's located in some other internal PowerShell module instead. When I share this script, I don't want that company reference in there. I'll need to either include the Do-SomeInternalThing with the script or refactor it somehow. Rather than eyeballing a large script and looking for instances like this, I can run a Pester test in the ScrubShare project to get an output like this:

PS> .\Community.Tests.ps1 -FolderPath C:\Scripts\ -CompanyReference Acme

Describing [C:\Scripts\CompanyScript.ps1] Test
  [-] has no references to our company-specific strings 80ms
    Expected: value to be empty but it was {Acme}
    at <ScriptBlock>, \\Mac\Home\DropBox\GitRepos\ScrubShare\Community.Tests.ps1: line 102
    102:                 $companyReferences | should benullOrempty
  [-] has no references to private functions 41ms
    Expected: {}
    But was:  {Do-SometInternalThing}
    at <ScriptBlock>, \\Mac\Home\DropBox\GitRepos\ScrubShare\Community.Tests.ps1: line 106
    106:                 $privateCommandNames | should be $null
  [+] has no references to private modules 31ms

Above you can see that the code is telling me that I still have a reference to my company name, and I am calling a function that isn't available. The act of putting these checks to code could potentially save a ton of time!

The next time you'd like to share some PowerShell code, use ScrubShare or build your own tool. By eliminating the barriers you'll be more inclined to give back and to get involved.

Topics: IT insights

Leave a Reply

Your email address will not be published. Required fields are marked *

THIS POST WAS WRITTEN BY Adam Bertram

Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

Free Trials

Getting started has never been easier. Download a trial today.

Download Free Trials

Contact Us

Let us know how we can help you. Focus on what matters. 

Send us a note

Subscribe to our Blog

Let’s stay in touch! Register to receive our blog updates.