Summary: Microsoft Scripting Guy, Ed Wilson, talks about removing a Windows feature by using a Windows PowerShell workflow. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a cup of Sencha green tea. I happened to have some lying around and I decided I would go ahead and drink it this morning. I added a bit of fresh lemon and half a stick of cinnamon to the cup. It is quite good, if a bit understated. Normally, I would have green tea in the afternoon, but like I said, this morning I happened to have some lying around, so I thought, “Why not?” There are, in fact, several times when I say, “Why not?” Today is one of those occasions.
Create a module from a Windows PowerShell workflow
Today I want to create a Windows PowerShell workflow that will uninstall the Windows PowerShell ISE. Then I will reboot the computer, and double check that the Windows PowerShell ISE is, in fact, uninstalled. Later, I will change it so that it will also install the Windows PowerShell ISE, so don’t get too concerned. The cool thing is that there is really no difference between creaing a workflow and storing the workflow in a script (or in a module), except that if it is in a module, I can use Export-ModuleMember to export the functions. Windows PowerShell considers a workflow to be a function, and I can treat the workflow in the same manner as I treat a function. This means that I will find them hanging out on the Function drive, and I can use Get-Command to find them. The advantage is that if I store my workflow in a module, I can use Import-Module to import the workflow.
Uninstall the ISE
The first thing I want to do is to create a workflow that will uninstall the Windows PowerShell ISE. To do this, I use the Get-WindowsFeature cmdlet and retrieve the PowerShell-ISE feature. I then pipe it to Remove-WindowsFeature. This really the meat and potatoes of the operation. What makes it a workflow is that I use the Workflow keyword, and I define my tasks inside a Parallel script block. Here is that portion of the script:
Workflow Uninstall-ISE
{
Parallel {
Get-WindowsFeature PowerShell-ISE |
Remove-WindowsFeature } While I am playing around, I decide to define a sequence that will restart the computer and wait for Windows PowerShell to start up on the remote computer. After that takes place, I use the Get-WindowsFeature cmdlet to ensure the Windows PowerShell ISE is uninstalled. Here is that portion of the workflow:
Sequence {
Restart-Computer -Wait -for PowerShell
Get-WindowsFeature PowerShell-ISE
} The entire workflow is shown here:
Workflow Uninstall-ISE
{
Parallel {
Get-WindowsFeature PowerShell-ISE |
Remove-WindowsFeature }
Sequence {
Restart-Computer -Wait -for PowerShell
Get-WindowsFeature PowerShell-ISE
}
} When I write code to do or undo something, at the same time, I like to write code to reverse the process. So here is that script:
Workflow Install-ISE
{
Parallel {
Get-WindowsFeature PowerShell-ISE |
ADD-WindowsFeature }
Sequence {
Restart-Computer -Wait -for PowerShell
Get-WindowsFeature PowerShell-ISE
}
} Cool. So how hard is it to turn this into a module? Not hard at all, I add the Export-ModuleMember command at the bottom of the script and save it as a .psm1 file. Here is the Export command:
Export-ModuleMember -Function * And, that is it. I save it in a shared location because tomorrow I am going to use this module for a remote workflow to uninstall the Windows PowerShell ISE against a list of servers. That is all there is to using a workflow to uninstall a feature. Workflow Week will continue tomorrow when I will talk about remotely running the workflow. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy
0 comments