Needing to have user input for some paths and use relative paths for compressing my folder

109 Views Asked by At

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.

2

There are 2 best solutions below

4
iRon On BEST ANSWER

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
6
cptcmdlet On

You could use windows forms to prompt for directory paths. A function to do so might look something like

Function Get-filePath() {

    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $OpenFolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $OpenFolderDialog.rootfolder = 'MyComputer'
    $OpenFolderDialog.ShowDialog() | Out-Null
    $OpenFolderDialog.SelectedPath
}

Then use it like

Write-Host "Please enter your path for X/Y/Z"
$newDirectory = Get-filePath

for each of your needed paths, or however you'd care to integrate. You can play with initial directory a bit too.

updated example:

Function Get-filePath() {

    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $OpenFolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $OpenFolderDialog.rootfolder = 'MyComputer'
    $OpenFolderDialog.ShowDialog() | Out-Null
    $OpenFolderDialog.SelectedPath
}

Write-Host "Path to new directory:"
$newDirectory = Get-filePath
$newDirectory
Write-Host "Path to deploy the ps1 file:"
$deployPath = Get-filePath
$deployPath
Write-Host "Path for new zip file:"
$zipFile = Get-filePath
$zipFile