|
| 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 | + |
0 commit comments