I'm trying to run a custom Trellix on-demand scan of a directory using PowerShell and want to continue with my script after the scan is done. Here's my code snippet that triggers the scan. I'm redirecting the standard output and error from the process to return a custom object with the ExitCode and output details:
```powershell
function Invoke-Trellix {
$ScanCmdPath = "C:Program FilesMcAfeeEndpoint SecurityThreat Preventionamcfg.exe"
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $ScanCmdPath
$pinfo.Arguments = "/scan /task 501 /action start"
$pinfo.UseShellExecute = $false
$pinfo.RedirectStandardOutput = $true
$pinfo.RedirectStandardError = $true
$pinfo.CreateNoWindow = $true
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$stdOut = $p.StandardOutput.ReadToEnd()
$stdErr = $p.StandardError.ReadToEnd()
$p.WaitForExit()
[pscustomobject]@{
ExitCode = $p.ExitCode
StdOutput = $stdOut
StdError = $stdErr
}
}
```
When I execute this code, the standard output I get looks weird. Instead of a simple "Custom scan started", I see it as "C u s t o m s c a n s t a r t e d" with unnecessary spaces added between the characters. The output length reports 46 characters, but counting the displayed characters shows it should only be 38. Even simple matches like `$result.StdOutput -match 'C'` work, but more complex ones like `$result.StdOutput -match 'C u'` fail.
What's causing this strange character output in StandardOutput? Is there a better way to read it?
2 Answers
Good catch on checking the encoding. The way ISE handles the output is different from the console, which could lead to those spacing issues. If the null bytes are being counted as visible characters in your output, that would explain why your string length is off. I’d also suggest using `ReadToEnd()` only after making sure your process has exited to avoid partial data, just to ensure you're getting complete output.
It sounds like you're hitting an encoding issue, especially since you mention it works fine in a regular PowerShell window but not in ISE. The extra spaces you're seeing are likely null byte characters, which might happen if the output is UTF-16 but you're reading it as ASCII or UTF-8. Try setting the `StandardOutputEncoding` and `StandardErrorEncoding` properties for your ProcessStartInfo to Unicode like this:
```powershell
$pinfo.StandardOutputEncoding = [System.Text.Encoding]::Unicode
$pinfo.StandardErrorEncoding = [System.Text.Encoding]::Unicode
```
Also, make sure to read from both stdout and stderr to prevent deadlock. I’d recommend using separate threads for that if the process generates a lot of output.
As for your matching problem, remember that if there are null bytes, character combinations might behave unexpectedly. You could test with `Format-Hex` to confirm the data you're getting.
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String