Agile/Scrum blog

Onderwerp: PowerShell

Creating a unit testing framework for PowerShell - Part 6

Prajeesh Prathap Door: Prajeesh Prathap,  21-01-2015
Onderwerp: PowerShell  unit testing  TDD  

The authorization manager helps control the execution of commands for the runspace. When you try to execute a PowerShell script from C#, and haven’t changed PowerShell’s default execution policy, the scripts that are executed under the execution policy set on the machine. If you want the tests executed from C# to bypass the default security policy, then you need to either use a null AuthorizationManager implementation for the runspace or create a custom implementation of the AuthorizationManager and override the policy based on any condition you have. Deriving from the AuthorizationManager class allows you to override the ShouldRun method and add the logic specific to your needs like set up a reason parameter with a custom execption with proper explanation and  details on why this command was blocked etc. Read more..

Creating a unit testing framework for PowerShell - Part 5

Prajeesh Prathap Door: Prajeesh Prathap,  21-01-2015
Onderwerp: PowerShell  unit testing  TDD  

PowerShell cmdlets and modules can report two kinds or errors (Terminating and non-terminating). Terminating errors are errors that cause the pipeline to be terminated immediately, or errors that occur when there is no reason to continue processing. Nonterminating errors are those errors that report a current error condition, but the cmdlet can continue to process input objects. With nonterminating errors, the user is  typically notified of the problem, but the cmdlet continues to process the next input object. Terminating errors are reported by throwing  exceptions or by calling the ThrowTerminatingError method, while non-terminating errors are reported by calling the Write-Error method that in turn sends an error record to the error stream. Read more..

Creating a unit testing framework for PowerShell - Part 4

Prajeesh Prathap Door: Prajeesh Prathap,  18-01-2015
Onderwerp: PowerShell  unit testing  TDD  

Before we proceed into how we can stub out commands in our test framework, we’ll see how the AddScript method works and how to use it to execute a script block in powershell. The AddScript method adds a script to the end of the pipeline of the PowerShell object and can be invoked by the Invoke method. We’ll use this method to add our dummy function as a script object to the pipeline so that when this command is called later from a function, it’s going to call the dummy function that was added using the AddScript method. Read mode..

Creating a unit testing framework for PowerShell - Part 3

Prajeesh Prathap Door: Prajeesh Prathap,  17-01-2015
Onderwerp: PowerShell  unit testing  TDD  

The windows PowerShell engine can be hosted in the System.Management.Automation.PowerShell class. Using this class you can create, execute and manage commands in a Runspace. We’ll use these features of the PowerShell class to load and execute our Modules and interact with the PowerShell engine whenever needed in our unit test framework. While creating the PowerShell host for our test framework, it’s good to define the Runspace that is responsible for the operating environment for command pipelines. In our framework I preferred to use the constrained runspace which will allow us to restrict the programming elements that can be applied by the user. Read more..

Creating a unit testing framework for PowerShell - Part 2

Prajeesh Prathap Door: Prajeesh Prathap,  17-01-2015
Onderwerp: PowerShell  unit testing  TDD  

Before we dig into the testing process, one of the main activities to be done as part of the Arrange phase is to populate or ModuleObject properties with the CommandInfo object that is used at the Acting phase of our test framework. For iterating through all the properties and reading the attributes using reflection and setting the values of the ModuleObject implementation, we’ll make use of the Visitor pattern. Read more..

Creating a unit testing framework for PowerShell - Part 1

Prajeesh Prathap Door: Prajeesh Prathap,  17-01-2015
Onderwerp: PowerShell  unit testing  TDD  

Recently I started working on a light weight unit testing framework for my PowerShell modules. There are a lot of testing frameworks for  PowerShell that can be executed as script files from a PS host, but not many allows you to integrate with VSTests and write test methods and classes in C#.
The following posts in this series is about how you can create a unit testing framework for Windows PowerShell and use it.
 
Being a big fan of the Page objects pattern and have seen the benefits of how easily you can model a page and reduce the amount of duplicate code when creating UI tests for websites, I wanted to do something similar modules for PowerShell also. So when I started writing the framework, one of the main considerations was to simplify my testing process by modelling a PowerShell module in code. Read more..

Writing Help for DSC Resources

Prajeesh Prathap Door: Prajeesh Prathap,  02-01-2015
Onderwerp: DSC  Desired State Configuration  PowerShell  

Windows PowerShell modules can include Help topics about the module and about the module members, such as cmdlets, providers, functions and scripts. The Get-Help cmdlet displays the module Help topics in the same format as it displays Help for other Windows PowerShell items,  and users use standard Get-Help commands to get the Help topics. Using the help system you can include details like syntax overviews, detailed explanations, examples etc. for your PowerShell modules or DSC resources.

For DSC you can make use of the conceptual ("about") Help topic to describe the module and its members and to explain how the members can be used together to perform tasks. Conceptual Help topics are text files with Unicode (UTF-8) encoding. The file name must use the about_.help.txt format, such as about_xSampleDSCResource1.help.txt. Read more..

DSC – Protecting the credentials in the MOF files

Prajeesh Prathap Door: Prajeesh Prathap,  01-01-2015
Onderwerp: DSC  Desired State Configuration  PowerShell  

