Hi @Ian Xue (Shanghai Wicresoft Co., Ltd.) What i meant by accurate results is to verify if Last Sync Activity between Offline Cache and Netshare was greater than 1 week, if yes send an Email alert to the admin but the CIM Classes win32_OfflineFilesCache
doesnt include classes to check last sync status for Offline File Cache so i decided to use the Last Read /Write Activity to the C:\Windows\CSC
or Synced Netshare in order to get the LastWrite Activity (TimeStamp) and then compare if it was more than one week. I dont know if that is enough or accurate. I also found https://learn.microsoft.com/en-us/previous-versions/windows/desktop/offlinefiles/win32-offlinefilesfilesysinfo . I also tried filtering through Win32_UserProfile but the information there wasn't that useful. There was a similar issue reported on this post https://social.technet.microsoft.com/Forums/ie/en-US/cdc831e8-1431-4c6a-9b3d-b835da8b7fbd/redirected-folder-offline-files-health?forum=winserverfiles. I have the second version of the Script below with a few changes like using Get-CimInstance instead of Get-wmiobject in some cases and adding a few more conditions based on the use case unique to us. I couldn't use the assigned variables because results were inconsistent using them, i dont know if it was due to the datatypes etc. Some recommendations and/or improvements will be very much appreciated.
Unused Variables
#$LastSyncTimeGreater = Get-CimInstance -Class Win32_UserProfile | Select Win32_FolderRedirectionHealth | where {$_.LastSuccessfulSyncTime -ge "(Get-Date).AddDays(-7)" -or $_.LastSyncTime -ge "(Get-Date).AddDays(-7)" -or $_.LastSyncStatus -eq "3" -and $_.OfflineAccessEnabled -eq "$true" -and $_.redirected -eq "$true"}
$Username = Get-CimInstance -class Win32_ComputerSystem | select username | Out-String
$CSCInactive = Get-wmiobject -computername localhost -Namespace root\CIMV2 -Query "Select * from Win32_OfflineFilesCache" | where {$_.Active -match "$False" -and $_.Enabled -eq "$True"}
$CSCDisabled = Get-wmiobject -computername localhost -Namespace root\CIMV2 -Query "Select * from Win32_OfflineFilesCache" | where {$_.Active -match "$False" -and $_.Enabled -eq "$False"}
$RebootRequired = Invoke-CimMethod -ClassName Win32_OfflineFilesCache -MethodName Enable -Arguments @{Enable = $false} | where {$_.RebootRequired -eq "$True"}
#$LastWriteNetShare = Get-Childitem "\\NetShare\HomeDrive\" | Select LastWriteTime | where {$_.LastWirteTime -le $WeekOld}
$LastWriteCSC = Get-item C:\Windows\CSC | Select LastWriteTime | where {$_.LastWirteTime -le $WeekOld}
$WeekOld =(Get-Date).AddDays(-7)
$Username = Get-CimInstance -class Win32_ComputerSystem | select username | Out-String
Start Script Block
#Verify if VPN is connected if not, notify user to connect in order to proceed
if(Get-CimInstance -class win32_networkadapter | where {$_.netconnectionstatus -eq "2" -and $_.netConnectionid -match "Ethernet 2" -and $_.name -like "VPN Client"}){
Write-Host "VPN Connected, We shall go ahead and conduct Offline Cache Analysis" -ForegroundColor "Green"
#No further Action Required in case Offline Cache is already disabled, you proceed with Migration
If(Get-wmiobject -computername localhost -Namespace root\CIMV2 -Query "Select * from Win32_OfflineFilesCache" | where {$_.Active -match "$False" -and $_.Enabled -eq "$False"}){
Write-Host "No further Action Required, Offline Cache is already disabled, You can commence with your Migration"
}
#If Offline Cache is enabled, check last Sync and Notify Administrator in case Last Sync Activity is older than a week. If Offline Cache is Disabled but requires reboot, pls go ahead and reboot.
if((Get-wmiobject -computername localhost -Namespace root\CIMV2 -Query "Select * from Win32_OfflineFilesCache" | where {$_.Active -eq "$True" -and $_.Enabled -eq "$True" -or $_.Active -eq "$False"}) -and (Get-Item "C:\Windows\CSC" | Select LastWriteTime | where {$_.LastWirteTime -gt $WeekOld})){
Send-MailMessage -to "Offline Cache Analysis <admin@domain.com>" -from "LastSyncActivityReport <admin@domain.com>" -subject "Offline Cache Last Sync Activity" -Body "$($Username|Out-String;$LastWriteCSC|Out-String)" -smtpserver "Xchange.comm.net"
}
if((Get-wmiobject -computername localhost -Namespace root\CIMV2 -Query "Select * from Win32_OfflineFilesCache" | where {$_.Active -eq "$True" -or $_.Enabled -eq "$True"}) -and (Get-Item "C:\Windows\CSC" | where {$_.LastWirteTime -le $WeekOld})) {
Invoke-CimMethod -ClassName Win32_OfflineFilesCache -MethodName Enable -Arguments @{Enable = $false}
}
if((Get-wmiobject -computername localhost -Namespace root\CIMV2 -Query "Select * from Win32_OfflineFilesCache" | where {$_.Active -match "$True" -and $_.Enabled -eq "$False"}) -or (Invoke-CimMethod -ClassName Win32_OfflineFilesCache -MethodName Enable -Arguments @{Enable = $false} | where {$_.RebootRequired -eq "$True"})){
write-host "Computer will Force restart in order to disable offline Cache. Run the Script again after restarting Computer" -F Yellow
sleep -Seconds 10
restart-computer -Force
}
}
Else{Write-Host "Your VPN is disconnected, Please Connect Your VPN and run the Script Again" -ForegroundColor "Red"}