Here's a PowerShell Script that demonstates how to create COM objects in PowerShell, in this case an Internet Explorer instance. The script then takes the output of the Get-Service cmdlet and writes the results to the IE window.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
# IEServiceList.ps1
# Jeffery Hicks
# http://jdhitsolutions.blogspot.com
# http://www.jdhitsolutions.com
# May 2006
#Display all running services in an Internet Explorer window
new-variable html
#create an object with the running services
$svc = get-service | where {$_.status -eq "running"}
#create a new COM object that is Internet Explorer
$oIE=New-object -COM InternetExplorer.Application
# If you want to see what Internet Explorer methods and
# properties exist, then run from within this script:
#$oIE |get-member
#Configure IE object
$oIE.navigate2("about:blank")
$oIE.width=400
$oIE.height=600
$oIE.Resizable=$True
$oIE.StatusBar=$True
$oIE.AddressBar=$False
$oIE.MenuBar=$False
$oIE.Toolbar=$False
#build the html code to display
foreach ($s in $svc) {$html=$html+"<font face=Verdana size=2>"+`
$s.Displayname+": "+$s.status+"</font><br>"}
#set the body with our html code
$oIE.document.body.innerHTML=$html
#display a summary in the status bar
$oIE.StatusText=($svc.Count).ToString()+" running services"
#display the IE object
$oIE.Visible=$True
Technorati Tags:
PowerShell
Scripting
Great post ! One of the first script I ran on PowerShell… I am getting the following error: Property ‘innerHTM:’ cannot be found on this object; make sure it exists and is settable. Am I missing something?
I just retested and it still works for me. I wonder if something didn’t get copied right, assuming you cut and pasted from the blog. Try commenting out these lines:
$oIE.document.body.innerHTML=$html
and
$oIE.StatusText=($svc.Count).ToString()+” running services”
You should just get a blank IE window to display. If not then the new object isn’t getting properly created. You might uncomment
#oIE | Get-member
which should list properties and methods for object. If you get nothing or an error then double-check the line
$oIE=New-object -COM InternetExplorer.Application
which should be all one one line.
Finally, I’m assuming Internet Explorer normally works on your computer and hasn’t been disabled in any way.
Hi Jeffery,
Thanks for taking the time to answer. I tried the script again on another machine and got the same result. Everything is working (ie the blank IE windows shows up, and I got the correct summary in the status bar…) but the windows stays blank. The only line of code not working for me is:
$oIE.document.body.innerHTML=$html
If I comment it out, I do not get the error message. Thanks!
What OS are you running on? What version of Internet Explorer? I have not tried this with IE7. Unfortunately I’m going to be out of town all next week starting tomorrow with basically no Internet. I’ll have to see what else I can come up. If you want, email me the script you are using as a text file. Maybe there’s something in the way you have the file formatted.
Hi Jeffery,
Looks like a useful piece of scripting , unfortunately I’m also experiencing the exact same as the other comment.
It appears that the COM object is being created, However Powershell can’t ref the property innerHTML.
What version of Powershell are u using? i.e MSH beta or PSH?
One also note, with the aid of ShinyPower (fantastic tool), the option -com doesn’t exists, however -comObject does, which seem to further the idea that you developed this potentially useful script in an earlier version.
Lookforward to your reply.
P.S How’s the Powershell book coming along?
Kind regards
Karl
Hi Jeffery,
Looks like a useful piece of scripting , unfortunately I’m also experiencing the exact same as the other comment.
It appears that the COM object is being created, However Powershell can’t ref the property innerHTML.
What version of Powershell are u using? i.e MSH beta or PSH?
One also note, with the aid of ShinyPower (fantastic tool), the option -com doesn’t exists, however -comObject does, which seem to further the idea that you developed this potentially useful script in an earlier version.
Lookforward to your reply.
P.S How’s the Powershell book coming along?
Kind regards
Karl
I’m using the latest version but it was an upgrade (I believe) over the last beta. As I mentioned in earlier comments, if you’re getting the IE object created then -com vs -comobject shouldn’t be the issue. I’ll have to look when I’m back at home.
Oh, and the book is really coming along. We’re in tech and copy edit mode. I think the plan is for a publish date early fall.
I had someone else test and he had no problem. I’d still like someone to send me copy of the script they are trying to run ([email protected]) so I can see if something is getting snafu’d in copying from the blog to a script. Also tell me what version of IE and what OS you are running.
The innerHTML method does not work with IE7 so if you are trying this on Vista it probably won’t work.
I ran into the same problem, but found an answer on a different website. If you replace the following line:
$oIE.document.body.innerHTML=$html
with this one:
$oIE.document.write(“$html”)
I was able to get it working. I found the tip from the following website:
http://www.vistax64.com/powershell/13956-innerhtml-method-not-working.html
Hi Jeff, no need to construct the HTML. Just pipe the object into ConvertTo-HTML like this:
$svc = get-service | where {$_.status -eq “running”} | ConvertTo-Html
Much better!
The point of the post was to demonstrate using COM objects in PowerShell. I was using IE as a viewer. When the script ended, nothing would be left behind. You can achieve the same results with an expression like this:
PS C:\> get-service | where {$_.status -eq “running”} | select name | convertto-html | out-file c:\reports\running.html
How could you then print out the Internet Explorer window from the script?
I’m not aware that the IE COM object has a print method.
I encourage you to post your script and problem in the PowerShell forum at ScriptingAnswers.com.