When you dot source the DSC configuration file that contains a resource having a PSCredential object as parameter with the PSDscAllowPlainTextPassword option set in the configuration data, the resulting MOF file will have the password saved as plain text in a human readable format. This results in compromising the credentials from the MOF files. If you want to secure your credentials in the MOF file, then you need to use the LCM to check the credentials of the user, encrypt the credentials using the certificate details mentioned in the configuration data and then later decrypt it using the settings of the LCM.

To secure your MOF files, you’ll need to first create a certificate with a private key to decrypt the data in the MOF files and then use that information to create the MOF files. Read more..

DSC - Extending to Azure virtual machines

Prajeesh Prathap Door: Prajeesh Prathap,  25-12-2014
Onderwerp: DSC  Windows Azure  PowerShell  

Being a fan of PowerShell DSC, and have seen the ease and benefits of DSC, I wanted to test the extensions on my Azure infrastructure. To use DSC on your Azure VM’s you need to install the Azure PowerShell SDK on the VM. Once you have installed the SDK, you can create an authentication for the subscription by using the Azure AD. 

DSC – Configuration delivery modes

Prajeesh Prathap Door: Prajeesh Prathap,  30-11-2014
Onderwerp: PowerShell  DSC  Desired State Configuration  CD  

In the configuration management life cycle in DSC, configuration delivery plays a major role. Once a configuration is authored, a delivery mode helps to enact the configuration on the target systems. These modes dictate how the configuration is enforced and corrected, as required. DSC supports two types of delivery modes: Push and Pull.

Desired State Configuration - Elements of the configuration

Prajeesh Prathap Door: Prajeesh Prathap,  17-11-2014
Onderwerp: DSC  Desired State Configuration  PowerShell  

DSC is a feature built into the Windows Operating System. It’s based on the standards like CIMS and WS-Management remote management offered by the operating system. With DSC you can move to a way of configuration management where you can create a script that defines how the state of the server should be instead of defining how to make the server in the desired state. That means, DSC is more of a declarative syntax than an imperative one. This makes DSC scripts/ configurations easy to understand and maintain by the operations.

Desired State Configuration - Introduction to CIM

Prajeesh Prathap Door: Prajeesh Prathap,  03-11-2014
Onderwerp: CIM  Common Information Model  Desired state configruation  DSC  PowerShell  Windows Remote Management  

PowerShell 4.0 introduces desired state configuration (DSC), a powerful new feature that makes it easier than ever to manage your Windows infrastructure, whether on premise or in the cloud. DSC is built on the Common Information Model (CIM) standard developed by the Desktop Management Task Force (DMTF) and uses Windows Remote Management (WinRM) technology as a communication mechanism. In this post we will look at what Windows Remote Management is and how it is used in the Windows OS world to enable standards-based management. In the upcoming posts I'll explain how CIM cmdlets in PowerShell use WinRM to work with management data from remote systems and the role of DSC in managing remote machines.

Continuous Deployment - Remote execution of PowerShell scripts from your build process

Prajeesh Prathap Door: Prajeesh Prathap,  29-09-2014
Onderwerp: CD  PowerShell  TFS  architecture  

Including Windows PowerShell script as part of your build and deployment process, brings you the flexibility of easily and effectively customize your packaging and deployment process. With the proper combination of environment configuration files (XML) and PowerShell scripts you can achieve the impossible. This post will show you how to run Windows PowerShell scripts remotely from a TFS build process.


Using CredSSP for second-hop remoting
One common issue with PowerShell remoting is the “double hop” problem. When the scripts are executed remotely on a Server A and then it tries to connect from Server A to Server B, the second connection fails to send the credentials to that server. As a result the second server fails to authenticate the request and rejects the connection. To get around this issue you need to use the CredSSP authentication mechanism in PowerShell.

CD tips - Create a custom activity to execute PowerShell scripts in the TFS build process

Prajeesh Prathap Door: Prajeesh Prathap,  24-09-2014
Onderwerp: Continous Delivery  PowerShell  CD  TFS  

PowerShell is a powerful scripting language which can be used to customize the behavior of your package deployments. With PowerShell you can add powerful scripting to your build to for example execute a deployment process.
With TFS 2013 hooking up a PowerShell script in the build process is provided out of the box. There are pre- and post-build as well as pre- and post-test hooks. These make customizing build a whole lot easier.

Using the SPImport events to control SharePoint content import process

Prajeesh Prathap Door: Prajeesh Prathap,  18-09-2014
Onderwerp: SharePoint  PowerShell  SPImport  

The SPImport class in the Microsoft.SharePoint.Deployment namespace participates with other classes in the Deployment namespace to support importing, publishing, and migrating Windows SharePoint content, as well as supporting restore capabilities of content.

You can initiate an import operation by first initializing an instance of the Microsoft.SharePoint.Deployment.SPImportSettings class with the required import settings, and then passing the SPImportSettings object to the constructor of SPImport class; you then call the SPImport.Run method. The SPImport type exposes events that can be used to control the process of import. You can utilize the event model in PowerShell to register the events and then make use of this information to control the complete process of importing and exporting SharePoint content. Below is the PowerShell snippet to use the Error event and use the data to fail the execution process after an Import is performed.

Exporting and importing content types using PowerShell

Prajeesh Prathap Door: Prajeesh Prathap,  18-09-2014
Onderwerp: SharePoint  PowerShell  

SharePoint stores the definition schema of content types in an XML file that creates a content type in the element manifest file of a Feature. You can use the information present in the SchemaXML of a content type to migrate it between site collections using PowerShell.