tools.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
enum actions {
  Set
  Test
}

enum ensures {
  Present
  Absent
}

function Module {
  param (
    [actions]$Action,
    [string]$Name,
    [System.Version]$Version,
    [string]$ProviderName,
    [ensures]$Ensure
  )
  switch ($Action) {
    Test {
      $Module = Get-Module -ListAvailable -Name $Name
      if ((! $Module) -or ($Module.version -lt $Version)) {$return = $false}
      else {$return = $true}
      switch ($Ensure) {
        Present {
          $return
        }
        Absent {
          ! $return
        }
      }
    }
    Set {
      switch ($Ensure) {
        Present {
          $params = @{
            Repository = $ProviderName
            Name = $Name
          }
          if ($Version) {
            $params.Add('RequiredVersion',$Version)
          }
          Find-Module @params| Install-Module -Force -Scope AllUsers
        }
        Absent {
          Uninstall-Module -Name $Name -Force
        }
      }
    }
  }
}

function Package {
  param (
    [actions]$Action,
    [string]$Name,
    [version]$Version,
    [string]$ProviderName,
    [ensures]$Ensure
  )
  switch ($Action) {
    Test {
      $pkg = Get-Package -Name $Name -ErrorAction SilentlyContinue
      if ((! $pkg) -or ($pkg.version -lt $Version)) {$return = $false}
      else {$return = $true}
      switch ($Ensure) {
        Present {
          $return
        }
        Absent {
          ! $return
        }
      }
    }
    Set {
      switch ($Ensure) {
        Present {
          Find-Package -Source $ProviderName -Name $Name -RequiredVersion $Version | Install-Package -Force -Scope AllUsers
        }
        Absent {
          Uninstall-Package -Name $Name -Force
        }
      }
    }
  }
}

function website {
  param (
    [string]$Name,
    [string]$Path,
    [int]$Port,
    [switch]$Ssl,
    [actions]$Action
  )
  switch ($Action)
  {
    'test' {
      if (get-website -name $Name) {
        return $true
      } 
      else {
        return $false
      }
    }
    'set' {
      switch ($Ssl) {
        $True {
          New-Website -Name $Name -PhysicalPath $Path -Port $Port -Ssl
        }
        $False {
          New-Website -Name $Name -PhysicalPath $Path -Port $Port
        }
      }
      
    }
  }
}

function IIS {
  param (
    [validateset('test','set')]
    [actions]$Action
  )
  switch ($Action)
  {
    'test' {
      (get-windowsfeature -name Web-Server).installed
    }
    'set' {
      Add-WindowsFeature -Name Web-Server
    }
  }
}

function ASP {
  param (
      [validateset('test','set')]
      [actions]$Action
    )
  switch ($Action) {
      'test' {
          (get-windowsfeature -name Web-Asp-Net45).installed
        }
      'set' {
          Add-WindowsFeature -Name Web-Asp-Net45
        }
    }
}

function Zip {
  param (
    [validateset('test','set')]
    [actions]$Action,
    [string]$Path = "$env:Systemdrive\inetpub\wwwroot"
  )
  $names = @(
    'App_Readme'
    'bin'
    'DataServices'
    'obj'
    'Packages'
    'Properties'
    'Default.aspx'
    'favicon.ico'
    'LocalNuGetFeed.csproj'
    'LocalNuGetFeed.csproj.user'
    'packages.config'
    'Web.config'
    'Web.Release.config'
  )
  switch ($Action) {
    'test' {
        $return = $names | foreach {
            Test-Path $Path\$_
          }
        if ($return -contains $false) {$false}
        else {$true}
      }
    'set' {
        Expand-Archive -Path $PSScriptRoot\nuget.zip -DestinationPath $Path -Force
      }
  }
}

function pkg {
  param (
    [validateset('test','set')]
    [actions]$Action,
    [string]$path
  )
  switch ($Action) {
    'test' {
      Test-Path -Path $path
    }
    'set' {
      $null = New-Item -Path $path -ItemType Directory -Force 
    }
  }
}

function webconf {
  param (
    [validateset('test','set')]
    [actions]$Action,
    [string]$Path =  "$env:SystemDrive\inetpub\wwwroot\Web.config",
    [string]$Conf
  )
  switch ($Action) {
    'test' {
      $temp = $Path + ".tmp"
      $Conf | Set-Content -Path $temp
      $control = Get-Content $temp -Raw
      Remove-Item -Path $temp -Force
      if (Compare-Object -ReferenceObject $control -DifferenceObject (Get-Content $Path -Raw))
        {
          $false
        }
      else {$true}
    }
    'set' {
      $Conf | Set-Content -Path $Path
    }
  }
}

