我正在尝试创建一个脚本,它将自动检查BitLocker状态,如果没有启用,则发送电子邮件。
以下是我到目前为止所拥有的:
Get-BitlockerVolume -MountPoint "C:" | Select ProtectionStatus这显示了我的状态,但现在我很难处理输出。我试过这样做:
$OutputVariable = (Get-BitlockerVolume -MountPoint "C:" | Select
ProtectionStatus)
If ($OutputVariable -like "Off") {Echo "Oops"}
Else {Echo "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)
}发布于 2018-02-26 14:21:36
PowerShell返回对象。您可以使用Select cmdlet将这些对象的属性简化为您感兴趣的对象。
因此,以下命令:
Get-BitlockerVolume -MountPoint "C:" | Select ProtectionStatus返回一个具有单个"ProtectionStatus“属性的对象,因此将其与字符串进行比较不会导致匹配。
相反,您可以通过点表示法(例如$OutputVariable.protectionstatus)访问该属性,以便对其内容执行比较。或者,您可以修改Select cmdlet以使用-ExpandProperty,它将返回指定属性值作为其类型的对象:
$OutputVariable = Get-BitlockerVolume -MountPoint "C:" | Select -ExpandProperty ProtectionStatus实现同样结果的另一种方法是:
$OutputVariable = (Get-BitlockerVolume -MountPoint "C:").ProtectionStatus在这里,括号使cmdlet执行,但是我们使用点表示法只返回指定的属性。
发布于 2018-02-26 13:42:57
$OutputVariable = (Get-BitlockerVolume -MountPoint "C:")
If ($OutputVariable.protectionstatus -like "Off")
{
Write-Output "Oops"
}
Else
{
Write-Output "Wow!"
}尝尝这个
https://stackoverflow.com/questions/48989853
复制相似问题