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
}
}