#Контейнер, в котором проверяем пользователей - значение distinguishedName
$UsersBase = 'OU=ХХХ,DC=ХХХ,DC=ХХХ,DC=ХХХ'
#Период неактивности пользователя в домене
$TimeSpan = 90
$TimeStamp = (Get-Date).AddDays(-($TimeSpan+14))
$logFile = "inactiveUsers_$(Get-Date -Format "dd.MM.yyyy").csv"
#Учетная запись актавна и LogonTimestamp наступило раньше, чем $TimeSpan; или lastLogonTimestamp пустое, но при этом запись создана более $TimeSpan дней назад
Get-ADUser -Filter {(Enabled -eq $true) -and ((lastLogonTimestamp -le $TimeStamp) -or ((lastLogonTimestamp -notlike '*') -and (whenCreated -le $TimeStamp)))} -SearchBase $UsersBase -Properties lastLogonTimestamp,whenCreated | select Name,SamAccountName,@{Name="lastLogonTimestamp";Expression={if ($_.lastLogonTimestamp -eq $null) {''} else {[datetime]::FromFileTime($_.lastLogonTimestamp)}}},whenCreated | sort -Property Name | Export-Csv $logFile -Encoding utf8 -NoTypeInformation
function getConfig ($configFile){
if (Test-Path ((Get-Location).Path + "\$configFile")){
$config = ([xml]$(Get-Content $configFile)).configuration
if ($? -and $config){
return $config
}
else{
Write-Host 'Config file is incorrect'
}
}
else {
Write-Host 'Config file not found'
}
}
function init {
$files = $configuration.logFiles.File
foreach ($file in $files){
New-Item -Path ($(Get-Location).Path + "\Logs\$(Get-Date -Format 'yyyy')\$(Get-Date -Format 'MM')\$(Get-Date -Format 'dd')") -Name $file.value -ItemType File -Force | Out-Null
}
}
$ErrorActionPreference = 'SilentlyContinue'
$configFile = 'config.xml'
$configuration = getConfig ($configFile)
init
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<configuration>
<logFiles>
<File key="Full" value="logFull.log"/>
<File key="Warning" value="logWarning.log"/>
<File key="Error" value="logError.log"/>
</logFiles>
</configuration>