我正在编写一个脚本,它将在用户计算机上运行exe,然后提示他们将详细信息通过电子邮件发回给我。
问题代码是:
$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"
}
问题在于$ResultsOutput
被添加到邮件对象的$Mail.Body
属性中,
它似乎只将其截断为第一行 - $ResultsOutput
的内容为:
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..)
然后$Mail.Body
设置为:
DHCPUtil Results at 08/26/2016 13:36:02
Computer Name : COMPNAME
User Name : DOM\user
Results:
Starting Discovery ...
下面没有进一步的信息。
运行$ResultsOutput | Out-File .\ResultsOutput.txt
并在Notepad ++中打开,确认输出已CRLF
分开。
我也尝试过以下方法来排除格式化的奇怪之处。
$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()) }
最后一行通常是:
$ResultsOutput = "DHCPUtil Results at $(Get-Date)
Computer Name : $([System.Environment]::MachineName)
User Name : $([System.Environment]::UserDomainName)\$([System.Environment]::UserName)
Results:
$($Details)"
非常感谢您解决这个奇怪问题的任何帮助。
编辑 -
通过以下代码检索 $Details
:
$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
答案 0 :(得分:1)
您的$ResultsOutput
字符串包含0
字符。
您可以使用以下方式验证
# 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
然后解决方案是在将字符串中的0
字符放入邮件正文之前删除它们。
答案 1 :(得分:0)
尝试将$ResultsOutput
转换为[string]
个对象。我想现在是一个字符串数组。
$ResultsOutput= $ResultsOutput | Out-String