Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

combining variable + .pfx in foreach

$
0
0

Hi

I have a script that is supposed to copy a certificate for each server(certs are by name) and import it to each server and then set the rdp listener to use that certificate(instead of the default)

something like this:

$certsLocation="c:\temp"

 

 

 

 

 

 

 

 

$servernames

 

 

=Get-ChildItem$certsLocation|selectname|%{$_.name.Split("{.}") |select-First1}

ForEach

 

 

($serverin$servernames) {

if

 

 

(!(Test-Path\\$server\c$\certs)){New-Item-typedirectory-Path\\$server\c$\certs}

Copy-Item

 

 

-Path$certsLocation\$server.PFX-Destination\\$server\c$\certs-Force

$data

 

 

=Invoke-Command-ScriptBlock {Import-PfxCertificate–FilePathC:\certs\'$($server)+".PFX"'cert:\localMachine\my-Password (ConvertTo-SecureString-String"1234"-AsPlainText-Force)} -ComputerName$server

$thumbprint

 

 

=$data.Thumbprint

$path= (Get-WmiObject-computeradfs1-class"Win32_TSGeneralSetting"-Namespaceroot\cimv2\terminalservices-Filter"TerminalName='RDP-tcp'").__path

Set-WmiInstance

 

 

-Path$path-argument @{SSLCertificateSHA1Hash=$thumbprint

}

}

so far good

it seem to break here:

$data=Invoke-Command-ScriptBlock {Import-PfxCertificate–FilePathC:\certs\'$($server)+".PFX"'cert:\localMachine\my-Password (ConvertTo-SecureString-String"1234"-AsPlainText-Force)} -ComputerName$server

and specifically I think here:

C:\certs\'$($server)+".PFX"'

The system cannot find the path specified. 0x80070003 (WIN32: 3 ERROR_PATH_NOT_FOUND

now if I test this using the actual computer name it works no problem

for example:

=Invoke-Command-ScriptBlock {Import-PfxCertificate–FilePathC:\certs\server1.PFXcert:\localMachine\my-Password (ConvertTo-SecureString-String"1234"-AsPlainText-Force)} -ComputerName server1

so im guessing its either that combination with foreach loop or the name of the cert isn't being constructed right from comning server name and .PFX extension

anyway, any help would be appreciated

Thanks

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles