用于检查 Bitlocker 状态并在关闭时发送电子邮件的 Powershell 脚本

Powershell script to check Bitlocker Status and email if Off

我正在尝试制作一个脚本来自动检查 BitLocker 状态,如果未启用则发送电子邮件。

这是我目前的情况:

Get-BitlockerVolume -MountPoint "C:" | Select ProtectionStatus

这显示了我的状态,但现在我正在努力处理输出。我试过这样做:

$OutputVariable = (Get-BitlockerVolume -MountPoint "C:" | Select 
ProtectionStatus)
If ($OutputVariable -like "Off") {Echo "Oops"}
Else {Echo "Wow!"}

如果我理解正确,应该输出 "Oops",但它一直显示 "Wow!"

也许我做错了,所以我正在寻找一些指导。

编辑:

多亏了下面的评论,我才能让它发挥作用。这是我的完整脚本:

# Bitlocker Script

set-alias ps64 "$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe" 
set-alias ps32 "$env:windir\syswow64\WindowsPowerShell\v1.0\powershell.exe"
ps64 {Import-Module BitLocker; Get-BitlockerVolume}
$wmiDomain = Get-WmiObject Win32_NTDomain -Filter "DnsForestName = '$( (Get-WmiObject Win32_ComputerSystem).Domain)'"
$domain = $wmiDomain.DomainName
$OutputVariable = (ps64 {Get-BitlockerVolume -MountPoint "C:"})
If ($OutputVariable.volumestatus -like "FullyEncrypted") 
{
Exit
} 
ElseIf ($OutputVariable.volumestatus -NotLike "FullyEncrypted") 
{
$date = Get-Date
$emailSmtpServer = "smtp.xxx.com"
$emailSmtpServerPort = "xxx"
$emailSmtpUser = "xxx@xxx.nl"
$emailSmtpPass = "xxx"

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "Report <xxx@xxx.nl>"
$emailMessage.To.Add( "xxx@xxx.net" )
$emailMessage.Subject = "Bitlocker Status Alert | $domain $env:COMPUTERNAME"
$emailMessage.Body = "Bitlocker niet actief op $domain $env:COMPUTERNAME getest op $date"

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );


$SMTPClient.Send( $emailMessage)
}
$OutputVariable = (Get-BitlockerVolume -MountPoint "C:")
If ($OutputVariable.protectionstatus -like "Off") 
{
    Write-Output "Oops"
} 
Else 
{
    Write-Output "Wow!"
}

试试这个

PowerShell returns 个对象。您使用 Select cmdlet 将这些对象的属性简化为您感兴趣的对象。

因此执行以下命令:

Get-BitlockerVolume -MountPoint "C:" | Select ProtectionStatus

Returns 具有单个 "ProtectionStatus" 属性 的对象,因此将其与字符串进行比较不会导致匹配。

您可以改为通过点符号(例如 $OutputVariable.protectionstatus)访问 属性 以对其内容进行比较。或者,您可以修改 Select cmdlet 以使用 -ExpandProperty,这将 return 指定 属性 的 作为对象它的类型:

$OutputVariable = Get-BitlockerVolume -MountPoint "C:" | Select -ExpandProperty ProtectionStatus

实现相同结果的另一种方法是:

$OutputVariable = (Get-BitlockerVolume -MountPoint "C:").ProtectionStatus

此处括号使 cmdlet 执行,但随后我们使用点符号来仅 return 指定的 属性.