Description :
This script checks if the number of synced files in OneDrive is <280000 files then it does nothing, If its greater than 280000 it throws a alert(1) else alert (0).
#!/usr/bin/env python
# coding: utf-8
# In[6]:
ps_content=r'''
$IniFiles = Get-ChildItem "$ENV:LOCALAPPDATA\Microsoft\OneDrive\settings\Business1" -Filter 'ClientPolicy*' -ErrorAction SilentlyContinue
if (!$IniFiles)
{ write-host "No Onedrive configuration files found. Stopping script." exit 1 }
$SyncedLibraries = foreach ($inifile in $IniFiles) {
$IniContent = get-content $inifile.fullname -Encoding Unicode
[PSCustomObject]@{
'Item Count' = ($IniContent | Where-Object \{ $_ -like 'ItemCount*' }) -split '= ' | Select-Object -last 1
'Site Name' = ($IniContent | Where-Object { $_ -like 'SiteTitle*' }) -split '= ' | Select-Object -last 1
'Site URL' = ($IniContent | Where-Object { $_ -like 'DavUrlNamespace*' }) -split '= ' | Select-Object -last 1
}
}
if (($SyncedLibraries.'Item count' | Measure-Object -Sum).sum -gt '280000')
{ write-host "Alert Unhealthy - OneDrive is Currently syncing more than 280k files. Please investigate." $SyncedLibraries }
else
{ write-host "Healthy - OneDrive Syncing less than 280k files." }
'''
import os
import sys
def ecmd(command):
import ctypes
from subprocess import PIPE, Popen
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
with disable_file_system_redirection():
obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
out, err = obj.communicate()
ret=obj.returncode
if ret==0:
if out:
return out.strip()
else:
return ret
else:
if err:
return err.strip()
else:
return ret
file_name='powershell_file.ps1'
file_path=os.path.join(os.environ['TEMP'], file_name)
with open(file_path, 'wb') as wr:
wr.write(ps_content)
ecmd('powershell "Set-ExecutionPolicy RemoteSigned"')
output=ecmd('powershell "%s"'%file_path)
#print output[0:25]
ls=output.split()
def alert(arg):
sys.stderr.write("%d%d%d" % (arg, arg, arg))
k=''
for i in ls:
if i=="Healthy" or i=="Unhealthy":
k=k+i
if k !='Healthy':
alert(1)
else:alert(0)
# In[ ]:
Comments