Skip to content

Commit 8b47206

Browse files
committed
Added demos for PowerShell - Grow Your Script From Simple to Module
0 parents  commit 8b47206

40 files changed

+1878
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Get to the presentation directory
2+
Set-Location "Presentations:\__Original\PowerShell - Grow Your Script From Simple to Module\Demo"
3+
4+
# Execte the raw script
5+
.\1_Demo_Parameters_01_RawScript.ps1
6+
7+
# Execute the parameterized script
8+
.\1_Demo_Parameters_02_ParametersUsed.ps1 -FileCount 10 -Destination C:\temp\demo
9+
10+
# Failed attempt with an invalid file count
11+
.\1_Demo_Parameters_03_Validation.ps1 -FileCount 30 -Destination C:\temp\demo -DeleteFiles
12+
13+
# Failed attempt with an invalid destination
14+
.\1_Demo_Parameters_03_Validation.ps1 -FileCount 20 -Destination C:\temp\demo2 -DeleteFiles
15+
16+
# Failed attempt with an invalid extension
17+
.\1_Demo_Parameters_03_Validation.ps1 -FileCount 20 -Destination C:\temp\demo -Extension bat -DeleteFiles
18+
19+
# Execute the script with functions
20+
.\1_Demo_Parameters_04_FunctionsImplemented.ps1
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[int]$fileCount = 15
2+
[string]$destination = "C:\temp\demo"
3+
[string]$extension = 'txt'
4+
[switch]$deleteFiles = $true
5+
6+
# Test if destination exists
7+
if(-not (Test-Path $destination)){
8+
Write-Host "Creating destination directory..."
9+
# Try to create destination directory
10+
try{
11+
New-Item -Path $destination -ItemType Directory
12+
}
13+
catch{
14+
Write-Error "Couldn't create directory $destination.`n$_"
15+
}
16+
}
17+
18+
# Checking if files need to be deleted
19+
if($deleteFiles){
20+
Write-Host "Deleting files..."
21+
$item = Get-ChildItem -Path $destination
22+
23+
# Check if there are any items in the directory
24+
if($item.Count -ge 1){
25+
try{
26+
Remove-Item -Path "$destination\*.*"
27+
}
28+
catch{
29+
Write-Error "Couldn't remove files.`n$_"
30+
}
31+
32+
}
33+
}
34+
35+
Write-Host "Create files..."
36+
for($i = 0; $i -lt $fileCount; $i++){
37+
# Generate random string
38+
[string]$random = -join ((65..90) + (97..122) | Get-Random -Count 10 | Foreach-Object {[char]$_})
39+
40+
# Setup the new file path
41+
$newFilePath = "$destination\File_$random.$extension"
42+
43+
# Try to create the file
44+
try{
45+
New-Item -Path $newFilePath -ItemType File
46+
}
47+
catch{
48+
Write-Error "Couldn't create file $newFilePath.`n$_"
49+
}
50+
}
51+
52+
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
param(
2+
[Parameter(Mandatory = $true)]
3+
[int]$FileCount,
4+
[Parameter(Mandatory = $true)]
5+
[string]$Destination ,
6+
[string]$Extension = 'txt',
7+
[switch]$DeleteFiles
8+
)
9+
10+
# Test if destination exists
11+
if(-not (Test-Path $Destination)){
12+
Write-Host "Creating destination directory..."
13+
# Try to create destination directory
14+
try{
15+
New-Item -Path $Destination -ItemType Directory
16+
}
17+
catch{
18+
Write-Error "Couldn't create directory $Destination.`n$_"
19+
return
20+
}
21+
}
22+
23+
# Checking if files need to be deleted
24+
if($DeleteFiles){
25+
Write-Host "Deleting files..."
26+
$item = Get-ChildItem -Path $Destination
27+
28+
# Check if there are any items in the directory
29+
if($item.Count -ge 1){
30+
try{
31+
Remove-Item -Path "$Destination\*.*"
32+
}
33+
catch{
34+
Write-Error "Couldn't remove files.`n$_"
35+
}
36+
37+
}
38+
}
39+
40+
Write-Host "Create files..."
41+
for($i = 0; $i -lt $FileCount; $i++){
42+
# Generate random string
43+
[string]$random = -join ((65..90) + (97..122) | Get-Random -Count 10 | Foreach-Object {[char]$_})
44+
45+
# Setup the new file path
46+
$newFilePath = "$Destination\File_$random.$Extension"
47+
48+
# Try to create the file
49+
try{
50+
New-Item -Path $newFilePath -ItemType File
51+
}
52+
catch{
53+
Write-Error "Couldn't create file $newFilePath.`n$_"
54+
}
55+
}
56+
57+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
param(
2+
[Parameter(Mandatory = $true)]
3+
[ValidateRange(1, 20)]
4+
[int]$FileCount,
5+
[Parameter(Mandatory = $true)]
6+
[ValidateNotNullOrEmpty()]
7+
[ValidateScript( {Test-Path -Path $_ -PathType 'Container'})]
8+
[string]$Destination ,
9+
[ValidateSet('txt', 'log')]
10+
[string]$Extension = 'txt',
11+
[switch]$DeleteFiles
12+
)
13+
14+
# Checking if files need to be deleted
15+
if ($DeleteFiles) {
16+
Write-Host "Deleting files..."
17+
$item = Get-ChildItem -Path $Destination
18+
19+
# Check if there are any items in the directory
20+
if ($item.Count -ge 1) {
21+
try {
22+
Remove-Item -Path "$Destination\*.*"
23+
}
24+
catch {
25+
Write-Error "Couldn't remove files.`n$_"
26+
}
27+
28+
}
29+
}
30+
31+
Write-Host "Create files..."
32+
for ($i = 0; $i -lt $fileCount; $i++) {
33+
# Generate random string
34+
[string]$random = -join ((65..90) + (97..122) | Get-Random -Count 10 | Foreach-Object {[char]$_})
35+
36+
# Setup the new file path
37+
$newFilePath = "$Destination\File_$random.$Extension"
38+
39+
# Try to create the file
40+
try {
41+
New-Item -Path $newFilePath -ItemType File
42+
}
43+
catch {
44+
Write-Error "Couldn't create file $newFilePath.`n$_"
45+
}
46+
}
47+
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
2+
3+
function Invoke-DemoCreateDirectory {
4+
param(
5+
[Parameter(Mandatory = $true)]
6+
[string]$Directory,
7+
[switch]$Force
8+
)
9+
10+
Write-Host "Creating destination directory..."
11+
# Try to create destination directory
12+
try {
13+
New-Item -Path $Directory -ItemType Directory -Force:$Force
14+
}
15+
catch {
16+
Write-Error "Couldn't create directory $Destination.`n$_"
17+
return
18+
}
19+
}
20+
21+
function Remove-DemoCreatedFiles {
22+
param(
23+
[Parameter(Mandatory = $true)]
24+
[string]$Directory,
25+
[switch]$Force
26+
)
27+
28+
Write-Host "Deleting files..."
29+
$item = Get-ChildItem -Path $Destination
30+
31+
# Check if there are any items in the directory
32+
if ($item.Count -ge 1) {
33+
try {
34+
Remove-Item -Path "$Destination\*.*" -Force:$Force
35+
}
36+
catch {
37+
Write-Error "Couldn't remove files.`n$_"
38+
}
39+
}
40+
}
41+
42+
function Get-DemoRandomString {
43+
param(
44+
[Parameter(Mandatory = $true)]
45+
[int]$Length = 5
46+
)
47+
48+
# Generate random string
49+
[string]$random = -join ((65..90) + (97..122) | Get-Random -Count $Length | Foreach-Object {[char]$_})
50+
51+
return $random
52+
}
53+
54+
function Invoke-DemoCreateFiles {
55+
[cmdletbinding()]
56+
param(
57+
[Parameter(Mandatory = $true)]
58+
[ValidateRange(1, 20)]
59+
[int]$FileCount,
60+
[Parameter(Mandatory = $true)]
61+
[ValidateNotNullOrEmpty()]
62+
[string[]]$Destination ,
63+
[ValidateSet('txt', 'log')]
64+
[string]$Extension = 'txt',
65+
[switch]$DeleteFiles,
66+
[switch]$Force
67+
)
68+
69+
foreach ($dest in $Destination) {
70+
# Checking if files need to be deleted
71+
if ($DeleteFiles -and ((Test-Path $dest))) {
72+
Remove-DemoCreatedFiles -Directory $dest -Force:$Force
73+
}
74+
75+
# Check if path exists
76+
if (-not (Test-Path $dest)) {
77+
Invoke-DemoCreateDirectory -Directory $dest -Force:$Force
78+
}
79+
80+
Write-Host "Create files..."
81+
82+
for ($i = 0; $i -lt $FileCount; $i++) {
83+
# Generate random string
84+
[string]$random = Get-DemoRandomString -Length 5
85+
86+
# Setup the new file path
87+
$newFilePath = "$dest\File_$random.$Extension"
88+
89+
# Try to create the file
90+
try {
91+
New-Item -Path $newFilePath -ItemType File
92+
}
93+
catch {
94+
Write-Error "Couldn't create file $newFilePath.`n$_"
95+
}
96+
97+
} # end for loop file count
98+
99+
} # end for each destination
100+
101+
} # end function
102+
103+
$fileCount = 10
104+
$destination = "c:\temp\demo"
105+
$destination1 = "c:\temp\demo1"
106+
$destination2 = "c:\temp\demo2"
107+
108+
# Get a random string
109+
#Get-DemoRandomString -Length 10
110+
111+
# Create the demo directory
112+
#Invoke-DemoCreateDirectory -Directory $destination -Force
113+
114+
# $3move the files from the demo directory
115+
#Remove-DemoCreatedFiles -Directory $destination
116+
117+
# Execute the main function
118+
#Invoke-DemoCreateFiles -FileCount $fileCount -Destination $destination
119+
120+
# Execute the script with a multiple values
121+
Invoke-DemoCreateFiles -FileCount $fileCount -Destination $destination1, $destination2
122+
123+
124+
125+
126+
127+
128+
129+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Execute script with first step to advanced function
2+
.\2_Demo_Functions_01_GettingAdvanced_Pipeline
3+
4+
# Making sure the function is able to executed with a pipeline and multiple values assigned as parameter
5+
.\2_Demo_Functions_02_GettingAdvanced_PipelineLoop
6+
7+
# Execute the functions with the -Confirm and -Whatif
8+
.\2_Demo_Functions_03_ConfirmAndWhatif.ps1

0 commit comments

Comments
 (0)