I am writing a script that will run exe on the user's computer and then prompt them to send me detailed information.
Problem Code:
$ResultsOutput = "DHCPUtil Results at $(Get-Date)
Computer Name : $([System.Environment]::MachineName)
IP Address :
User Name : $([System.Environment]::UserDomainName)\$([System.Environment]::UserName)
Results:"
$Details -split "`r`n" | % { $ResultsOutput += ([System.Environment]::NewLine + $_.Trim()) }
$Outlook = New-Object -ComObject Outlook.Application
if ($Outlook -ne $null)
{
$Mail = $Outlook.CreateItem(0)
$Mail.Subject = "DHCPUtil Results"
$Mail.Body = $ResultsOutput
$Mail.Display()
}
else
{
$Desktop = (Get-Item "~\desktop").FullName
Write-Host "Outlook not found - saving results to $($Desktop)\DHCPResults.txt"
$ResultsOutput | Out-File "$($Desktop)\Desktop\DHCPResults.txt"
}
The problem is adding a mail object $ResultsOutputto the attribute $Mail.Body. It seems that it truncates it only to the first line - the contents $ResultsOutputare:
DHCPUtil Results at 08/26/2016 13:36:02
Computer Name : COMPNAME
User Name : DOM\user
Results:
Starting Discovery ...
Sending Packet (Size: 284, Network Adapter: 0.0.0.0, Attempt Type: Broadcast + Unicast)
--Begin Packet--
DHCP: INFORM (xid=0)
(more below..)
And then $Mail.Bodyinstalled:
DHCPUtil Results at 08/26/2016 13:36:02
Computer Name : COMPNAME
User Name : DOM\user
Results:
Starting Discovery ...
No further information below.
running $ResultsOutput | Out-File .\ResultsOutput.txtand opening in Notepad ++ confirms the conclusion CRLF.
I also tried the following to eliminate something strange while formatting.
$ResultsOutput = "DHCPUtil Results at $(Get-Date)
Computer Name : $([System.Environment]::MachineName)
User Name : $([System.Environment]::UserDomainName)\$([System.Environment]::UserName)
Results:"
$Details -split "`r`n" | % { $ResultsOutput += ([System.Environment]::NewLine + $_.Trim()) }
If the last line is usually:
$ResultsOutput = "DHCPUtil Results at $(Get-Date)
Computer Name : $([System.Environment]::MachineName)
User Name : $([System.Environment]::UserDomainName)\$([System.Environment]::UserName)
Results:
$($Details)"
Any help in resolving this strange problem would be greatly appreciated.
edit -
$Details retrieved through the code below:
$ProcInfo = [System.Diagnostics.ProcessStartInfo]::new()
$ProcInfo.Arguments = "-EmulateClient"
$ProcInfo.FileName = $TempFileName
$ProcInfo.UseShellExecute = $False
$ProcInfo.CreateNoWindow = $True
$ProcInfo.RedirectStandardOutput = $True
$Results = [System.Diagnostics.Process]::Start($ProcInfo)
Write-Host "Waiting for DHCP Discovery (This can take some time!)" -ForegroundColor Green
$Results.WaitForExit()
$Details = $Results.StandardOutput.ReadToEnd()