16 March 2017

How to publish your Windows PowerShell script in the PowerShell public gallery

PowerShell Logo
PowerShell is more than just a terminal console. It is developed by Microsoft allowing you to manage their products through command lines or scripts. 

One of the great advantages of belonging to Windows Management Framework (WMF) is that it allows access to:
  • Common Information Model (CMI);
  • Component Object Model (COM);
  • Framework .NET;
  • Server Manager CIM Provider;
  • Server Manager WMI provider;
  • Software Inventory Logging (SIL);
  • Windows Management Infrastructure (WMI);
  • Windows PowerShell;
  • Windows PowerShell Desired State Configuration (DSC);
  • Windows PowerShell Integrated Scripting Environment (ISE);
  • Windows PowerShell Web Services (Management OData IIS Extension);
  • Windows Remote Management (WinRM).

But does it have so many advantages?
Yes, in addition to being able to use the .NET framework you can create scripts to do autonomous jobs that can be executed by parameters, which allows the script to be reusable and updatable.


In PowerShell command lines are called cmdlets and are more complex commands. A cmdlet may even be a script that invokes an API call or other cmdlets. You can see the Cmdlet overview at https://msdn.microsoft.com/pt-br/library/ms714395(v=vs.85).aspx .


It is important to know that new cmdlets or features are only available in the latest versions of the operating system. The latest stable version of WMF is 5.1 which includes:
  • New cmdlets: local users and groups; Get-ComputerInfo;
  • PowerShellGet improvements include enforcing signed modules, and installing JEA modules;
  • PackageManagement added support for Containers, CBS Setup, EXE-based setup, CAB packages;
  • Debugging improvements for DSC and PowerShell classes;
  • Security enhancements including enforcement of catalog-signed modules coming from the Pull Server and when using PowerShellGet cmdlets;
  • Responses to a number of user requests and issues;
WMF Compatibility



WMF 2.0WMF 3.0WMF 4.0WMF 5.0WMF 5.1
Windows XP YesNoNoNoNo
Windows Server 2003YesNoNoNoNo
Windows VistaYesNoNoNoNo
Windows 7 SP1YesYesYesYesYes
Windows Server 2008 SP2Yes
Yes
(Expect IA64)
NoNoNo
Windows Server 2008 R2 SP1Yes
Yes
(Expect IA64)
YesYesYes
Windows 8YesYesYesYesYes
Windows 8.1YesYesYesYesYes
Windows Server 2012 SP1YesYesYesYesYes
Windows Server 2012 R2YesYesYesYesYes
Windows 10YesYesYesYesYes
Windows Server 2016YesYesYesYesYes


As of August 18, 2016 Windows PowerShell became Open-Source which means you can run on MacOS and some Linux distributions. The PowerShell team is using GitHub so anyone can contribute with the project at https://github.com/powershell .

PowerShell Gallery
The PowerShell Gallery is the central repository for PowerShell content. New PowerShell or Desired State Configuration (DSC) commands are present. 

Before you start you must register in https://www.powershellgallery.com/ with a Microsoft account that will be used for publishing.

PowerShell Gallery Login



By accessing your account you can verify that you have an API key and it is this key that is used for submission.
 
PowerShell Gallery - My Account


Script Creation
Before starting the script you need to create the block that includes the script information with the cmdlet 'New-ScriptFileInfo'.  The block will contain the version, GUID that is automatically generated, author, required information and script dependencies.

If you already have a script created add the PSScriptInfo body at the beginning of the script.

cmdlet New-ScriptFileInfo

 
Testing the script
The cmdlet 'Test-ScriptFileInfo' checks the comment block if it has an error it will tell you where it is located or how to fix it.

 
cmdlet Test-ScriptFileInfo


As you can see in the image, no error was displayed and can be sent. The following image has a purposeful error in which a variable is not defined and can not be sent until the error is corrected.



Script publishing
With the tested script you can now publish it to publish you need the API key that you can query in your account.  To publish, use the Publish-Script cmnlet.

Publish-Script -Path <localização do script> -NuGetApiKey <chave API>

An important rule is that the name  of the script must be unique if you try to publish a script with the same ID will return an error and is required to rename the script. 


cmnlet Publish-Script


Manage the script in the gallery
In your Manage My Items account you can manage all submitted modules or scripts. 


You can edit the contents of the script.




The gallery also has the chance to delete the script but is not yet supported by the gallery what it can do is remove the script from the searches.



With successful publishing the script is visible and searchable in the Web Gallery comes as in PowerShell. The gallery hypothesis to save the script without installing with the 'Save-Script' cmldet. If someone wants to install you can use the cmdlet 'Install-Script'.




There is no restriction for publication but you have to be careful that the script or module you want to publish does not contain any sensitive information such as logins, passwords, keys for database links.

Even if the script has passed the tests it is always important to review and test the script before publishing. If it is possible to do it on another machine. 

No comments:

Post a Comment