Skip to content
This repository was archived by the owner on Sep 8, 2022. It is now read-only.

Commit 06086c4

Browse files
committed
Add example for checking if something exists
1 parent cfc9945 commit 06086c4

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

examples/06-CheckExists/IIS.ps1

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -----------------------------------------------------------------------------
2+
# Setup
3+
# -----------------------------------------------------------------------------
4+
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
5+
cd $here
6+
7+
. ..\_Setup.IIS.ps1
8+
9+
mkdir "C:\Sites\Website1" -ErrorAction SilentlyContinue
10+
11+
$manager = Get-IISServerManager
12+
$manager.ApplicationPools.Add("My Pool")
13+
$manager.Sites.Add("Website1", "http", "*:8022:", "C:\Sites\Website1")
14+
$manager.Sites["Website1"].Applications.Add("/MyApp", "C:\Sites\MyApp")
15+
$manager.CommitChanges()
16+
17+
# -----------------------------------------------------------------------------
18+
# Example
19+
# -----------------------------------------------------------------------------
20+
Import-Module IISAdministration
21+
22+
$manager = Get-IISServerManager
23+
24+
# The pattern here is to get the things you want, then check if they are null
25+
26+
if ($manager.ApplicationPools["My Pool"] -eq $null) {
27+
# Application pool does not exist, create it...
28+
# ...
29+
}
30+
31+
if ($manager.Sites["Website1"] -eq $null) {
32+
# Site does not exist, create it...
33+
# ...
34+
}
35+
36+
if ($manager.Sites["Website1"].Applications["/MyApp"] -eq $null) {
37+
# App/virtual directory does not exist, create it...
38+
# ...
39+
}
40+
41+
$manager.CommitChanges()
42+
43+
# -----------------------------------------------------------------------------
44+
# Assert
45+
# -----------------------------------------------------------------------------
46+
if ($manager.ApplicationPools["My Pool"] -eq $null -or $manager.ApplicationPools["sdij18uc"] -ne $null) { Write-Error "Our logic is wrong" }
47+
if ($manager.Sites["Website1"] -eq $null -or $manager.Sites["isjdjdsoidj"] -ne $null) { Write-Error "Our logic is wrong" }
48+
if ($manager.Sites["Website1"].Applications["/MyApp"] -eq $null -or $manager.Sites["Website1"].Applications["/sdidsuidshdsh"] -ne $null) { Write-Error "Our logic is wrong" }
49+
50+
# -----------------------------------------------------------------------------
51+
# Clean up
52+
# -----------------------------------------------------------------------------
53+
54+
. ..\_Teardown.IIS.ps1
55+
56+
Remove-IISSite -Name "Website1" -Confirm:$false
57+
58+
$manager = Get-IISServerManager
59+
$manager.ApplicationPools["My Pool"].Delete()
60+
$manager.CommitChanges()

examples/06-CheckExists/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Check whether sites, virtual directories or application pools already exist
2+
3+
You'll re-deploy your application to IIS many times over the course of a project, so you can't assume the script is being run for the first time. The examples below show a pattern for checking whether a site, application, or application pool already exists before making a change.

examples/06-CheckExists/Web.ps1

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -----------------------------------------------------------------------------
2+
# Setup
3+
# -----------------------------------------------------------------------------
4+
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
5+
cd $here
6+
7+
. ..\_Setup.Web.ps1
8+
9+
mkdir "C:\Sites\Website1" -ErrorAction SilentlyContinue
10+
mkdir "C:\Sites\MyApp" -ErrorAction SilentlyContinue
11+
12+
New-Item -Path "IIS:\AppPools" -Name "My Pool" -Type AppPool
13+
New-Website -Name "Website1" -Port 80 -IPAddress "*" -HostHeader "" -PhysicalPath "C:\Sites\Website1"
14+
New-Item -Type Application -Path "IIS:\Sites\Website1\MyApp" -physicalPath "C:\Sites\MyApp"
15+
16+
# -----------------------------------------------------------------------------
17+
# Example
18+
# -----------------------------------------------------------------------------
19+
20+
Import-Module WebAdministration
21+
22+
# The pattern here is to use Test-Path with the IIS:\ drive provider
23+
24+
if ((Test-Path "IIS:\AppPools\My Pool") -eq $False) {
25+
# Application pool does not exist, create it...
26+
# ...
27+
}
28+
29+
if ((Test-Path "IIS:\Sites\Website1") -eq $False) {
30+
# Site does not exist, create it...
31+
# ...
32+
}
33+
34+
if ((Test-Path "IIS:\Sites\Website1\MyApp") -eq $False) {
35+
# App/virtual directory does not exist, create it...
36+
# ...
37+
}
38+
39+
# -----------------------------------------------------------------------------
40+
# Assert
41+
# -----------------------------------------------------------------------------
42+
43+
if ((Test-Path "IIS:\AppPools\My Pool") -eq $false -or (Test-Path "IIS:\AppPools\sijsijd") -ne $false) { Write-Error "Our logic is wrong" }
44+
if ((Test-Path "IIS:\Sites\Website1") -eq $false -or (Test-Path "IIS:\Sites\sdijdsijdd") -ne $false) { Write-Error "Our logic is wrong" }
45+
if ((Test-Path "IIS:\Sites\Website1\MyApp") -eq $false -or (Test-Path "IIS:\Sites\Website1\sdddsds") -ne $false) { Write-Error "Our logic is wrong" }
46+
47+
# -----------------------------------------------------------------------------
48+
# Clean up
49+
# -----------------------------------------------------------------------------
50+
51+
. ..\_Teardown.Web.ps1
52+
53+
Remove-Item -Path "IIS:\Sites\Website1" -Recurse -Force
54+
Remove-Item -Path "IIS:\AppPools\My Pool" -Recurse -Force

0 commit comments

Comments
 (0)