I'm using a custom script for a while I got from the content exchange on my Windows servers that's working fine on most of the standard Windows servers. But when I run it against Windows server running a core OS (no GUI) I get the message when testing the script - 'System.Management.Automation.Runspaces.InitialSessionState'
For reference, here's the script that works on most of the other servers:
# Exclude certificates
# Add new subject in the format "subject here" with a comma between each subject, so one subject looks like this
# $exludeCerts = "Part of the subject of certificate" and two or more certs looks like this
# $exludeCerts = "Part of the subject of certificate", "Part of the subject of another certificate"
# Try to be specific to reduce false positives
$exludeCerts = "Verisign"
# Number of days to look for expiring certificates, update this value to change the threshold
$intThreshold = 30 # 730 days = 2 years
#Set deadline date
$dateDeadline = (Get-Date).AddDays($intThreshold)
# Grabs the Certificate Store command and stores it in an object
$objStore = new-object System.Security.Cryptography.X509Certificates.X509Store("\\${IP}\My","LocalMachine")
# Opens the Cerificate store and places contents into the object as ReadOnly
$objStore.open("ReadOnly")
# Scans through each certificate in the store and logs each one that meets the criteria
$count = 0
try {
$objStore.certificates | % {
# boolean value, used if any of the exclude cert values are found to be appicable
$excludeBool = $false
# Compares each exluded cert subhect against the current certificate's subject
foreach($name in $exludeCerts){
If ($_.Subject.ToLower().contains($name.ToLower())){
$excludeBool = $true
}
}
# If an exluded certificate is found then it's valuation is skipped
If (!$excludeBool) {
# If the certificate expires within the number of days described in variable $intThreshold and that value is not negative then log the certificate
If ($_.NotAfter -lt $dateDeadline -and ($_.NotAfter - (Get-Date)).Days -gt 0) {
[int]$dateExpireDays = ($_.NotAfter - (Get-Date)).Days
If ($dateExpireDays -lt $intThreshold){
[string]$strSubject = $_.Subject
[string]$strExpireDate = $_.NotAfter.ToString("MM-dd-yyyy")
Write-Host "Message.$count : Certificate $strSubject Will Expire on $strExpireDate, which is in $dateExpireDays days."
Write-Host "Statistic.$count : $dateExpireDays"
$count++
}
} else {
If (!$dateExpireDays){
[int]$dateExpireDays = ($_.NotAfter - (Get-Date)).Days
}
If($dateExpireDays){
[int]$dateExpireDaysNew = ($_.NotAfter - (Get-Date)).Days
If ($dateExpireDaysNew -lt $dateExpireDays){
If ($dateExpireDaysNew -gt 0){
$dateExpireDays = $dateExpireDaysNew
}
}
}
}
}
}
}
catch {
exit 2
}
if ($count -gt 0){
exit 3
} else {
If (!$dateExpireDays){
$dateExpireDays = 730
}
Write-Host "Message.$count : No Certificate Will Expire within next $intThreshold days."
Write-Host "Statistic.$count : $dateExpireDays"
exit 0
}
exit 0