【发布时间】:2019-03-08 03:34:39
【问题描述】:
我正在尝试拼凑一些 PowerShell 代码以遍历服务器列表,返回有关其 IIS 站点和绑定的一些信息,如果它们具有 https 绑定,则获取证书哈希并使用它通过指纹定位证书并返回其到期日期。
我遇到的问题是,当我在 $binding.cerficateHash 下面运行我的代码时,似乎返回了我所期望的证书哈希字符串,但是当我使用该证书哈希属性尝试通过其获取证书时指纹,它不起作用......但是当我获取证书哈希值的原始字符串值并对其进行硬编码时,它起作用了......
我检查了 certificateHash.GetType() ,它似乎只是一个字符串,所以我不明白我做错了什么,我尝试了一些事情,但无济于事,因为这是我第一次破解powershell 所以有很多我不知道的。
$sites = Invoke-Command -ComputerName $serverName { Import-Module WebAdministration; Get-ChildItem -path IIS:\Sites } -ErrorAction SilentlyContinue
foreach($site in $sites)
{
$serverName
$site.name
$site.physicalPath
foreach($binding in $site.bindings.Collection)
{
$binding.protocol
$binding.bindingInformation
$binding.certificateHash
$binding.certificateStoreName
if($binding.certificateHash)
{
# This outputs AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
$binding.certificateHash
# this retrieves a cert and returns its expiration date, Woohooo!
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock { (Get-ChildItem -path Cert:\LocalMachine\WebHosting | Where-Object {$_.Thumbprint -eq "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" })[0].GetExpirationDateString() }
# this does not find a cert, and ive tried many things, and no dice.
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock { (Get-ChildItem -path Cert:\LocalMachine\WebHosting | Where-Object {$_.Thumbprint -eq $binding.certificateHash })[0].GetExpirationDateString() }
# i've tried extracting the hash via "tostring" and using that, no dice
$hash = $binding.certificateHash.ToString()
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock { (Get-ChildItem -path Cert:\LocalMachine\WebHosting | Where-Object {$_.Thumbprint -eq $hash })[0].GetExpirationDateString() }
# i've tried adding some wildcards and using the -like operator, no dice.
$hash = "*" + $binding.certificateHash + "*"
Start-Job Invoke-Command -ComputerName $serverName -ScriptBlock { (Get-ChildItem -path Cert:\LocalMachine\WebHosting | Where-Object {$_.Thumbprint -lilke $hash })[0].GetExpirationDateString() }
}
}
}
网站的示例输出。
- 站点 1
- D:\Apps\site1
- http
- *:80:Site1-test.ourdomain.com
- https
- *:443:Site1-test.ourdomain.com
- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- 虚拟主机
【问题讨论】:
-
哦,很有趣,所以当你调用命令时,你是在远程机器上调用它,所以你是在该机器的“上下文”中运行的。所以你说它不知道那个变量是什么......有没有办法在不硬编码的情况下传递该信息?
标签: powershell iis ssl-certificate invoke-command