Unable to Copy item from UNC path to destination using $en:programfiles \ (86)

###Windows powershell RightScript to install Espera

##stop and fail script if the software alreday installed##
if (${env:programfiles(x86)})
  { $advatel_path = join-path "${env:programfiles(x86)}" "Advatel" }

if (test-path $advatel_path)
{
   Write-Output "Espera already installed. Skipping script"
   read-host “Press ENTER to exit...”
   exit 0
   }
if (${env:programfiles})
  { $advatel_path = join-path "${env:programfiles}" "Advatel" }
if (test-path $advatel_path)
{
   Write-Output "Espera already installed. Skipping script"
   read-host “Press ENTER to exit...”
   exit 0
}

##Prompt for admin credentials before starting##
Write-Host "Please wait... "  
$CurrentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) 
if (($CurrentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) -eq $false) 
{ 
    $ArgumentList = "-noprofile -noexit -file "{0}" -Path "$Path" -MaxStage $MaxStage" 
    If ($ValidateOnly) { $ArgumentList = $ArgumentList + " -ValidateOnly" } 
    If ($SkipValidation) { $ArgumentList = $ArgumentList + " -SkipValidation $SkipValidation" } 
    If ($Mode) { $ArgumentList = $ArgumentList + " -Mode $Mode" } 
    Write-Host "elevating" 
    Start-Process powershell.exe -Verb RunAs -ArgumentList ($ArgumentList -f ($myinvocation.MyCommand.Definition)) -Wait 
    Exit 
} 

#Set-ExecutionPolicy -Scope CurrentUser -Confirm Unrestricted -Verbose

##Locate the msi files for silent execution##
$CurrentLocation = Split-Path -Parent $MyInvocation.MyCommand.Path;

##To install the Espera##
$msiinstall = @("/i", "EsperaClientSetup.msi", "/q", "/norestart", "/l*v","$env:windir\Temp\espera.log")
Start-Process -FilePath "$env:systemroot\system32\msiexec.exe" -ArgumentList $msiinstall -Wait -WorkingDirectory $CurrentLocation

##Check IP address to determine which XML file to install
$IP = ((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
If('$IP' -like "10.10.*.*") {
Copy-Item '.\Thh\EsperaClient.exe.config' -Destination "$env:programfiles(x86)"\Advatel\Espera Client\bin  -Recurse -Force
}Else
{
   Write-host "Espera Installed"
}
stop-process -Id $PID

LINE 48 is where i am having issues with copying from the unc path to destination. I think the issue is the destination part.

"${env:programfiles(x86)}\Advatel\Espera Client\bin"

I have tried that with no luck :frowning:

Do you get errors? … I tried it and it outputs exactly the expected result. Did you notice - there is no Dollar sign inside the curly braces. :wink:

Yeah it looks like permissions

Copy-Item : Access to the path ‘C:\Program Files (x86)\Advatel\Espera Client\bin\EsperaClient.exe.config’ is denied.
At line:1 char:1

  • Copy-Item ‘.\Thh\EsperaClient.exe.config’ -destination "${env:program …
  •   + CategoryInfo          : PermissionDenied: (\\tpin01\it_dat...ient.exe.config:FileInfo) [Copy-Item], UnauthorizedAccessException
      + FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
    
    

Copy-Item : Access to the path ‘C:\Program Files (x86)\Advatel\Espera Client\bin\EsperaClient.exe.config’ is denied.
At line:1 char:1

  • Copy-Item ‘.\Thh\EsperaClient.exe.config’ -destination "${env:program …
  •   + CategoryInfo          : NotSpecified: (:) [Copy-Item], UnauthorizedAccessException
      + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.CopyItemCommand

So your issue is not your Powershell code it is your lack of rights to the target folder. :wink: