Skip to content

Commit 141f4a2

Browse files
committed
Added files for session
1 parent 2c507a5 commit 141f4a2

File tree

91 files changed

+4172
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

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