我正在整理一个脚本,该脚本将检查我的所有Web服务器证书,以便我可以监视它们何时设置为过期。当脚本执行Invoke-Command
时,我收到此错误:
这是我的代码:
Import-Module WebAdministration
$results = @()
$ou = 'OU=test,OU=test,OU=Servers,DC=contoso,DC=com'
$subtree = Get-ADOrganizationalUnit -SearchBase $ou -SearchScope Subtree -filter * | Select-Object -ExpandProperty DistinguishedName
ForEach($dn in $subtree){
$servers = Get-ADComputer -Filter * -SearchBase $dn | select Name
$results += $servers
}#ForEach($dn in $subtree)
$scriptBlock = [scriptblock]::Create({
Import-Module WebAdministration; Get-ChildItem -Path 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
CertificateDNSNameList = $certificate.DnsNameList
CertificateNotAfter = $certificate.NotAfter
}#[PSCustomObject]
}#If($_.Sites)
}#Import-Module
})#ScriptBlock
ForEach($server in $results){
Invoke-Command -ComputerName $server -ScriptBlock $scriptBlock | Select Sites,CertificateDNSNameList,CertificateNotAfter,PSComputerName #ScriptBlock
}#ForEach($server in $results)
现在,如果我从循环中取出以下行并将$server
替换为实际的服务器名称,我会得到我正在寻找的结果:
Invoke-Command -ComputerName ServerName -ScriptBlock $scriptBlock | Select Sites,CertificateDNSNameList,CertificateNotAfter,PSComputerName
任何想法我做错了什么?
答案 0 :(得分:2)
在我看来,“select Name”正在返回一个对象而不是一个字符串。尝试将获取服务器的行更改为:
$servers = Get-ADComputer -Filter * -SearchBase $dn | select -ExpandProperty Name
答案 1 :(得分:2)
你的结果 - 数组不是一个字符串数组,它是一个对象数组。每个对象都有属性名称。将您的最后一个ForEach-Loop更改为此脚本,您的脚本将起作用:
ForEach($server in $results){
Invoke-Command -ComputerName $server.name -ScriptBlock $scriptBlock | Select Sites,CertificateDNSNameList,CertificateNotAfter,PSComputerName #ScriptBlock
}#ForEach($server in $results)