PS Variable - cannot find path

Guys. I have this script that I knocked up. It basically copies a .PFX file to a remote machine and then installs the .PFX file.

I have two lines in the script where I am using a hardcoded path C:\SomeFolder\Cert which when hard coded works fine. However you will see i have a variable $RemoteFolder which I would like to use. When i do this i get an error that C:\Cert is not found. Its like its ignoring the $RemoteFolder path.

Its the only part of the script i have not been able to change to a variable

Import-PfxCertificate -Password $Using:MyPwd.Password -CertStoreLocation Cert:\LocalMachine\My -Exportable -FilePath C:\SomeFolder\Cert\Cert.pfx

Remove-Item -path C:\SomeFolder\Cert -Recurse -force

For example i wish to change to

Remove-Item -path $RemoteFolder\Cert -Recurse -force

The full script below.

#Set Runtime Parameters
# PFX Password
$MyPwd = Get-Credential -UserName 'Enter password below' -Message 'Enter password below'
# List of servers to install PFX on 
$Servers = Get-Content -Path 'C:\Servers.txt'
# Location of the PFX file
$CertFolder = 'C:\Cert'
$RemoteFolder = 'C:\SomeFolder\'

foreach ($Server in $Servers) {
    if (Test-Connection -ComputerName $server -Quiet -Count 1) {
        $session = New-PSSession -ComputerName $server       
        Copy-Item –Path $CertFolder -Recurse –Destination $RemoteFolder\Cert –ToSession $session -Force
        Write-Host "Assets successfully copied!" -ForegroundColor Green

        Invoke-Command -ComputerName $Server -ScriptBlock {
        Import-PfxCertificate -Password $Using:MyPwd.Password -CertStoreLocation Cert:\LocalMachine\My -Exportable -FilePath C:\SomeFolder\Cert\Cert.pfx
        Write-Host "Setup successful on: $env:COMPUTERNAME" -ForegroundColor Green
        #
        # Delete PFX file that we copied
        Write-Host "Delete copied PFX file" -ForegroundColor Green
        Remove-Item -path C:\SomeFolder\Cert -Recurse -force
        #
        # Close the PS Session
        write-host "Removing PS Session to $env:COMPUTERNAME"  -ForegroundColor Green
        Get-PSSession | Disconnect-PSSession
        
         }
         }
     else {
        Write-Host "$Server appears to be offline!" -ForegroundColor Red
      }

}

If you work with different providers it helps sometimes to explicitly provide the provider you want to access. In this case you could use

$RemoteFolder = 'FileSystem::C:\SomeFolder\Cert'

If you want to build a path from parts you should avoid something like this:

$RemoteFolder\Cert

A more professional option would be to use …

$Path = Join-Path -Path $RemoteFolder -ChildPath 'Cert'

… and use $Path instead.

Although this is some good advice, the issue would appear to be trying to access a local variable on a remote system.

When running commands on remote powershell sessions (including jobs and alternate runspaces) the variables defined locally aren’t automatically available. You can see with the password the $using: modifier was invoked, which allows the password to be retrieved from the local session to the remote. You could use the same method for the $RemoteFolder variable or, in my opinion, an even better technique is to explicitly define and pass variables into the remote scriptblock.

Invoke-Command -ComputerName $Server -ScriptBlock {
    Param(
        $Password,
        $Path
    )
    
    $fullpath = Join-Path $Path Cert
    $filepath = Join-Path $fullpath Cert.pfx

    Import-PfxCertificate -Password $Password.Password -CertStoreLocation Cert:\LocalMachine\My -Exportable -FilePath $filepath
    Write-Host "Setup successful on: $env:COMPUTERNAME" -ForegroundColor Green

    # Delete PFX file that we copied
    Write-Host "Delete copied PFX file" -ForegroundColor Green
    Remove-Item -Path $filepath -Recurse -Force
        
} -ArgumentList $MyPwd,$RemoteFolder

Also you are attempting to remove the PSSession in the remote scriptblock, that should be outside.

Oooops … I didn’t pay attention to that. Good catch. :+1:t4: :wink:

Thank you both for providing info on where i was going wrong. Thank you as i have learned something new today. Appreciate the reply