PowerShellGet & waldo’s PowerShell Modules

Since Microsoft Dynamics NAV 2017, the VM images that Microsoft (Freddy) makes available on Azure, contains the PowerShellGet module. Freddy mentioned that in one of his blogposts about the Azure images. For me this was quite new, but sounded interesting, so I looked into it.

What is “PowerShellGet”?

Well, PowerShellGet is a module that is now by default present on Windows 10 (and I assume also on Windows Server 2016 – which is by the way the new OS for all the upcoming demo environments!) that makes it much easier for you to find, download and install PowerShell modules that are made available in the PowerShell Gallery: https://www.powershellgallery.com/ . If PowerShellGet not available on your OS, you can download it from that web page.

So, when you installed the module, you get PowerShell CmdLets that can search for PowerShell modules, install them, load them, and make them available. So in a way, from within a script, you can make available PowerShell modules.

The thing really makes me think of the way we installed stuff on Linux many years ago, with the apt-get command.

I made my PowerShell modules available on the PowerShell Gallery

Which you might have expected – hence the title :-). You can find them easily when you search for “waldo” on the PowerShell Gallery. But that’s not really how you should use it. You have to use it from PowerShell (in my opinion).

Let’s show that in this small walkthrough

I just created myself an image of The NAV Development Tools Preview – January Update (oh yes, there is a new update on the New DEV tools! :-) ). But as said, this should work on any Win10 or WinServer2016 – or any system where you installed the PowerShellGet module.

Just open PowerShell as an administrator (since you’re installing modules, it seems like a good idea to do that as admin…). Let’s first search for my modules from within PowerShell:

Find-Module | where Author -eq waldo

The “Find-Module” is part of the “PowerShellGet” module, which is going to search for modules in the gallery. So in this case, I will search of all my modules in the gallery.

So if we execute this:

Find-Module | where Author -eq waldo | Install-Module

with the “install-module” (also part of the “PowerShellGet” module), it will “install all waldo’s modules :-)”. So when you get this:

You simply say “yes to all”, since you trust me (IF you trust me :-)). It will download and install the modules. You’ll find the downloaded files in “C:\Program Files\WindowsPowerShell\Modules”.

Since it will just have downloaded them, you need to still import them like you have to import any module before you can use them in your runspace .. So therefore:

Import-Module Cloud.Ready.Software.NAV

With this line, you can show all the functions that you have just made available by downloading the modules from the PowerShell Gallery.

get-command -Module Cloud.Ready.Software.*

So, let’s see if it works

Ok, let’s try some of my functions on this Azure image.

In order to do that, we will first import the standard NAV commandlets. And I made that somewhat easier with this function:

Import-NAVModules

This function will put all the commands that are necessary to load the default NAV CmdLets to your clipboard. Simply past and execute. I haven’t managed to import the module in the global context from within the function (seems like a bug in PowerShell) – but this did the trick quite nicely as well :-). So paste the content of the clipboard in the shell, and execute!

Now, let’s set up a new sandbox environment (database and NST) by simply copy everything from the “NAV” instance:

Get-NAVServerInstance -ServerInstance 'NAV' | Copy-NAVEnvironment -ToServerInstance sandbox

And start the development environment:

Start-NAVIdeClient sandbox

This is how I always build my development environments on dev images – local or in the cloud. To remove, you can simply do:

Remove-NAVEnvironment -ServerInstance sandbox

Be careful with that one. It’s removing the database and server instance – so be sure you have a backup when needed :-).

What if you have updated your modules?

Well, you can imagine there is a PowerShellGet function for that as well: Update-Module. You can simply do:

Find-Module | where author -eq waldo | Update-Module

This PowerShellGet-module is awesome! :-). Thanks, Freddy, for introducing me :-).

Related
Recommended