Invoke-RemoteZipExpansion.ps1


Description

Purpose

Expands a ZIP file on a remote computer.

Detailed Description

The Invoke-RemoteZipExpansion function expands a ZIP file on a remote computer. It uses the Invoke-Command cmdlet to run the Expand-Archive cmdlet on the remote computer.

Back to Top

Usage

Example 1

$credential = Get-Credential

$sourceZipFilePath = “C:\Path\To\Your\ZipFile.zip” $destinationFolderPath = “C:\Path\To\DestinationFolder” Invoke-RemoteZipExpansion -ComputerName “RemoteServerName” -SourceZipFilePath $sourceZipFilePath -DestinationFolderPath $destinationFolderPath -Credential $credential

Back to Top

Notes

No additional notes.

Back to Top


Script

<#
.SYNOPSIS
Expands a ZIP file on a remote computer.

.DESCRIPTION
The Invoke-RemoteZipExpansion function expands a ZIP file on a remote computer. 
It uses the Invoke-Command cmdlet to run the Expand-Archive cmdlet on the remote computer.

.PARAMETER ComputerName
The name of the remote computer where the ZIP file will be expanded.

.PARAMETER SourceZipFilePath
The path of the ZIP file on the remote computer.

.PARAMETER DestinationFolderPath
The path of the folder on the remote computer where the ZIP file will be expanded.

.PARAMETER Credential
The credentials used to connect to the remote computer.

.EXAMPLE
$credential = Get-Credential
$sourceZipFilePath = "C:\Path\To\Your\ZipFile.zip"
$destinationFolderPath = "C:\Path\To\DestinationFolder"
Invoke-RemoteZipExpansion -ComputerName "RemoteServerName" -SourceZipFilePath $sourceZipFilePath -DestinationFolderPath $destinationFolderPath -Credential $credential
#>
function Invoke-RemoteZipExpansion {
    param (
        [Parameter(Mandatory = $true)]
        [string]$ComputerName,

        [Parameter(Mandatory = $true)]
        [string]$SourceZipFilePath,

        [Parameter(Mandatory = $true)]
        [string]$DestinationFolderPath,

        [Parameter(Mandatory = $true)]
        [PSCredential]$Credential
    )

    # Check if the source ZIP file exists
    if (-not (Test-Path $sourceZip)) {
        Write-Error "ZIP file not found at $sourceZip"
        return
    }

    $scriptBlock = {
        param (
            [string]$sourceZip,
            [string]$destinationFolder
        )

        # Check if the destination folder exists and is writable
        if (-not (Test-Path $destinationFolder -PathType Container)) {
            Write-Error "Destination folder not found at $destinationFolder"
            return
        }

        # Expand the ZIP file
        Expand-Archive -Path $sourceZip -DestinationPath $destinationFolder -Force
        Write-Host "Uncompressed $sourceZip to $destinationFolder"
    }

    # Execute the command on the remote computer with credentials
    Invoke-Command -ComputerName $ComputerName -ScriptBlock $scriptBlock -ArgumentList $SourceZipFilePath, $DestinationFolderPath -Credential $Credential
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top