带参数的Powershell com对象 [英] Powershell com object with parameters
问题描述
我想编写一个Powershell脚本,它将以com对象的形式打开Internet Explorer实例,以便我可以对其进行操作.问题是我需要使用参数来运行它,以确保IE作为新会话运行.
I want to write a powershell script that will open an Internet Explorer instance as a com object so i can manipulate it. The problem is I need to run it with a parameter to make sure IE is run as a new session.
我需要传递的参数是-noframemerging我需要传递这个参数,所以每个实例都是一个新的会话.这是我用来启动实例的代码.
The parameter I need to pass is -noframemerging I need to pass this so each instance is a new session. Here is the code I've been using to get the instance started.
$ie = new-object -com "InternetExplorer.Application"
如果有人可以帮助我弄清楚如何能够在IE中创建多个对象作为新会话,那就太好了!
If someone could give me a hand figuring out how to be able to create multiple objects as a new session in IE that would be fantastic!
推荐答案
这是我的解决方案:
$IEProcess = [System.Diagnostics.Process]::Start("iexplore", "-noframemerging")
Sleep -Seconds 1
$ie = $(New-Object -ComObject "Shell.Application").Windows() | ? {$_.HWND -eq $IEProcess.MainWindowHandle}
我使用System.Diagnostic.Process库启动了一个实例,我使用的是Start-Process,但得到了一些奇怪的结果,这已经将其修复了.此时,我打开了一个带有noframemerging开关和指向该进程的变量的IE实例.
I launch an instance with the System.Diagnostic.Process library, I was using Start-Process but getting some weird outcomes, this fixed that right up. At this point I have an instance of IE open with the noframemerging switch and a variable pointing the process.
我在那里睡了一会儿,让com对象有机会在尝试引用它之前进行构建.
I threw a sleep in there to give the com object a chance to construct before trying to reference it.
然后,我抓住所有Windows com对象,并选择一个具有与刚刚启动的进程相同的句柄(HWND)的句柄,并将其分配给$ ie变量.
Then I grab all the windows com objects and select the one that has a handle (HWND) that is the same as the one from the process I just started and assign it to my $ie variable.
最终结果是我有一个引用Internet Explorer的com对象的变量,该对象独立于所有其他会话.我之所以特别需要这样做,是因为我要打开IE的许多实例,并使用不同的用户帐户登录网站,如果它们共享一个会话,则将跳过登录页面,并且他们已经作为第一个登录的用户登录了.
The end result is I have a variable that references a com object of Internet Explorer that is independent of all other sessions. The reason I needed this in particular is I'm opening many instances of IE and logging into a website with different user accounts, if they share a session the login page is skipped and they will already be logged in as the first user that logged in.
感谢所有帮助人员!
这篇关于带参数的Powershell com对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!