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 the C:\WINDOWS\system32
directory. See: PowerShell issue Introduce a PowerShell-aware path-information class for convenient .NET interoperability #14745
.
You might further describe in the comment base help
<#
.SYNOPSIS
Compress Files
.DESCRIPTION
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.
.PARAMETER ZipFile
(Relative) path for new zip file
#>
Param(
[Parameter(Mandatory=$true)]
[string]
$zipFile
)
Join-Path (Get-Location) $ZipFile
If you put this in a ZipFile.ps1
script, the following happens:
PS C:\CurrentDirectory> .\ZipFile.ps1 -?
Returns
NAME
C:\CurrentDirectory\ZipFile.ps1
SYNOPSIS
Compress Files
SYNTAX
C:\CurrentDirectory\ZipFile.ps1 [-zipFile] <String> [<CommonParameters>]
DESCRIPTION
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.
RELATED LINKS
REMARKS
To see the examples, type: "Get-Help C:\CurrentDirectory\ZipFile.ps1 -Examples"
For more information, type: "Get-Help C:\CurrentDirectory\ZipFile.ps1 -Detailed"
For technical information, type: "Get-Help C:\CurrentDirectory\ZipFile.ps1 -Full"
If you than invoke your script (.\ZipFile.ps1
) without arguments the script will automatically ask for the mandatory parameters:
PS C:\CurrentDirectory> .\ZipFile.ps1
Returns
cmdlet ZipFile.ps1 at command pipeline position 1
Supply values for the following parameters:
zipFile: Test.Zip
C:\CurrentDirectory\Test.Zip
And you might also consider to invoke you script directly with the required arguments:
PS C:\CurrentDirectory\ZipFile.ps1 My.Zip
Returns
C:\CurrentDirectory\My.Zip