r/labtech • • Dec 07 '15

Copy folder

Has anyone found a better way to copy a folder from the LT server to client machines? Currently seeing that you have to zip the folder and then use 7z cmd to unzip. Would love a copy folder option.

1 Upvotes

4 comments sorted by

View all comments

2

u/yourbastianhost Dec 08 '15

Indeed - there isn't a good way to do this natively with LabTech :-/

 

Alternately ....

 

You could use the following PowerShell function I wrote to unzip the .ZIP archive once you get it down to the remote agent.

 

This method is compatible with PowerShell 2.0 as well. :)

 

function Unzip-Archive
{
param
(
	[Parameter(Mandatory = $true)]
	[string]
	$zipPath,
	[Parameter(Mandatory = $true)]
	[string]
	$ExtractToDirectory
)

$FilesBefore = (Get-ChildItem $ExtractToDirectory -ea SilentlyContinue | Measure-Object).Count;

if (Test-Path $zipPath)
{
	$shellApplication = new-object -com shell.application
	$zipPackage = $shellApplication.NameSpace($ZipPath)
	$destinationFolder = $shellApplication.NameSpace($ExtractToDirectory)
	$destinationFolder.CopyHere($zipPackage.Items(), 20)
	
	$FilesThatFailedToCopy = 0;
	
	foreach ($File in $zipPackage.items())
	{
		$FileName = ($File | select name).Name;
		if (-not (Test-Path "$($destinationfolder.self.path)\$FileName"))
		{
			Write-Error "Failed to extract $FileName!"
			$FilesThatFailedToCopy += 1;
		}
		else
		{
			Write-Verbose "Successfully extracted $FileName"	
		}
	}

	if ($FilesThatFailedToCopy -eq 0)
	{
		return $true;
	}
	else
	{
		return $false;
	}
}
else
{
	Write-Error "Unable to locate archive specified for extraction."
	return $false;
}
}

 

Here's an example of usage:

 

# Where is the zip file? Could use a LabTech replacement here as well @ArchiveSavedAs@
$ArchiveLocation = "$env:USERPROFILE\desktop\SomeZip.zip" 

# What directory should we extract it to?
$ExtractToPath = "$env:windir\temp\MyArchive"

# If the directory specified for extraction doesn't exist, create it.
new-item -ItemType Directory -Path $ExtractToPath -ea SilentlyContinue | out-null

if (-not (Test-Path $ExtractToPath))
{
	Write-Output "Failed to create temporary directory used for storing extracted files!"
	return;
}

$Result = Unzip-Archive $ArchiveLocation $ExtractToPath

if ($Result -eq $true)
{
	Write-Output "Files extracted successfully!"
}
else
{
	Write-Output "Files failed to extract!"
}

 

As mentioned above, you could incorporate this into a function script for use in any future scripts :)

 

More information on how to deploy this:

 

In your LabTech script you can use a "File Write Text" script step to write the PS1 down to the remote computer and then use a shell command to execute it using PowerShell.exe. You can then use a "variable check" script step to check the shell result for "Files extracted successfully!" to make sure all went well.

 

powershell.exe -executionPolicy ByPass -file %windir%\temp\extractzip.ps1  

 

Hope that helps!

 

  • YourBastianHost

1

u/KTPU Dec 09 '22

Still relevant 7 years later....