I am getting an error while running the script with Invoke command
Script:
Import-Module -Name WebAdministration
Invoke-Command -ComputerName “Test” -ScriptBlock {Get-ChildItem IIS:SSLBindings | ForEach-Object -Process {
if ($.Sites)
{
$certificate = Get-ChildItem -Path CERT:LocalMachine/My |
Where-Object -Property Thumbprint -EQ -Value $.Thumbprint
[PsCustomObject]@{
Sites = $_.Sites.Value
CertificateFriendlyName = $certificate.FriendlyName
CertificateDnsNameList = $certificate.DnsNameList
CertificateNotAfter = $certificate.NotAfter
CertificateIssuer = $certificate.Issuer
}
}
}}
Cannot find drive. A drive with the name ‘IIS’ does not exist.
+ CategoryInfo : ObjectNotFound: (IIS:String) [Get-ChildItem], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
+ PSComputerName : test
Hey Divakar,
The webadministration module has been imported in your local PowerShell session. Try adding that to the invoke-command script block instead.
Liam
Thank you for your enlightenment. I have added the webadministration module in remote PowerShell session.
Now script is not giving any error but the script is fetching data from local computer only not from remote computer.
Below is the script for your ready reference.
$session = New-PSSession -ComputerName a,b,c,d
Invoke-Command -Session $session -ScriptBlock {Import-Module WebAdministration}
Clear-content C:\Scripts\CertReport.htm
$result=Invoke-Command -Session $session -ScriptBlock {Get-ChildItem -Path IIS:SSLBindings -Recurse} | ForEach-Object -Process `
{
if ($.Sites)
{
$certificate = Get-ChildItem -Path CERT:LocalMachine/My |
Where-Object -Property Thumbprint -EQ -Value $.Thumbprint
[PsCustomObject]@{
Computername = $_.PSComputername
Sites = $_.Sites.Value
CertificateFriendlyName = $certificate.FriendlyName
CertificateDnsNameList = $certificate.DnsNameList
CertificateNotAfter = $certificate.NotAfter
CertificateIssuer = $certificate.Issuer
}
}
}
$a = “”
$a = $a + “BODY{background-color:#d6eaf8;font-family:verdana;font-size:8pt;}”
$a = $a + “TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}”
$a = $a + “TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}”
$a = $a + “TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}”
$a = $a + “”
$result | ConvertTo-HTML -Head $a | Out-File C:\Scripts\CertReport.htm
if
($result –eq $Null)
{
$bodym = “Certificate Report-No Expiration”
Send-MailMessage -To divakartandon@abc.com -from PKI@abc.com -Subject “Certificate Report-No Expiration” -Body $bodym -SmtpServer abc.com
}
else
{
$body = Get-Content C:\Scripts\CertReport.htm -Raw
Send-MailMessage -To divakartandon@abc.com -from PKI@abc.com -Subject “IISBinding” -Body $body -BodyAsHtml -SmtpServer abc.com
}