PSST.Wsp.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<#
.Synopsis
   Unpacks SharePoint WSP files into folders.
.EXAMPLE
   Unpacks the template.wsp to the template folder.
   Invoke-WspUnpackage -WspFile template.wsp -Destination .\template
.OUTPUTS
   None
.FUNCTIONALITY
   Uses Windows expand.exe to unpack WSP cabinet file.
   Requires Windows SDK to be installed.
#>

Function Invoke-WspUnpackage {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true)]
    [System.IO.FileInfo]$WspFile,

    [Parameter(Mandatory=$true)]
    [System.IO.DirectoryInfo]$Destination
  )

  $WspFile = Get-Item $WspFile;

  $DiskDirectory = New-Item $Destination -ItemType Directory -Force;

  expand $WspFile.FullName -F:* $DiskDirectory.FullName;
}

<#
.Synopsis
   Removes named SharePoint features from unpacked WSP folder (ONet.xml file).
.EXAMPLE
   Removes the named features.
   Remove-WspFeature -Directory .\template -FeatureDescritpion ShellControl,spouiremovalpublic
.OUTPUTS
   None
#>

Function Remove-WspFeature {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true)]
    [System.IO.DirectoryInfo]$Directory,

    [Parameter(Mandatory=$true)]
    [string[]]$FeatureDescription
  )

  $DiskDirectory = Get-Item $Directory;

  $onetFileName = "$($DiskDirectory.FullName)\*WebTemplate\*\ONet.xml";

  $onetContent = (Get-Content -Path "$onetFileName") -join "`r`n";

  $FeatureDescription | ForEach-Object { $onetContent = $onetContent -replace "[\s]*<!--$_.*-->[\s]*<Feature ID=.*", ""}

  Set-Content -Path $onetFileName -Value $onetContent;
}

<#
.Synopsis
   Packs SharePoint unpacked WSP folder and files into a single WSP file.
.EXAMPLE
   Unpacks the template.wsp to the template folder.
   Invoke-WspPackage -WspFileName template.wsp -Directory .\template
.OUTPUTS
   None
.FUNCTIONALITY
   Uses Windows makecab.exe to pack WSP cabinet file.
   Requires Windows SDK to be installed.
#>

Function Invoke-WspPackage {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true)]
    $WspFileName,

    [Parameter(Mandatory=$true)]
    [System.IO.DirectoryInfo]$Directory
  )

  $DiskDirectory = Get-Item $Directory;

  $header = @"
  .OPTION EXPLICIT
  .Set CabinetNameTemplate=$WspFileName
  .Set DiskDirectoryTemplate=CDROM
  .Set CompressionType=MSZIP
  .Set MaxDiskSize=CDROM
  .Set Cabinet=on
  .Set Compress=on
  .Set DiskDirectory1=$($DiskDirectory)
"@
;

  $ddfFileName = "$WspFileName.ddf";

  Set-Content -Path $ddfFileName -Value $header;

  Get-ChildItem $DiskDirectory -File -Recurse | ForEach-Object {Add-Content -Path $ddfFileName -Value "`"$($_.FullName)`" `"$($_.FullName.Replace($DiskDirectory.FullName, [string]::Empty))`""}

  makecab.exe -f $ddfFileName
}