Hi everybody,
I have a question. I made a little script for adding Users in our AD.
Now I want to send a litte report after creating the with, so far so good.
After defiing the mailbody and the other stuff I get a E-Mail wich looks like this:
Normal 0 21 false false false DE X-NONE X-NONE
Write-Host " " Write-Host " " Write-Host "Das Anlegen des neuen Benutzers wurde erfolgreich druchgef?hrt!" Write-Host " " if ($mail -eq "j") { Write-Host "Daten werden gesammelt ..." $x = 5 $length = $x / 100 while($x -gt 0) { $min = [int](([string]($x/60)).split('.')[0]) $text = "" + ($x % 60) + " Sekunden verbleibend ..." Write-Progress"Pause ..." -status $text -perc ($x/$length) start-sleep -s 1 $x-- } Get-Aduser -identity $UserDN | fl GivenName,Surname,SamAccountName Get-ADUser -identity $UserDN -properties AccountExpirationDate | `Where-Object{$_.AccountExpirationDate -lt (Get-Date) -and $_.AccountExpirationDate -ne $null} | select-object Givenname, Surname, samaccountname, `AccountExpirationDate Write-Host "User-Password: sZrkrvKx!" Write-Host "E-Mail Adresse:" (Get-ADUser $UserDN -properties mail).mail Write-Host " " Write-Host " " Write-Host "User Berechtigungen:" $UN = Get-ADUser $UserDN -Properties MemberOf $Groups = ForEach ($Group in ($UN.MemberOf)) { (Get-ADGroup $Group).Name } $Groups = $Groups | Sort ForEach ($Group in $Groups) { New-Object PSObject -Property @{ #Name = $UN.Name Group = $Group } } } else { Get-Aduser -identity $UserDN | fl GivenName,Surname,SamAccountName Write-Host " " Write-Host "User-Password: XXXXXXX" Write-Host " " Write-Host " " Write-Host "User Berechtigungen:" $UN = Get-ADUser $UserDN -Properties MemberOf $Groups = ForEach ($Group in ($UN.MemberOf)) { (Get-ADGroup $Group).Name } $Groups = $Groups | Sort ForEach ($Group in $Groups) { New-Object PSObject -Property @{ #Name = $UN.Name Group = $Group } } }
My Powershell code looks like this:
$mailbody = {
Write-Host " "
Write-Host " "
Write-Host "Das Anlegen des neuen Benutzers wurde erfolgreich druchgeführt!"
Write-Host " "
if
($mail -eq "j")
{
Write-Host "Daten werden gesammelt ..."
$x = 5
$length = $x / 100
while($x -gt 0) {
$min = [int](([string]($x/60)).split('.')[0])
$text = " " + ($x % 60) + " Sekunden verbleibend ..."
Write-Progress "Pause ..." -status $text -perc ($x/$length)
start-sleep -s 1
$x--
}
Get-Aduser -identity $UserDN | fl GivenName,Surname,SamAccountName
Get-ADUser -identity $UserDN -properties AccountExpirationDate | `Where-Object{$_.AccountExpirationDate -lt (Get-Date) -and $_.AccountExpirationDate -ne $null} | select-object Givenname, Surname, samaccountname, `AccountExpirationDate
Write-Host "User-Password: XXXXXXX"
Write-Host "E-Mail Adresse:"
(Get-ADUser $UserDN -properties mail).mail
Write-Host " "
Write-Host " "
Write-Host "User Berechtigungen:"
$UN = Get-ADUser $UserDN -Properties MemberOf
$Groups = ForEach ($Group in ($UN.MemberOf))
{ (Get-ADGroup $Group).Name
}
$Groups = $Groups | Sort
ForEach ($Group in $Groups)
{ New-Object PSObject -Property @{
#Name = $UN.Name
Group = $Group
}
}
}
else
{
Get-Aduser -identity $UserDN | fl GivenName,Surname,SamAccountName
Write-Host " "
Write-Host "User-Password: XXXXXX"
Write-Host " "
Write-Host " "
Write-Host "User Berechtigungen:"
$UN = Get-ADUser $UserDN -Properties MemberOf
$Groups = ForEach ($Group in ($UN.MemberOf))
{ (Get-ADGroup $Group).Name
}
$Groups = $Groups | Sort
ForEach ($Group in $Groups)
{ New-Object PSObject -Property @{
#Name = $UN.Name
Group = $Group
}
}
}
}
Send-MailMessage -SmtpServer mailsrv -To mail@me.org -Subject "Report zu $UserDN" -BodyAsHtml $mailbody -From send@mail.org
I think everybody knows what I want to do right?
So can anybody tell me where mit mistake is?