I have a script that copies files to some remote machines. It’s only purpose is to push the files out, but I also want to make it flexible. There is a standard destination folder on the remote machine, and the default source location is the current directory. I have created parameters to change both the source and destination folders, and an option to select the files to be added to the ‘-Include’ option.
However, the files do not copy, and I get no error message. If I copy and paste the command that is being issued directly into the PS prompt, it does work. What can I be doing wrong?
Thanks,
[CmdletBinding()]
Param(
[Parameter(valuefrompipeline=$true,Position=0)]
[alias("cn")]
[string[]]$ComputerName=($servername.keys | %{if($servername.$_ -eq $env:computername){$_}}),
[alias("f")]
[string]$File,
[string]$From,
[string]$To
)
Begin
{
}
Process
{
foreach($computer in $ComputerName){
# Push specified software out to listed machines
# default Destination will Data drive \ GEO_Install - Option for desination location
# Source will be current folder location - But option for source location
$geoServer= $($serverName[$($Computer)])
Write-Host -ForegroundColor Yellow "Processing: $ComputerName System Name: $geoServer`n"
if ($File)
{
Write-Verbose "A filter has been specified : $File"
}
Else
{
Write-verbose "No filter has been specified - All files will be copied."
}
if ($From)
{
Write-Verbose "A source location has been specified : $From"
}
Else
{
Write-Verbose "Using default source location, current directory."
$From ="*"
}
if ($To)
{
Write-Verbose "A Destination location has been specified : $To"
}
Else
{
Write-Verbose "Using default destination location for specified computer"
$destdrive = $loaderHT["$Computer"]
$destDrive = $destdrive.substring(0,1) + "$"
$To = "\\$geoserver\$destdrive\GEO_Install\"
Write-Verbose "Default Destination : $dest"
}
Write-Verbose "---------------------------------------------------------------------------"
Write-Verbose "Source Location : $From"
Write-Verbose "Destination : $To"
Write-Verbose "Include files : $File"
Write-Verbose "Copy-Item -Path $From -Destination $To -Include $File -Force -Recurse"
Try
{
Copy-Item -Path $From -Destination $To -Include $File -force -Recurse -ErrorAction stop
#Copy-Item -Path "C:\dev\*" -Destination "C:\Test" -Include *.bat,*.txt -Force -Recurse
Write-Verbose "Copy-Item -Path $From -Destination $To -Include $File -Force -Recurse"
write-host "Software copied."
}
Catch
{
$errorMessage = $_.Exception.Message
Write-Error "An error occured copying software to : $Computer ($geoServer) , please check the log file for details"
}
}
}
End
{}
}