用于修正的 PowerShell 脚本
本文包含客户可以实现或用作模板的示例脚本,以了解如何创建自己的脚本。 使用此处提供的信息为 修正创建脚本包。
脚本说明
此表显示了脚本名称、说明、检测、修正和可配置的项目。 名称以 Detect
开头的脚本文件是检测脚本。 修正脚本以 Remediate
开始。 可以从本文的下一节中复制这些脚本。
脚本名称 | 说明 |
---|---|
检查网络证书Detect_Expired_Issuer_Certificates.ps1 Remediate_Expired_Issuer_Certificates.ps1 |
检测 CA 在计算机或用户的个人存储区中颁发的已过期或即将过期的证书。
通过在检测脚本中更改 的值来 $strMatch 指定 CA。 将 $expiringDays 指定为 0 以查找过期证书,或指定另一个天数来查找即将过期的证书。
通过向用户发出 Toast 通知来修正。 $Title 使用希望用户看到的消息标题和文本指定 和 $msgText 值。
通知用户可能需要续订的过期证书。 使用登录凭据运行脚本:是 |
清除过时的证书Detect_Expired_User_Certificates.ps1 Remediate_Expired_User_Certificates.ps1 |
检测由当前用户的个人存储区中的 CA 颁发的过期证书。
通过在检测脚本中更改 的值来 $certCN 指定 CA。
通过从当前用户的个人存储中删除 CA 颁发的过期证书来修正。 通过在修正脚本中更改 的值来 $certCN 指定 CA。
从当前用户的个人存储中查找和删除 CA 颁发的过期证书。 使用登录凭据运行脚本:是 |
(内置) 更新过时的组策略Detect_stale_Group_Policies.ps1 Remediate_stale_GroupPolicies.ps1 |
检测上次组策略刷新是否已超过 7 days 之前。
此脚本包包含在修正中,但如果想要更改阈值,则会提供一个副本。 通过在检测脚本中更改 $numDays 的值来自定义七天阈值。
通过运行 gpupdate /target:computer /force 和 gpupdate /target:user /force 在通过组策略传递证书和配置时,帮助减少与网络连接相关的支持调用。 使用登录凭据运行脚本:是 |
检查网络证书脚本包
此脚本包检测计算机或用户的个人存储中 CA 颁发的已过期或即将过期的证书。 脚本通过引发对用户的 toast 通知来进行修正。
Detect_Expired_Issuer_Certificates.ps1
#=============================================================================================================================
#
# Script Name: Detect_Expired_Issuer_Certificates.ps1
# Description: Detect expired certificates issued by "CN=<your CA here>" in either Machine
# or User certificate store
# Notes: Change the value of the variable $strMatch from "CN=<your CA here>" to "CN=..."
# For testing purposes the value of the variable $expiringDays can be changed to a positive integer
# Don't change the $results variable
#
#=============================================================================================================================
# Define Variables
$results = @()
$expiringDays = 0
$strMatch = "CN=<your CA here>"
try
{
$results = @(Get-ChildItem -Path Cert:\LocalMachine\My -Recurse -ExpiringInDays $expiringDays | where {$_.Issuer -match $strMatch})
$results += @(Get-ChildItem -Path Cert:\CurrentUser\My -Recurse -ExpiringInDays $expiringDays | where {$_.Issuer -match $strMatch})
if (($results -ne $null)){
#Below necessary for Intune as of 10/2019 will only remediate Exit Code 1
Write-Host "Match"
Return $results.count
exit 1
}
else{
#No matching certificates, do not remediate
Write-Host "No_Match"
exit 0
}
}
catch{
$errMsg = $_.Exception.Message
Write-Error $errMsg
exit 1
}
Remediate_Expired_Issuer_Certificates.ps1
#=============================================================================================================================
#
# Script Name: Remediate_Expired_Issuer_Certificates.ps1
# Description: Raise a Toast Notification if expired certificates issued by "CN=..."
# to user or machine on the machine where detection script found them. No remediation action besides
# the Toast is taken.
# Notes: Change the values of the variables $Title and $msgText
#
#=============================================================================================================================
## Raise toast to have user contact whoever is specified in the $msgText
# Define Variables
$delExpCert = 0
$Title = "Title"
$msgText = "message"
# Main script
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
$APP_ID = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
$template = @"
<toast>
<visual>
<binding template="ToastText02">
<text id="1">$Title</text>
<text id="2">$msgText</text>
</binding>
</visual>
</toast>
"@
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($template)
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($toast)
清除过时的证书脚本包
此脚本包检测当前用户个人存储中 CA 颁发的过期证书。 脚本通过从当前用户的个人存储中删除 CA 颁发的过期证书来进行修正。
Detect_Expired_User_Certificates.ps1
#=============================================================================================================================
#
# Script Name: Detect_Expired_User_Certificates.ps1
# Description: Detect expired certificates issued by "CN=<your CA here>" to User
# Notes: Change the value of the variable $certCN from "CN=<your CA here>" to "CN=...".
# Don't change $results
#
#=============================================================================================================================
# Define Variables
$results = 0
$certCN = "CN=<your CA here>"
try
{
$results = Get-ChildItem -Path Cert:\CurrentUser\My -Recurse -ExpiringInDays 0 | where {$_.Issuer -eq($certCN)}
if (($results -ne $null)){
#Below necessary for Intune as of 10/2019 will only remediate Exit Code 1
Write-Host "Match"
Return $results.count
exit 1
}
else{
Write-Host "No_Match"
exit 0
}
}
catch{
$errMsg = $_.Exception.Message
Write-Error $errMsg
exit 1
}
Remediate_Expired_User_Certificates.ps1
#=============================================================================================================================
#
# Script Name: Remediate_Expired_User_Certificates.ps1
# Description: Remove expired certificates issued by "CN=<your CA here>" to User
# Notes: Change the value of the variable $certCN from "CN=<your CA here>" to "CN=..."
#
#=============================================================================================================================
# Define Variables
$certCN = "CN=<your CA here>"
try
{
Get-ChildItem -Path cert:\CurrentUser -Recurse -ExpiringInDays 0 | where {$_.Issuer -eq($certCN)} | Remove-Item
exit 0
}
catch{
$errMsg = $_.Exception.Message
Write-Error $errMsg
exit 1
}
更新过时的组策略脚本包
此脚本包包含在修正中,但如果想要更改阈值,则会提供一个副本。
此脚本包检测上次组策略刷新是否已超过 7 days
之前。 该脚本通过运行 gpupdate /target:computer /force
和 gpupdate /target:user /force
进行修正。
Detect_stale_Group_Policies.ps1
#=============================================================================================================================
#
# Script Name: Detect_stale_Group_Policies.ps1
# Description: Detect if Group Policy has been updated within number of days
# Notes: Remediate if "Match", $lastGPUpdateDays default value of 7, change as appropriate
#
#=============================================================================================================================
# Define Variables
try {
$gpResult = [datetime]::FromFileTime(([Int64] ((Get-ItemProperty -Path "Registry::HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Extension-List\{00000000-0000-0000-0000-000000000000}").startTimeHi) -shl 32) -bor ((Get-ItemProperty -Path "Registry::HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Extension-List\{00000000-0000-0000-0000-000000000000}").startTimeLo))
$lastGPUpdateDate = Get-Date ($gpResult[0])
[int]$lastGPUpdateDays = (New-TimeSpan -Start $lastGPUpdateDate -End (Get-Date)).Days
if ($lastGPUpdateDays -gt 7){
#Exit 1 for Intune. We want it to be within the last 7 days "Match" to remediate in SCCM
Write-Host "Match"
exit 1
}
else {
#Exit 0 for Intune and "No_Match" for SCCM, only remediate "Match"
Write-Host "No_Match"
exit 0
}
}
catch {
$errMsg = $_.Exception.Message
return $errMsg
exit 1
}
Remediate_stale_GroupPolicies.ps1
#=============================================================================================================================
#
# Script Name: Remediate_stale_GroupPolicies.ps1
# Description: This script triggers Group Policy update
# Notes: No variable substitution needed
#
#=============================================================================================================================
try {
$compGPUpd = gpupdate /force
$gpResult = [datetime]::FromFileTime(([Int64] ((Get-ItemProperty -Path "Registry::HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Extension-List\{00000000-0000-0000-0000-000000000000}").startTimeHi) -shl 32) -bor ((Get-ItemProperty -Path "Registry::HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Extension-List\{00000000-0000-0000-0000-000000000000}").startTimeLo))
$lastGPUpdateDate = Get-Date ($gpResult[0])
[int]$lastGPUpdateDays = (New-TimeSpan -Start $lastGPUpdateDate -End (Get-Date)).Days
if ($lastGPUpdateDays -eq 0){
Write-Host "gpupdate completed successfully"
exit 0
}
else{
Write-Host "gpupdate failed"
}
}
catch{
$errMsg = $_.Exception.Message
return $errMsg
exit 1
}
后续步骤
有关部署脚本包的信息,请参阅 修正。