嵌套If语句优先-不相似条件

时间:2019-06-11 18:02:42

标签: powershell

我正在尝试使用嵌套的IF语句检查AD计算机属性,如果它既不是VM也不是VeraCrypt,请使用Manage-BDE检查Bitlocker状态。

当我仅使用Get-Adcomputer <name> -Properties extensionAttribute1 | select extensionAttribute1时,它将返回预期的结果VM。

当我这样做

$Attribute = Get-Adcomputer <name> -Properties extensionAttribute1 | select extensionAttribute1
Write-Host $Attribute 

我得到@{extensionAttribute1=VM}

我尝试使用-Notlike "*VM*进行了许多尝试,但这没有用。

这是我的代码。

# -----------------------
# Define global variables
# -----------------------

$ScanCount = 0
$UnprotectedCount = 0
$Date = Get-Date -Format yyyyMMdd
$StartDate = Get-Date -Format HH:mm:ss
$ReportFile = "C:\BitLocker_Status\BitLocker_Status.csv"
$BackupFile = "C:\BitLocker_Status\BitLocker_Status_Backup$BackupCopy'_$Date.csv"
$OutputArray = @()
$BackupCopy = 0
$SearchBase = "DC=Merallis,DC=net"

# ----------------------------------------
# Checking backing up output file
# ----------------------------------------

if (Test-Path $ReportFile) {

Rename-Item -Path $ReportFile -newname $BackupFile
$BackupCopy = $BackupCopy + 1
}
Else {
$BackupCopy = 0
}


# ----------------------------------------
# Build array from list of computers in AD
# ----------------------------------------

Write-Host -NoNewline "- Gathering a list of Computers from Active Directory..."
Try
{
   $Computers = Get-ADComputer -SearchBase $SearchBase -Filter * -Properties Name,Description | Sort-Object 
   Write-Host -ForegroundColor Green "Success"
}
Catch
{
   Write-Host -ForegroundColor Red "Failed ($_)"
}




# -------------------------------------------------
# Use the Manage-BDE command to query each computer
# -------------------------------------------------

Write-Host "- Querying BitLocker status..."
ForEach ($Computer in $Computers)
{
   $Name = $Computer.Name
   $Description = $Computer.Description
   $BDE = Manage-BDE -ComputerName $Computer.Name -Status C:
    # -------------------------------------------------
    # Use the Get-ADComputer command to query the current attribute for each computer
    # -------------------------------------------------
    $Attribute = Get-ADComputer $Name -Properties extensionAttribute1 | select extensionAttribute1


   Write-Host -nonewline "  - $Name ..."

   If ($Attribute -notlike "*VM*" -or $Attribute -notlike "*VeraCrypt*") {
   If ($BDE -Like "*An error occurred while connecting*") {Write-Host -ForegroundColor Yellow "Unable to connect"; $Status = "Unable to connect"; Set-ADComputer -identity $Name -Replace @{"ExtensionAttribute1"=$Status}}
   ElseIf ($BDE -Like "*Protection On*") {Write-Host -ForegroundColor Green "Protected"; $Status = "Protected"; Set-ADComputer -identity $Name -Replace @{"ExtensionAttribute1"=$Status}}
   ElseIf ($BDE -Like "*Protection Off*") {Write-Host -ForegroundColor Red $Status; $Status = "Not protected"; $UnprotectedCount = $UnprotectedCount + 1 ; Set-ADComputer -identity $Name -Replace @{"ExtensionAttribute1"=$Status}}
   ElseIf  ($BDE -Like "*The term 'manage-bde'*") {Write-Host -ForegroundColor Red "error manage-bd!"; $Status = "Not protected"; $UnprotectedCount = $UnprotectedCount + 1}
   Else {Set-ADComputer -identity $Name -Replace @{"ExtensionAttribute1"="Unknown"}}
   }
   $ScanCount = $ScanCount +1
   $OutputArray += New-Object PsObject -Property @{
   'Computer name' = $Computer.Name
   'Description' = $Computer.Description
   'BitLocker status' = $Status
   }
}



# -----------------
# Generate a report
# -----------------

Write-Host -NoNewline "- Saving report..."
Try
{
   $OutputArray | Export-CSV -NoTypeInformation $ReportFile
   Write-Host -ForegroundColor Green "Success"
}
Catch
{
   Write-Host -ForegroundColor Red "Failed ($_)"
}


# -----------------------------------------
# Display completion message and statistics
# -----------------------------------------

$EndDate = Get-Date -Format HH:mm:ss
$Duration = New-TimeSpan $StartDate $EndDate

Write-Host ""
Write-Host "-------------------------------------------------------------"
Write-Host "Script complete.  Start time: $StartDate, End time: $EndDate"
Write-Host "Scanned $ScanCount computers. $UnprotectedCount are unprotected!"
Write-Host "-------------------------------------------------------------"man
Write-Host ""

1 个答案:

答案 0 :(得分:0)

如果您要根据某些属性值从列表中过滤对象,则可以使用Where-Object

即网站列表(没有“默认” 网站名称

$YOURLIST = Get-Website
$YOURLIST | Where-Object  { $_.Name -notlike "*Default*" }