You can download the source code from Codeplex, but we will create it from scratch.
In this lab, you will create a Powershell Cmdlet that will download a Sharepoint Solution package from the Sharepoint Solution store.
1. Start Visual Studio 2010 and create a new Class Library; delete the Class1.cs; name the project DownloadWsp.
2. Make sure the .Net framework 3.5 is selected and that the platform target in the project properties is Any CPU.
3. Add a reference to the Microsoft.Sharepoint assembly.
4. We need to add a reference to the System.Management.Automation assembly which is installed in the Gac, but not available in Visual Studio by default. Add a reference to System.Management (which is showing in the reference list).Save the project. Edit the project file with notepad and change "<Reference Include="System.Management" />" to "<Reference Include="System.Management.Automation" />.
5. Now we will create our custom Cmdlet : add a new public class (it is not public by default) , name it DownloadWspCmdlet and derived it from PSCmdlet; resolve the namespace:
1. Define 2 public properties WspFileName and StoreTo that will become the Cmdlet parameters by decorating them with the Parameter attribute:
2. Override the function ProcessRecord is the code that will run when the Cmdlet will be invoked:
3. Resolve the namespace Microsoft.Sharepoint.Administration.
4. We can also define the verb and the noun to use in order to invoke our Cmdlet :decorate the class with a Cmdlet attribute :
5. Now we need to create a SnapIn which is a class derived from PSSnapin :create a public class named DownloadWspSnapin, derive it from PSSnapIn and decorate it with the attribute RunInstaller:
6. RunInstaller is defined in the assembly System.Configuration.Install, ad a reference to this assembly.
7. PSSnapIn is an abstract class, implement its members:
8. Rebuild the project, add the generated dll into the Gac.
9. Run InstallUtil.exe against your dll; you will find InstallUtil.exe in the following folder :
10. Start a Powershell console and add the new snapin:
11. Test your Cmdlet by invoking Download-SPSolution in the Powershell console:
Code can be download from Codeplex.