I am creating an automation script that should take a user's input for version, package new, and new package path, then compress it into a zip folder. Unfortunately, I can't hardcode the paths, so I'm struggling with how to use relative paths and user inputs that I have to validate before using.
$env = Read-Host -Prompt "Version Number"
if ($env.length -ne 7) {
$env = Read-Host "Re-enter version number"
}
$packageName = Read-Host -Prompt "Package Name"
$newPackage = Write-Output $packageName"."$env
$newDirectory = Read-Host -Prompt "Path to new directory"
$deployPath = Read-Host -Prompt "Path to Deploy.ps1 file"
$zipFile = Read-Host -Prompt "Path for new zip file"
$newItem = $newDirectory"/"$newPackage
Write-Host $newItem
new-item $newItem -type directory
Copy-Item $deployPath -Destination $zipFile
Microsoft.PowerShell.Archive\Compress-Archive -Path /Users/SG/projects/$newPackage -DestinationPath /Users/SG/$newPackage.zip
I need the $zipFile to be input from the user, and the -Path/-DestinationPath should be relative paths since they can't be hardcoded.
You might simply add mandatory parameters to your script.
I would simply use a
[String]type for this as a[System.Io.FileInfo]type would default any relative path to theC:\WINDOWS\system32directory. See: PowerShell issue Introduce a PowerShell-aware path-information class for convenient .NET interoperability#14745.You might further describe in the comment base help
If you put this in a
ZipFile.ps1script, the following happens:Returns
If you than invoke your script (
.\ZipFile.ps1) without arguments the script will automatically ask for the mandatory parameters:Returns
And you might also consider to invoke you script directly with the required arguments:
Returns