I have a script that runs locally to retrieve system info. I’m using PDQ Deploy to copy the script as part of the new PC setup process. PDQ installs teh software first, and the script is run as the final step.
I’m having trouble outputting the text I see from Write-host correctly. It looks fine on the console, but I understand you can’t pipe results from write-host. I just want to export the results to a file, which I can then attach and send as an email to myself.
What you recommend I do instead of write-host? Ideally, I’d like to email the results to myself. But if I can at least get the results exported to a file that would be fine too.
Well… no :). So, this is covered in more depth in “Learn Windows PowerShell in a Month of Lunches,” but here’s the short version.
Write-Host puts nothing in the pipeline. Write-Output does.
At the end of the pipeline, whatever’s still there goes to Out-Default, which in the console and ISE immediately redirects to Out-Host, so that you see something on the screen. But that’s only if you don’t intercept it.
I’ll also point out that you’re missing out on a lot of really cool PowerShell stuff. For example, there’s no reason whatsoever to use all those + signs to concatenate strings.
$var = "hello"
$x = Get-Service | Select -First 1
Write "I say $var, and the name is $( $x.name )"
Will get really slow if you start querying remote computers. It’s often more efficient to open a CimSession, have each Get- command re-use that session, and then close the session. Not a factor on the local computer like you’re doing, but wanted you to be aware. Again, that’s also all in “Learn Windows PowerShell in a Month of Lunches,” now in its 3rd edition ;). You’ll save yourself a ton of time and heartache with that book, and YouTube.com/powershelldon has a ton of companion videos for it.
You should really be leveraging a PSObject versus trying to do things with text. If you format it as a PSObject, you can export the data to CSV, TXT, HTML, XML or any other format very easily.
Thanks for your help! I’m going to go through and re-write my script with your suggestions. I’ll be picking up a copy of your new book as well. I’m able to get the info to export(although not very pretty - those +'s are really screwing with the formatting), so I think I can get it to work from here.