In addition to the true/false problem IanXue pointed out, you have an undefined variable named "$OSCInactive" you're checking for the property "RebootRequired", and a missing set of "{" and "}" surrounding the Send-MailMessage
You can also eliminate most of your "Select-Object" cmdlets since you only creating a PSCustomObject that holds the same property names that are present in the object from which you're extracting them.
I cleaned up the code a bit (except for the undefined object problem):
$LastSyncTime = Get-WmiObject Win32_FolderRedirectionHealthConfiguration |
Where-Object { $.LastSyncDurationCautionInHours -ge "240" -and $.LastSyncDurationUnhealthyInHours -ge "480" }
$Username = Get-WmiObject -class Win32_ComputerSystem |
Select-Object username |
Out-String
#Check VPN is connected if not alert user to connect
#Check Offline Cache is enabled and active if yes check last sync Activity if more than a week write out Last Syn Date & USer Account info my email
if ((Get-WmiObject -class win32_networkadapter |
Where-Object { $_.netconnectionstatus -match "2" -and $_.netConnectionid -match "Ethernet 2" -and $_.name -like "VPN Client Name" }
) -and
(Get-WmiObject -computername localhost -Namespace root\CIMV2 -Query "Select * from Win32_OfflineFilesCache" |
Where-Object { $_.Active -and $_.Enabled}
) -and
(Get-WmiObject Win32_FolderRedirectionHealthConfiguration |
Where-Object { $_.LastSyncDurationCautionInHours -ge "240" -and $_.LastSyncDurationUnhealthyInHours -ge "480" }
)
){
Send-MailMessage -to "Offline Cache Analysis <xyz@Champ.com>" -from "LastSyncReport <xyz@Champ.com>" -subject "Offline File Last Sync" -Body "$($Username;$LastSyncTime)" -smtpserver "Champ.com"
}
#If Offline Sync activity is less than a week check lsimply Deactivate, reboot where required.
if ((Get-WmiObject -computername localhost -Namespace root\CIMV2 -Query "Select * from Win32_OfflineFilesCache" |
Where-Object { $_.Active -and $_.Enabled}
) -and
(Get-WmiObject Win32_FolderRedirectionHealthConfiguration |
Where-Object { $_.LastSyncDurationCautionInHours -lt "240" -and $_.LastSyncDurationUnhealthyInHours -lt "480" }
)
){
Invoke-CimMethod -ClassName Win32_OfflineFilesCache -MethodName Enable -Arguments @{Enable = $false }
if ($OSCInactive.RebootRequired) {
Write-Host "Computer will force restart to in order to disable offline Cache." -F Yellow
Start-Sleep -Seconds 5
Restart-Computer -Force
}
}
else{
Write-Host "Your VPN is disconnected, :( Please Connect Your VPN and run the Script Again" -ForegroundColor "Red"
}