function webconfvar {
  param (
    [bool]$AllowNugetPackagePush,
    [bool]$AllowPackageOverwrite,
    [string]$PackageSource,
    [string]$APIKey
  )
  $ExecutionContext.InvokeCommand.ExpandString($webconf)
}

function provider {
  param (
    [validateset('test','set')]
    [actions]$Action,
    [string]$Name,
    [string]$PublisherURI,
    [string]$SourceURI,
    [string]$Type,
    [string]$Ensure
  )
  switch ($Action) {
    'test' {
      Write-Verbose "Checking for provider: $Name"
      if (Get-PSRepository -Name $Name -ErrorAction SilentlyContinue)
      {
        $return = $true
      }
      else {$return = $false}
      switch ($Ensure) {
        Present {
          return $return
        }
        Absent {
          return (! $return)
        }
      }
    }
    'set' {
      switch ($Ensure) {
        Present {
          $null = Register-PackageSource -Name $Name -Location $SourceURI -ProviderName PowerShellGet -Force -ForceBootstrap -WarningAction Ignore -ErrorAction Ignore
          $null = Set-PSRepository -Name $Name -SourceLocation $SourceURI -PublishLocation $PublisherURI -InstallationPolicy $Type
        }
        Absent {
          $null = Unregister-PackageSource -Name $Name -Force
        }
      }
      
    }
  }
}

function package_provider {
  param (
    [validateset('test','set')]
    [actions]$Action,
    [string]$Name,
    [string]$SourceURI,
    [string]$ProviderName,
    [string]$Ensure
  )
  switch ($Action) {
    'test' {
      Write-Verbose "Checking for provider: $Name"
      if (Get-PackageSource -Name $Name -ErrorAction SilentlyContinue)
      {
        $return = $true
      }
      else {$return = $false}
      switch ($Ensure) {
        Present {
          return $return
        }
        Absent {
          return (! $return)
        }
      }
      
    }
    'set' {
      $Null = Find-PackageProvider -Name Nuget -ForceBootstrap -Force
      switch ($Ensure) {
        Present {
          $null = Register-PackageSource -Name $Name -Location $SourceURI -ProviderName $ProviderName -Force -ForceBootstrap -Trusted
        }
        Absent {
          $null = Unregister-PackageSource -Name $Name -Force
        }
      }
    }
  }
}


$webconf = @'
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
      <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
      <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5" maxRequestLength="31457280"/>
  <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </httpModules></system.web>
<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler"/>
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler"/>
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler"/>
    </modules>
  <staticContent>
      <mimeMap fileExtension=".nupkg" mimeType="application/zip"/>
    </staticContent></system.webServer><elmah>
    <!--
        See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for
        more information on remote access and securing ELMAH.
    -->
    <security allowRemoteAccess="false"/>
  <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data"/></elmah><location path="elmah.axd" inheritInChildApplications="false">
    <system.web>
      <httpHandlers>
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/>
      </httpHandlers>
      <!--
        See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for
        more information on using ASP.NET authorization securing ELMAH.
 
      <authorization>
        <allow roles="admin" />
        <deny users="*" />
      </authorization>
      -->
    </system.web>
    <system.webServer>
      <handlers>
        <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode"/>
      </handlers>
    </system.webServer>
  </location><appSettings>
    <!--
            Determines if an Api Key is required to push\delete packages from the server.
    -->
    <add key="requireApiKey" value="$AllowNugetPackagePush"/>
    <!--
            Set the value here to allow people to push/delete packages from the server.
            NOTE: This is a shared key (password) for all users.
    -->
    <add key="apiKey" value="$APIKey"/>
    <!--
            Change the path to the packages folder. Default is ~/Packages.
            This can be a virtual or physical path.
        -->
    <add key="packagesPath" value="$PackageSource"/>
    <!--
            Set allowOverrideExistingPackageOnPush to false if attempts to upload a package that already exists
            (same id and same version) should fail.
    -->
    <add key="allowOverrideExistingPackageOnPush" value="$AllowPackageOverwrite"/>
    <!--
            Set enableDelisting to true to enable delist instead of delete as a result of a "nuget delete" command.
            - delete: package is deleted from the repository's local filesystem.
            - delist:
              - "nuget delete": the "hidden" file attribute of the corresponding nupkg on the repository local filesystem is turned on instead of deleting the file.
              - "nuget list" skips delisted packages, i.e. those that have the hidden attribute set on their nupkg.
              - "nuget install packageid -version version" command will succeed for both listed and delisted packages.
                 e.g. delisted packages can still be downloaded by clients that explicitly specify their version.
    -->
    <add key="enableDelisting" value="false"/>
    <!--
      Set enableFrameworkFiltering to true to enable filtering packages by their supported frameworks during search.
    -->
    <add key="enableFrameworkFiltering" value="false"/>
  </appSettings><system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel></configuration>
'@