4

I am writing a script which will run an exe on a users machine, then prompt them to email the details back to me.

The problem code is this:

$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 issue is with $ResultsOutput being added to the $Mail.Body attribute of the mail object, It appears to truncate it to only the first line - the contents of $ResultsOutput is:

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.Body is set to:

DHCPUtil Results at 08/26/2016 13:36:02 
Computer Name : COMPNAME
User Name : DOM\user 
Results: 
Starting Discovery ...

With no further information below it.

running $ResultsOutput | Out-File .\ResultsOutput.txt and opening in Notepad++ confirms the output is CRLF seperated.

I have also tried the below to rule out something strange with the 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()) }

Where the last line is normally:

$ResultsOutput = "DHCPUtil Results at $(Get-Date)
Computer Name : $([System.Environment]::MachineName)
User Name : $([System.Environment]::UserDomainName)\$([System.Environment]::UserName)
Results:
$($Details)"

Any help to resolve this strange issue would be much appreciated.

edit -

$Details is retrieved via the below code:

$ProcInfo = [System.Diagnostics.ProcessStartInfo]::new() #Create 'info' object for Process
$ProcInfo.Arguments = "-EmulateClient" #Add the argument required
$ProcInfo.FileName = $TempFileName #Point to temp copy of DHCPUtil
$ProcInfo.UseShellExecute = $False #Required for getting the output
$ProcInfo.CreateNoWindow = $True #Hide the popup
$ProcInfo.RedirectStandardOutput = $True #So we can retrieve it as a string
$Results = [System.Diagnostics.Process]::Start($ProcInfo) #Launch the process
Write-Host "Waiting for DHCP Discovery (This can take some time!)" -ForegroundColor Green
$Results.WaitForExit() #Wait for DHCPUtil to complete
$Details = $Results.StandardOutput.ReadToEnd() #Get all of the results after it has completed
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
colsw
  • 3,216
  • 1
  • 14
  • 28
  • 1
    Instead of using .Body try assigning the string with `
    ` for new lines to `$Mail.HTMLBody`.
    – wOxxOm Aug 26 '16 at 13:01
  • I've tried assigning the string to HTMLBody - it's all on one line (didn't add the break tags) however it's cut off in the exact same place. – colsw Aug 26 '16 at 13:08
  • How are you constructing `$ResultsOutput`? Can you check what the output of `$ResultsOutput[-1]` is? – Poorkenny Aug 26 '16 at 13:17
  • the bottom two code blocks there are my constructors for the text block, i've tried both methods, and they both yield identical results. edit - $ResultsOutput is definitely just a multiline string - not a string array. – colsw Aug 26 '16 at 13:20
  • Appended to the question - it's an exe output from DHCP util - `CRLF` seperated single string output. – colsw Aug 26 '16 at 13:53
  • I add it both directly, and by splitting it and appending it to the block, neither seems to work. `Results: $($Details)"` – colsw Aug 26 '16 at 13:55
  • 1
    if this is confusing i've put the entire script up here: http://pastebin.com/rHZGtiK9 – colsw Aug 26 '16 at 13:59
  • Can you post the results of `[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($ResultsOutput))` – Lieven Keersmaekers Aug 26 '16 at 14:11
  • too long for a comment and i'd prefer not to clutter the op too much - http://pastebin.com/2agYWN5c – colsw Aug 26 '16 at 14:34

2 Answers2

1

Your $ResultsOutput string contains a 0 character.

You can use following to verify

# Using the Base64 string you've posted to populate $ResultsOutput
$ResultsOutput = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("REhDUFV0aWwgUmVzdWx0cyBhdCAwOC8yNi8yMDE2IDE1OjMzOjE4DQpDb21wdXRlciBOYW1lIDogTVNQMTBCMTINCklQIEFkZHJlc3MgOiAxNzIuMjUuMjUuMTMzIChEaGNwKQ0KVXNlciBOYW1lIDogSFRHXHN3ZWVuZXljDQpSZXN1bHRzOg0KU3RhcnRpbmcgRGlzY292ZXJ5IC4uLgANClNlbmRpbmcgUGFja2V0IChTaXplOiAyODQsIE5ldHdvcmsgQWRhcHRl")))

$ResultsOutput.Contains("`0") # Returns True
$ResultsOutput.IndexOf("`0") # Returns 160 which is right after "Discovery ..."

$ResultsOutput[147..159] # Outputs Discovery ... just up until the 0 character

The solution then would be to remove 0 characters from you string before putting them in the body of your mail.

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
0

Try to convert you $ResultsOutput to a [string] object. I guess it is a string array right now.

$ResultsOutput= $ResultsOutput | Out-String
restless1987
  • 1,558
  • 10
  • 16