Share via


修復用の PowerShell スクリプト

この記事には、お客様が独自のスクリプトを作成する方法を学習するためのテンプレートとして実装または使用できるサンプル スクリプトが含まれています。 修復用のスクリプト パッケージを作成するには、ここに記載されている情報を使用 します

スクリプトの説明

次の表は、スクリプトの名前、説明、検出、修復、および構成可能な項目を示しています。 名前が Detect で始まるスクリプト ファイルは、検出スクリプトです。 修復スクリプトは Remediate から始まります。 これらのスクリプトは、この記事の次のセクションからコピーできます。

スクリプト名 説明
ネットワーク証明書を確認する
Detect_Expired_Issuer_Certificates.ps1
Remediate_Expired_Issuer_Certificates.ps1
有効期限が切れているか有効期限が近づいている、コンピューター、ユーザーいずれかの個人用ストアの CA によって発行された証明書を検出します。
検出スクリプトで の $strMatch 値を変更して、CA を指定します。 有効期限が切れた証明書を検索するには $expiringDays に 0 を指定し、有効期限が近づいた証明書を検索するには別の日数を指定します。

ユーザーにトースト通知を生成して修復します。
ユーザーに表示する $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 の値を変更すると、7 日間のしきい値をカスタマイズできます。

を実行gpupdate /target:computer /forceして修復します。証明書とgpupdate /target:user /force

構成がグループ ポリシー経由で配信される場合に、ネットワーク接続関連のサポート呼び出しを減らすことができます。

ログオンした資格情報を使用してスクリプトを実行する: はい

ネットワーク証明書スクリプト パッケージを確認する

スクリプト パッケージは、有効期限が切れているか有効期限が近づいている、コンピューター、ユーザーいずれかの個人用ストアの CA によって発行された証明書を検出します。 スクリプトは、ユーザーにトースト通知を表示することで修復します。

期限切れ_発行者_証明書_検出.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
}

期限切れ_発行者_証明書_修復.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 によって発行された、有効期限が切れた証明書を削除することで修復します。

期限切れ_ユーザー_証明書_検出.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
}

期限切れ_ユーザー_証明書_修復.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 /forcegpupdate /target:user /force を実行して修復します。

古い_グループ_ポリシー_検出.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
}

古い_グループ ポリシー_修復.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
}

次の手順

スクリプト パッケージのデプロイの詳細については、「 修復」を参照してください。