Powershell

Table of Contents

5.11 Web Development

PowerShell is a powerfull tool for web development in Windows hosted websites such as IIS.

By using PowreShell you can update multiple web files such as css, html, js, php and more and update/remove lines of code on the fly.

How it works is by using PowerShell to reiterate through every file in a web directory and adding/removing/updating lines of code. A matching operator or method/function is used to find strings of text into arrays or litteral strings then place in variables for manipulation. The Variables are then output to set content to the list of files to update multiple files instantly.

The types of strong matching methods in PowerShell are below.

The -replace operator uses Regular Expression ('regex') matching rules. Regular expressions contain many special characters that don't behave in a 'normal' way, and so it's quite likely that the search is just failing. There are two easy solutions to this:

a) Use the Replace method of the [string] class, which uses plain matching

b) Use the Escape method of the [regex] class to create a search string that will work correctly with the -replace operator

Using a double-quoted string (e.g. "this") that needs to contain other double-quote characters. There are also two simple solutions to this:

c) Use the Powershell backtick (`) escape character to mark the internal double-quotes as being part of the string.

d) Use single-quoted strings on the outside, which can contain double-quote characters. WARNING: single-quoted strings do not support variable expansion, so if your plan is to include variables in the future, this is a bad route to follow. If you don't plan that, this is much simpler



Escape Characters

Escape characters are used in strings or arrays to allow special characters with the txt to be read literally as is or to perform an function such as a windows system variable $username outputting a username.

# Single-quotes in double-quotes:
$string1 = "This is a 'perfectly fine' string!"


# Double-quotes in double-quotes:
$string2 = "This is also a `"perfectly fine`" string!"


# Single-quotes in single-quotes:
$string3 = 'This is a third ''perfectly fine'' string!'


# Double-quotes in single-quotes
$string4 = 'And here is the final "perfectly fine" string!'


# Variable expansion in double quotes
$variable = "Value"
$string5 = "This string expands the variable: $variable"
$string6 = 'But this string just treats the dollar sign as another character: $variable'


# Output
$string1
$string2
$string3
$string4
$string5
$string6



When in doubt if a string contains special characters and no other variables or functions, then use single outside quotation marks and use a single quotation mark after every single quotation mark within the txt.

e.g. to encapsulate txt: ('gsc-i-id1').placeholder = 'Search.';};

do so with encapsulating with single quotation marks as the following: '('gsc-i-id1').placeholder = 'Search.';};'

then add a single quotation mark behind every single quotation mark special character in the string as so: '(''gsc-i-id1'').placeholder = ''Search.'';};'



PowerShell for Web Development Code

The below txt is an example of removing the faulty line of Google Search code on all html pages of the Posh Python website. The code of line that is to be replaced is: ><script async src="https://cse.google.com/cse.js?cx=001261653461880299926:zfxw-gxeulo"></script><div class='
The various methods below show 4 different means to use PowerShells parameter commands with a combination of escape characters to achieve its goal.


### Solution 1: Using (a) and (c)

Set-Location -Path "C:\inetpub\wwwroot"
$files = get-childitem -path "C:\inetpub\wwwroot" -Include "*.htm" -Recurse -Force| Select-Object -ExpandProperty name
ForEach ($file in $files) {
# use backtick to mark double-quote characters
$searchString = "><script async src=`"https://cse.google.com/cse.js?cx=001261653461880299926:zfxw-gxeulo`"></script><div class="
# use Replace method instead of -replace operator
$object = get-content -path $file | ForEach-Object {$_.Replace($searchString,"")}
$object | Set-Content -Path $file
}




### Solution 2: Using (b) and (c)

Set-Location -Path "C:\inetpub\wwwroot"
$files = get-childitem -path "C:\inetpub\wwwroot" -Include "*.htm" -Recurse -Force| Select-Object -ExpandProperty name
ForEach ($file in $files) {
# use backtick to mark double-quote characters
$searchStringOriginal = "><script async src=`"https://cse.google.com/cse.js?cx=001261653461880299926:zfxw-gxeulo`"></script><div class="
# use Escape method of [regex] class to format the string
$searchString = [regex]::Escape($searchStringOriginal)
$object = get-content -path $file | ForEach-Object {$_ -replace $searchString, ""}
$object | Set-Content -Path $file
}




### Solution 3: Using (a) and (d)

Set-Location -Path "C:\inetpub\wwwroot"
$files = get-childitem -path "C:\inetpub\wwwroot" -Include "*.htm" -Recurse -Force| Select-Object -ExpandProperty name
ForEach ($file in $files) {
# use single-quoted string to contain double-quote characters
$searchString = '><script async src="https://cse.google.com/cse.js?cx=001261653461880299926:zfxw-gxeulo"></script><div class='
# use Replace method instead of -replace operator
$object = get-content -path $file | ForEach-Object {$_.Replace($searchString,"")}
$object | Set-Content -Path $file
}




### Solution 4: Using (b) and (d)

Set-Location -Path "C:\inetpub\wwwroot"
$files = get-childitem -path "C:\inetpub\wwwroot" -Include "*.htm" -Recurse -Force| Select-Object -ExpandProperty name
ForEach ($file in $files) {
# use single-quoted string to contain double-quote characters
$searchStringOriginal = '><script async src="https://cse.google.com/cse.js?cx=001261653461880299926:zfxw-gxeulo"></script><div class='
# use Escape method of [regex] class to format the string
$searchString = [regex]::Escape($searchStringOriginal)
$object = get-content -path $file | ForEach-Object {$_ -replace $searchString, ""}
$object | Set-Content -Path $file
}




Visit next page to learn about - 6.0 Deployment