So this is Part 3 of a series of Blog Posts on my (currently ever changing) Workflow with Git, Github & PowerShell.
Hopefully you have had chance to look at the previous posts in this series if not they are
However, for this post we will be concentrating on Script & Module Creation and how we can make the overall experience more efficient with the PSISE_Addons module that I’m releasing on Github https://github.com/kilasuit/ISE_Cew
We will cover the following items today
- Use of PSDrives for the functions & why you should use them in this case
- Use of Git for the source control in this module – Simple and hopefully clear to follow and not too in depth
- The Functions used in this module
- Creating compliant PSD1 files for the PowerShell Gallery – Because it’s annoying to have to do this manually and that’s why we automate right – Again added in this Module is an example!
- Creating some basic Pester Tests – again without even thinking about it as I am giving this to you as part of the ISE_Cew Module!
So firstly – Using PSDrives within the Function and why to use them.
PSDrives are a good and simple way of having a location that you can reach only in PowerShell and can use a variety of Different Providers – FileSystem, ActiveDirectory etc
We will be using the FileSystem Provider in this example for our functions.
So I begin with a Few PSDrives created in my PowerShell Profile As you can see below – I use 1 profile and encapsulate an if Statement to check if the host is the PowerShell ISE for ISE only Functions – like the 3 I will be showing you today.
As you can see I Have a PSDrive for all the following OneDrive, Github, Scripts, Scripts-WIP, Modules & Modules-WIP
The Important bit here is that all my Github Repo’s are actually stored in my Personal OneDrive as you can see from the above image – this means that I can Switch between Devices Very Very Quickly once things are saved 😉 – It’s probably key to point out this could be your OneDrive For Business Location as well or a Shared Drive if you are in an organisation that uses HomeDrive locations. The Possibilities are endless – save your imagination.
So from here we have our PSDrives set up and the beauty of this is that it allows very simple navigation between repo’s as you have them all centralised. In my Next Post I will be showing you how you can populate this Github Location with all your Repo’s that you have Forked and how you can ensure that all repo’s are up to date and have the latest updates pulled into them or commits pushed from them in just a few functions! So stay tuned for that!
Hopefully this will leave you with a reason to adopt PSDrives into your workflow and we can move onto the next section.
Use of Git for Source Control in this module
Quick intro – Git can be used with your own offline Repo’s – It doesn’t need to be linked to a Github Repo however this is most common and I would recommend that you use Github – You can get 5 Private Repo’s for only $7 USD a month (about £4 odd)
For more information on Git for Source Control if you are new to it I would recommend having a look at this series on PowerShellMagazine http://www.powershellmagazine.com/2015/07/13/git-for-it-professionals-getting-started-2/ – that was how I got started and also have a play with the “Learn Git in your Browser” on http://try.github.com/ – it’s definitely a useful starting point and will help you out in your future endeavours.
So the Key Git commands used in this module are
- Git add – Simply adds files to be watched under the git version control system
- Git commit – commits a version change to the repository location for the files
Other Key Git commands
- git push – pushes new commits to the remote repository (this could be hosted on Github)
- git pull – Pulls changes from the remote repostitory (this could be hosted on Github)
- git clone – clones the remote repository to your own machine (this could be hosted on Github)
So that’s the key commands out of the way but why and when will we want to use them or in our case not think about using them.
The Functions used in this Module
For me I’m a bit data-centric (aka a data hoarder) – I prefer to have too much data than not enough. So to cover this I wanted a way to Auto Commit Any changes to Scripts and Modules every time I saved them
So this is where creating this module came in – and the functions contained within.
I have created 3 Core functions
- Save-CurrentISEFile -Saves Current File that is Open in ISE whether it has been previously Saved or not
- Save-AllNamedFiles – Saves all Files that have previously been saved
- Save-AllUnnamedFiles – Saves All files that have not been previously save
And also 2 helper Functions
- Request-YesOrNo (amended from the one included in SPPS – thanks to @Jpaarhuis)
- Get-CustomCommitMessage – basic VB popup box for custom commit message
Now I must note that currently this is only compatible with v4 and above though that can change – if I get enough time and requests to do so – though you could always add this in with your own updates to the module.
So let’s look at the Process to be used with the following functions.
Imagine we are creating a script called Get-UptimeInfo – we could easily create this and then save using the default handlers in ISE however there are some issues that I’ve found
- File path defaults to the last saved location – Example being you are working on a script in C:\MyAwesomeScript then when you click Save it will save it there and for each time you reopen ISE it will default there – Not Ideal
- I like things Centralised – that way I know where things are!
So to Overcome this we put at the beginning of the script the following #Script#Get-UptimeInfo# – this then tells the Save-CurrentISEFile or the Save-UnnamedFiles functions that we want to create a PS1 file called Get-UptimeInfo in the Scripts-WIP PSDrive location
This would look like the below before running either Function
And the We can run either function in the Command Pane like any other function
Oooh – look at that the file has saved and named Get-UptimeInfo – it is a ps1 file and we are being prompted about whether we want to add a Custom Commit Message – So we’ll click Yes and see what we get
Here we get a popup box, (currently uses VB for this but it works and is a few lines) asking us to provide our commit message – I’ll Add the Commit Message as “Testing PSISE_Addons Save-CurrentISEFile Function”
The Result can be seen below – note there is a Get-UptimeInfo.tests.ps1 file been created as well – This is set by the what you include in your profile as suggested in the PSISE_Addons.psm1 file
If we wanted to do the Same with Modules then it would be something like this #Module#FindSystemInfo# and that would tell Save-CurrentISEFile or the Save-UnnamedFiles functions that we want to create a folder in the Modules-WIP PSdrive location called FindSystemInfo and in there we want to save a PSM1 file called FindSystemInfo whilst also creating a compliant psd1 file for the Gallery & also Creating a FindSystemInfo.tests.ps1 file containing some default Pester tests
When we run the Save-CurrentISEFile function we get the Same as before
Again we will Click Yes here and in the next popup we will add the message “Adding New Module FindSystemInfo” and we can see this has happened below
But we can see here that there are 3 files added – a PSD1, a PSM1 and a tests.ps1 file have all been added to a New Folder based on the Module name FindSystemInfo – but we didn’t specify these. That’s because the Functions Save-CurrentISEFile & Save-AllUnnamedFiles will do the hard work for us and create a fully compliant with the PowerShell Gallery ps1d file and also a default Pester test as long as you have them specified in your profile. BONUS – I provide you sample versions of both of these with the module. How generous is that!
But the most important thing is being able to not have to call the actual functions but using simple keyboard combinations so as part of the ISE_Cew.psm1 file there is a sample part at the bottom to add into your PowerShell Profiles – again another easy freebie!
So you can now download this from the PowerShell Gallery using Install-Module ISE_Cew – so go and get it and give me some feed back via the GitHub Repo – https://github.com/kilasuit/ISE_Cew/