C# and PowerShell Scripts Samples

This article provides a summary of the PowerShell and C# Sample scripts mentioned throughout the Hosting Guidance. They can be used for deployment, configuration, migration, and provisioning tasks.

by Walter Oliver

Deployment Scripts -Shared Hosting

  1. Web Server

    These PowerShell scripts will help deploy and configured the Web Server. Notice that these scripts were written and tested using PS 2.0:

    • runPKGMGT_IIS.PS1: runs the "pkgmgt.exe" command tool to install IIS 7.0 with the recommended modules for shared hosting, see Installing the Web Server Role.

      • The script uses the file "unattended.xml" as its input configuration data, it specifies the IIS modules to install.
      • Required: Unattend.xml requires that the version number be set to the current Product version of Windows i.e. version="6.0.6000.16386". This can be found by Right Clicking C:\Windows\RegEdit.exe and choosing the details tab. PkgMgr requires this parameter so that it can properly install all desired components
    • Install_IISSharedHosting.PS1: runs all the configuration scripts in accordance to the Shared Hosting guidelines, see Configuring IIS 7.0 for Shared Hosting.

      • The script uses the IISSettings.xml file as input configuration data. It contains settings for Dynamic Compression and Idle Threshold
      • The Default File setting for the server is controlled through Default_File_Config.xml. It contains a list of all Default file to be added or removed. Create as many entries as you'd like. Set activity="ADD" to add or "REMOVE" to remove.
    • Enable32bitModeWorkerProcess.PS1: enables the 32-bit mode worker processes in 64 bit machines.
    • AppPoolIdentAsAnonymousUser.PS1: sets the Application Pool Identity as Anonymous User.
    • AddRemoveDefaultDoc.PS1: adds or removes Default Documents based on the Default_Files_Config.XML input file specification.
    • ConfigureDynamicIdleThreshold.PS1: sets dynamicIdleThreshold configuration property.
    • ConfigureDynamicCompression.PS1: sets the Dynamic Compression properties.
    • HTTPResponseCache.PS1: shows the HTTP Response Cache.
    • Coming soon: Setting ASP.NET medium trust.
  2. File Server

    These PowerShell scripts will help deploy and configured the File Server. Notice that these scripts were written and tested using PS 2.0:

    • InstallFileServer.PS1: runs all the scripts to install and configure the FileServer Role.

      • Files: FolderPermissions.xml, SharePermissions.xml, DirectoryQuotas_Settings.xml are populated with sample settings. each should be configured pre-deployment as these settings will vary on a per deployment.
      • DirectoryQuotas_Settings.xml: Allows you to define as many folders quotas as necessary.
      • FolderPermissions.xml: Allows you to define as many Folders as necessary, and each folder may have 0, 1 or many permissions.
      • SharePermissions.xml: Allows you to define as many shares as necessary (will error if the new share does not correspond to an existing folder). Each share may have 0, 1, or many permissions.
    • runPKGMGR_FileServer: runs the "pkgmgt.exe" command tool to install the File Server role. The script uses the file "unattended.xml" as its input parameter, it specifies the modules to install.
    • DirectoryQuota.PS1: sets the directory quota, see Directory Quotas.
    • Folder_Shares_Permissions.PS1: sets the folder permissions in accordance to the specification in the "SharePermissions.XML" input file. See Share and NTFS Permissions.

Provisioning and Managing

  1. Provisioning Sample in C# is a set of C# samples to perform several common provisioning tasks, See details in the Provisioning Sample in C# article.
  2. Hosting Services Sample is an extensive C# code sample for provisioning Sites, User accounts, SQL db, and others. See details in the Hosting Services Code Sample article.
  3. Code Samples and Scripts provides sample code snippets for creating IIS 7.0 Sites and Configuration tasks.
  4. [IIS Sites Provisioning PowerShell Scripts](https://www.iis.net/community/files/hosting/ProvisioningScripts 4-7-2008.zip "IIS Sites PowerShell Scripts"). These are 6 PowerShell Scripts to help you automate the provisioning of AppPools, Sites, Applications, Virtual Directories, and Bindings. They use the Microsoft.Web.Administration managed code namespace interfaces to provision these objects. Here is an example for each of them:

    4.1. To create any number of AppPools, Sites, Applications, Virtual Directories, and Bindings arranged in accordance to a configuration data file use Sample_AppPool_Site_AppCreation. This script calls all the others to create each object in accordance to the configuration data found in the ProvisioningConfig.xml XML file.



    Example of a ProvisioningConfig.xml XML file:

    <Site Name="DAP1Site2" PhysicalPath="C:\Content\DAP1Site2">
    <Application PhysicalPath="C:\Content\DAP1Site2\App1" RelativePath="/App1">
    <VirtualDirectory PhysicalPath="C:\Content\Logs" RelativePath="/App1/VDir"/>
    <Application PhysicalPath="C:\Content\DAP1Site2\App2" RelativePath="/App2">
    <Binding Port="80" BindingInfo="www.DAP1Site2.com" Protocol="http"/>
    <Folder name="C:\Content\DAP1Site2" quota="50mb"> 

    4.2. To create AppPools use CreateIISAppPool PoolName username password. If you do not provide username and password it will use the current user.


    CreateIISAppPool "DemoAppPool1" "" ""

    4.3. To create a Site use CreateIISSite SiteName PhysicalPath PoolName ID.


    CreateIISSite "DAP1Site1" "C:\Content\DAP1Site1" "DemoAppPool1" 1702121

    4.4. To create an Application on a site use CreateIISApplicationOntoSite PhysicalPath RelativePath SiteName PoolName.


    CreateIISApplicationOntoSite "C:\Content\DAP1Site1\App1" "/App1" "DAP1Site1" "DemoAppPool1"

    4.5. To create a Virtual Directory for an application use CreateIISVDirOntoApplication ApplicationPhysicalPath PhysicalPath SiteName RelativePath.


    CreateIISVDirOntoApplication "C:\Content\Logs" "/App1/VDir" "DAP1Site1" "/App1"

    4.6. To create a Binding for a Site use CreateIISBindingOntoSite SiteName Port BindingInfo Protocol.


    CreateIISBindingOntoSite "DAP1Site1" 80 "www.DAP1Site2.com" "http"