Hi,
I wrote a script that goes through accounts that are being deleted to find applications where the users are not controlled by AD. So they need to be removed from those systems. I wanted to have the output be the body of an email so I create it as a text file, read each line in to a PSObject and then convert it to HTML. It converts to HTML great, I can see that as I output the file to sanity check it was creating it properly. The script errors though when I try to insert the $Fileline as the email body with the error message posted below the script.
Cheers
Jonathan
##Groups that need to be checked
Param(
[array]$Applications= @("xa_AdobeWriter8","xa_AFLive","xa_APurchasingLive","xa_Bottomline","xa_Civica_ICON",`
"xa_CrystalReports","xa_DMS","xa_DRS_Live","xa_ExperianCitizenView","xa_Flare","xa_FMS","xa_FrameworkiLive",`
"xa_GCSXMail","xa_Highways_web","xa_InputAccel_Index7","xa_InputAccel_ValidationBen7","xa_InputAccel_ValidationCred7",`
"xa_Lagan_ECM_Live","xa_Project2007","xa_RAISE","xa_Servitor_V8_Live","xa_SX3","xa_Uniform_Live","xa_Visio2007","xa_YFAS")
)
##Get the date for file naming
$Date=Get-Date-FormatMMMMyyyy
##Email Stuff
$From="You"
$To="Me"
$MailSever="It"
##Create an empty text file that contains the date in the filename
New-Item-PathC:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt-ItemTypeFile
##Add a comment to the top of the file explaining what it is for
Add-Content"C:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt" "These are user accounts, of users who have left the business, whom used applications that are not"
Add-Content"C:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt" "AD integrated. Therefore the deletion process does not remove them from these systems"
Add-Content"C:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt" " "
Add-Content"C:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt" "For the following applications create a support ticket for each and assign to the relevant team"
Add-Content"C:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt" "For an application such as Raise or Uniform email the relevant administrator of that application"
##Loop through each security group and find users who are in the ToProcess Deletion OU and add to the $Date-Applications.txt file
##Add the name of the application, list the users discovered and separate each chunk with a row of dashes
Foreach ($ain$Applications){
Add-Content"C:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt" "----------------------------------"
Add-Content"C:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt"$a
Get-ADGroupMember-Identity$a|where-object {$_.distinguishedName -like"*OU=ToProcess,OU=Staff-Ex,OU=Users,OU=Here,DC=There,DC=EveryWhere,DC=uk"}`
|Select-Object-ExpandPropertyName `
|Add-Content"C:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt"
}
##Create a PSObject for later convertion to HTML
$File=Get-ContentC:\PurgeUserRecords\ApplicationUsers\$Date-Applications.txt
$FileLine= @()
Foreach ($Linein$File) {
$MyObject=New-Object-TypeNamePSObject
Add-Member-InputObject$MyObject-TypeNoteProperty-NameApplications-Value$Line
$FileLine+=$MyObject
}
##Convert $FileLine to HTML and save as a file (Proves valid output as file can be opened in a browser)
$FileLine|ConvertTo-Html|Out-FileC:\PurgeUserRecords\ApplicationUsers\$Date-Applications.html
##Trying to convert to a format that I can insert into an email
$FileLine=$FileLine|ConvertTo-Html-Fragment
##Send an email in HTML format with the $FileLine data as the body
Send-MailMessage-From$From-Subject"User to Application Matching for Ex-Staff $date"-To$To-Body$FileLine-BodyAsHtml-SmtpServer$MailServer
PS U:\> $Error[0]
Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
At C:\scripts\MonthlyPurgeApplicationCheck.ps1:54 char:140
+ ... *.***.uk -Body $FileLine -BodyAsHtml -SmtpServer ***.***.***.uk
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage