Import-Module ActiveDirectory
$date = (Get-Date).AddDays(-90)
$Managers = Get-ADUser -Filter {((lastlogontimestamp -lt $date) -or (lastlogontimestamp -notlike '*')) -and (whencreated -lt $date) -and ((PasswordLastSet -lt $date) -or (PasswordLastSet -notlike '*')) -and (PasswordNeverExpires -eq 'False')} -property * | Select SamAccountName,Name,CanonicalName, EmailAddress,PasswordLastSet,WhenCreated,PasswordExpired,PasswordNeverExpires, ProfilePath,Department,Title,Manager,@{Name="Disabled"; Exp={$_.useraccountcontrol -band 2}} |?{($_.Disabled -eq 0) -and ($_.PasswordExpired -eq 'True')} | select Manager | sort-object Manager -Unique
$msWord = New-Object -Com Word.Application
foreach ($Manager in $Managers){
$uman = Get-ADUser $Manager.Manager -property *
$fileusers = ".\data\" + $uman.name + ".html"
# $userswman = Get-ADUser -Filter {((lastlogontimestamp -lt $date) -or (lastlogontimestamp -notlike '*')) -and (whencreated -lt $date) -and ((PasswordLastSet -lt $date) -or (PasswordLastSet -notlike '*')) -and (PasswordNeverExpires -eq 'False') -and (manager -eq $uman)} -property * | Select SamAccountName,Name,CanonicalName,EmailAddress,PasswordLastSet,WhenCreated, PasswordExpired,PasswordNeverExpires,ProfilePath,Department, Title,Manager,@{Name="Disabled"; Exp={$_.useraccountcontrol -band 2}}|?{$_.Disabled -eq 0 -and $_.PasswordExpired -eq 'True'} | sort-object Name | Export-CSV $fileusers -encoding Default
$userswman = Get-ADUser -Filter {((lastlogontimestamp -lt $date) -or (lastlogontimestamp -notlike '*')) -and (whencreated -lt $date) -and ((PasswordLastSet -lt $date) -or (PasswordLastSet -notlike '*')) -and (PasswordNeverExpires -eq 'False') -and (manager -eq $uman)} -property * | Select SamAccountName,Name,EmailAddress,WhenCreated,Department,Title,Manager, PasswordExpired,lastlogontimestamp,@{Name="Disabled"; Exp={$_.useraccountcontrol -band 2}}|?{$_.Disabled -eq 0 -and $_.PasswordExpired -eq 'True'} | sort-object Name # | Export-CSV $fileusers -encoding Default
$userswman | ConvertTo-HTML | Out-File $fileusers
$wordDoc = $msWord.Documents.Open('.\doctemplate.docx')
$wordDoc.Activate()
#defines IP address input
$objRange = $wordDoc.Bookmarks.Item("Department").Range
$objRange.Text = $uman.Department
$wordDoc.Bookmarks.Add("Department",$objRange)
#defines IP address input
http://upload.iksrv.ru/store/20130611173418fMB/doctemplate.docx
$objRange = $wordDoc.Bookmarks.Item("Description").Range
$objRange.Text = $uman.Description
$wordDoc.Bookmarks.Add("Description",$objRange)
$objRange = $wordDoc.Bookmarks.Item("fio").Range
$objRange.Text = $uman.name
$wordDoc.Bookmarks.Add("fio",$objRange)
$uout = "ФИО сотрудника`tДолжность`tПоследний вход`n"
foreach ($ulist in $userswman){
if ($ulist.lastlogontimestamp -gt 0){
$llts = [datetime]::FromFileTime($ulist.lastlogontimestamp).ToString('g')
}
else {
$llts = "Отсутствовал"
}
$uout += $ulist.Name + "`t" + $ulist.Title + "`t" + $llts + "`n"
}
$uout >> .\data\_log.txt
$objRange = $wordDoc.Bookmarks.Item("Users").Range
$objRange.Text = $uout
$wordDoc.Bookmarks.Add("Users",$objRange)
$filename = ".\data\" + $uman.name + ".docx"
$wordDoc.SaveAs([REF]$filename)
$wordDoc.Close()
}
$msWord.Application.Quit()
http://upload.iksrv.ru/store/20130611173418fMB/doctemplate.docx