Permet de supprimer les machines supérieur à 60 jours dans Appsense
<#
.NOTES
===========================================================================
Created with: Windows Powershell ISE
Version : v1.4
By : Troxsa
===========================================================================
.DESCRIPTION
A description of the file.
Delete the computer in the Appsense
#>
Clear-Host
$TitleEventLog = "Appsense Delete Computer"
$AppsenseWebSite = [String]http://YourServerAppsense
$NumberDaysMax = 60
#===========================================================================
#===========================================================================
#===========================================================================
# DO NOT TOUCH
#===========================================================================
#===========================================================================
#===========================================================================
function WriteEventlog
{ [CmdletBinding()]
Param
( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("Content")]
[string]$Message,
[Parameter(Mandatory=$false)]
[ValidateSet("Error","Warn","Info")]
[string]$Level="Info"
)
Begin
{ # Set VerbosePreference to Continue so that verbose messages are displayed.
$VerbosePreference = 'Continue'
New-EventLog -LogName Application -Source $TitleEventLog -ErrorAction Ignore
}
Process
{
switch ($Level) {
'Error' {
Write-Error $Message
Write-EventLog -LogName Application -Source $TitleEventLog -EntryType Error -EventId 99 -Message $Message
}
'Warn' {
Write-Warning $Message
Write-EventLog -LogName Application -Source $TitleEventLog -EntryType Warning -EventId 50 -Message $Message
}
'Info' {
Write-Verbose $Message
Write-EventLog -LogName Application -Source $TitleEventLog -EntryType Information -EventId 1 -Message $Message
}
}
}
}WriteEventlog -Message "Start Scan on the serveur : $($AppsenseWebSite)" -Level Info
If(($AppsenseWebSite -match "/$") -eq $false)
{$AppsenseWebSite = $AppsenseWebSite.Insert($AppsenseWebSite.Length, "/")
WriteEventlog -Message "New URL with the char '/' : $($AppsenseWebSite)" -Level Info
}
$AppsenseServer = New-WebServiceProxy -Uri ($AppsenseWebSite + "managementserver/DataAccess/Machines.asmx") -UseDefaultCredential
$AppsenseGroup = New-WebServiceProxy -Uri ($AppsenseWebSite + "managementserver/DataAccess/Groups.asmx") -UseDefaultCredential
$AppsenseMachines = $AppsenseServer.GetMachines($false).Machines
$AppsenseGroups = $AppsenseGroup.GetGroups($false).Groups
$i = 0
foreach($item in $AppsenseMachines)
{
$i = $i + 1
If($item.LastResponseSeconds.GetType().Name -ne "DBNull")
{ If(([timespan]::fromseconds($item.LastResponseSeconds).Days -gt [int]$NumberDaysMax))
{
$AppsenseServer.DeleteMachine($item.MachineKey, $null)
WriteEventlog -Message "Delete the computer : $($item.NetBiosName)`n`r
MachineKey : $($item.MachineKey )
GroupKey : $($item.GroupKey )
GroupName : $($item.GroupName )
Platform : $($item.Platform )
NetBiosName : $($item.NetBiosName )
DistinguishedName : $($item.DistinguishedName )
OldDistinguishedName: $($item.OldDistinguishedName)
ObjectGuid : $($item.ObjectGuid )
LastPollTime : $($item.LastPollTime )
LastPollStatus : $($item.LastPollStatus )
LastUploadTime : $($item.LastUploadTime )
LastUploadStatus : $($item.LastUploadStatus )
IsPendingDeletion : $($item.IsPendingDeletion )
AlertCount : $($item.AlertCount )
CreationTime : $($item.CreationTime )
ModifiedTime : $($item.ModifiedTime )
ModifiedGroupTime : $($item.ModifiedGroupTime )
DiagnosticsError : $($item.DiagnosticsError )
DiagnosticsState : $($item.DiagnosticsState )
DiagnosticsTime : $($item.DiagnosticsTime )
Deployed : $($item.Deployed )
DeployError : $($item.DeployError )
Offline : $($item.Offline )
DNS : $($item.DNS )
LastResponseSeconds : $($item.LastResponseSeconds )`r
The computer : $($item.NetBiosName) did not responding since : $([timespan]::fromseconds($item.LastResponseSeconds).Days)
" -Level Warn
} Else
{Write-Host $item.NetBiosName
Write-Host "$($i) / $($AppsenseMachines.Count) The computer : $($item.NetBiosName) did not responding since : $([timespan]::fromseconds($item.LastResponseSeconds).Days)"
}
}
}