Using PowerShell to run Javascript in Internet Explorer on Windows Server
I have this piece of a PowerShell script:
$IE = New-Object -com InternetExplorer.Application
$IE.Navigate($URL)
While ($IE.ReadyState -Ne 4) {Start-Sleep -Milliseconds 100}
$IE.Document.ParentWindow.ExecScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$Obj = $IE.Document.ParentWindow.GetType().InvokeMember("JSIEVariable", 4096,
$Null, $IE.Document.parentWindow, $Null)
$HTML = $Obj.ToString()
$IE.Quit()
On Windows 10 it works fine but on Windows Server 2016 for lines 4, 5 and 6 I get the error:
You cannot call a method on a null-valued expression.
I'm pretty sure it has something to do with extra security in Windows Server preventing IE from running Javascript. There must be some way to dial back that security to be more on par with Windows 10 so that this script can run properly, but I can't figure out how. I've turned off IE Enhanced Security Configuration and ensured that Active Scripting is enabled. Aside from that I don't know what else to do.
javascript powershell internet-explorer windows-server windows-server-2016
add a comment |
I have this piece of a PowerShell script:
$IE = New-Object -com InternetExplorer.Application
$IE.Navigate($URL)
While ($IE.ReadyState -Ne 4) {Start-Sleep -Milliseconds 100}
$IE.Document.ParentWindow.ExecScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$Obj = $IE.Document.ParentWindow.GetType().InvokeMember("JSIEVariable", 4096,
$Null, $IE.Document.parentWindow, $Null)
$HTML = $Obj.ToString()
$IE.Quit()
On Windows 10 it works fine but on Windows Server 2016 for lines 4, 5 and 6 I get the error:
You cannot call a method on a null-valued expression.
I'm pretty sure it has something to do with extra security in Windows Server preventing IE from running Javascript. There must be some way to dial back that security to be more on par with Windows 10 so that this script can run properly, but I can't figure out how. I've turned off IE Enhanced Security Configuration and ensured that Active Scripting is enabled. Aside from that I don't know what else to do.
javascript powershell internet-explorer windows-server windows-server-2016
add a comment |
I have this piece of a PowerShell script:
$IE = New-Object -com InternetExplorer.Application
$IE.Navigate($URL)
While ($IE.ReadyState -Ne 4) {Start-Sleep -Milliseconds 100}
$IE.Document.ParentWindow.ExecScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$Obj = $IE.Document.ParentWindow.GetType().InvokeMember("JSIEVariable", 4096,
$Null, $IE.Document.parentWindow, $Null)
$HTML = $Obj.ToString()
$IE.Quit()
On Windows 10 it works fine but on Windows Server 2016 for lines 4, 5 and 6 I get the error:
You cannot call a method on a null-valued expression.
I'm pretty sure it has something to do with extra security in Windows Server preventing IE from running Javascript. There must be some way to dial back that security to be more on par with Windows 10 so that this script can run properly, but I can't figure out how. I've turned off IE Enhanced Security Configuration and ensured that Active Scripting is enabled. Aside from that I don't know what else to do.
javascript powershell internet-explorer windows-server windows-server-2016
I have this piece of a PowerShell script:
$IE = New-Object -com InternetExplorer.Application
$IE.Navigate($URL)
While ($IE.ReadyState -Ne 4) {Start-Sleep -Milliseconds 100}
$IE.Document.ParentWindow.ExecScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$Obj = $IE.Document.ParentWindow.GetType().InvokeMember("JSIEVariable", 4096,
$Null, $IE.Document.parentWindow, $Null)
$HTML = $Obj.ToString()
$IE.Quit()
On Windows 10 it works fine but on Windows Server 2016 for lines 4, 5 and 6 I get the error:
You cannot call a method on a null-valued expression.
I'm pretty sure it has something to do with extra security in Windows Server preventing IE from running Javascript. There must be some way to dial back that security to be more on par with Windows 10 so that this script can run properly, but I can't figure out how. I've turned off IE Enhanced Security Configuration and ensured that Active Scripting is enabled. Aside from that I don't know what else to do.
javascript powershell internet-explorer windows-server windows-server-2016
javascript powershell internet-explorer windows-server windows-server-2016
asked Nov 28 '18 at 3:49
Joel KolbJoel Kolb
33
33
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This may have something to do with Internet Explorer 'protected mode'.
If IE indeed is in protected mode, the $IE
object gets lost after the .Navigate()
command and any action after that will result in the error You cannot call a method on a null-valued expression.
To handle this, here's a function that tries to reconnect the $IE object.
function Connect-InternetExplorer {
# creates a new 'InternetExplorer.Application' object and navigates to the given url.
# If IE is in 'protected mode', the function tries to reconnect using the window handle
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, Position = 0)]
$Url,
[switch]$Visible
)
# test if Internet Explorer is in 'Protected Mode'
# see https://www.lifewire.com/how-to-disable-protected-mode-in-internet-explorer-2624507
$ieProtectedMode = ((Get-ItemProperty -Path 'HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet SettingsZones3' -Name '2500').2500 -ne 3)
$ie = New-Object -ComObject 'InternetExplorer.Application' -ErrorAction SilentlyContinue
$ie.Visible = [bool]$Visible
$ie.Silent = $true
$hwnd = $ie.Hwnd
$ie.Navigate($Url)
if ($ieProtectedMode) {
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
$objShell = New-Object -ComObject 'Shell.Application'
Start-Sleep -Milliseconds 100
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
# sometimes the Shell.Application does not find the window quickly enough,
Start-Sleep -Milliseconds 500
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
Write-Warning "Could not connect to the InternetExplorer ComObject."
}
}
finally {
$ErrorActionPreference = $oldErrorActionPreference
# clean up the Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objShell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
}
if (!$ie) { return $null }
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 50 }
return $ie
}
# this replaces the first three lines of your original code
$IE = Connect-InternetExplorer -Url $URL
if ($IE) {
$IE.Document.ParentWindow.ExecScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$Obj = $IE.Document.ParentWindow.GetType().InvokeMember("JSIEVariable", 4096, $Null, $IE.Document.parentWindow, $Null)
$HTML = $Obj.ToString()
$IE.Quit()
# clean up the $IE Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($IE) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
else {
Write-Warning "Could not connect Internet Explorer"
}
Hope that helps
That didn't work as hoped. Using your function I'm getting the same errors and then some.
– Joel Kolb
Nov 28 '18 at 16:47
The error text is too long to post here. I posted the full text on Pastebin. [link]pastebin.com/LKS7yvBm
– Joel Kolb
Nov 28 '18 at 16:57
@Joel Kolb, I try to make a test with your sample script on Windows Server 2016. I find that i am getting similar error like yours. I try to change the script related settings to enable or prompt helps me to solve the issue. You can try to make a test with each option and try to enable it one by one to check which specific option solves the issue.
– Deepak-MSFT
Nov 29 '18 at 8:15
@JoelKolb Can you turn off Protected mode manually using these instructions ? I cannot test myself as I don't have server 2016
– Theo
Nov 29 '18 at 9:54
Protected mode is already disabled. Tried through the GUI and through Group Policy.
– Joel Kolb
Nov 29 '18 at 16:23
add a comment |
A coworker helped me figure this out. It doesn't have anything to do with IE security, at least not as far as anything that hasn't already been covered. The problem is 'Microsoft.mshtml.dll' is missing from the GAC. It won't be present on a clean install of Windows Server but installing something like Office or Visual Studio will add it. However, i would bet that most people running a Windows Server wouldn't want to do that just for the sake of getting this working. What I did was copy the following folder/file structure from my Windows 10 PC to my server, closed out all instances of PowerShell and ISE and when I opened PowerShell again and ran the script everything worked.
C:WindowsassemblyGACMicrosoft.mshtml
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3aMicrosoft.mshtml.dll
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a__AssemblyInfo__.ini
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511837%2fusing-powershell-to-run-javascript-in-internet-explorer-on-windows-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This may have something to do with Internet Explorer 'protected mode'.
If IE indeed is in protected mode, the $IE
object gets lost after the .Navigate()
command and any action after that will result in the error You cannot call a method on a null-valued expression.
To handle this, here's a function that tries to reconnect the $IE object.
function Connect-InternetExplorer {
# creates a new 'InternetExplorer.Application' object and navigates to the given url.
# If IE is in 'protected mode', the function tries to reconnect using the window handle
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, Position = 0)]
$Url,
[switch]$Visible
)
# test if Internet Explorer is in 'Protected Mode'
# see https://www.lifewire.com/how-to-disable-protected-mode-in-internet-explorer-2624507
$ieProtectedMode = ((Get-ItemProperty -Path 'HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet SettingsZones3' -Name '2500').2500 -ne 3)
$ie = New-Object -ComObject 'InternetExplorer.Application' -ErrorAction SilentlyContinue
$ie.Visible = [bool]$Visible
$ie.Silent = $true
$hwnd = $ie.Hwnd
$ie.Navigate($Url)
if ($ieProtectedMode) {
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
$objShell = New-Object -ComObject 'Shell.Application'
Start-Sleep -Milliseconds 100
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
# sometimes the Shell.Application does not find the window quickly enough,
Start-Sleep -Milliseconds 500
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
Write-Warning "Could not connect to the InternetExplorer ComObject."
}
}
finally {
$ErrorActionPreference = $oldErrorActionPreference
# clean up the Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objShell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
}
if (!$ie) { return $null }
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 50 }
return $ie
}
# this replaces the first three lines of your original code
$IE = Connect-InternetExplorer -Url $URL
if ($IE) {
$IE.Document.ParentWindow.ExecScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$Obj = $IE.Document.ParentWindow.GetType().InvokeMember("JSIEVariable", 4096, $Null, $IE.Document.parentWindow, $Null)
$HTML = $Obj.ToString()
$IE.Quit()
# clean up the $IE Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($IE) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
else {
Write-Warning "Could not connect Internet Explorer"
}
Hope that helps
That didn't work as hoped. Using your function I'm getting the same errors and then some.
– Joel Kolb
Nov 28 '18 at 16:47
The error text is too long to post here. I posted the full text on Pastebin. [link]pastebin.com/LKS7yvBm
– Joel Kolb
Nov 28 '18 at 16:57
@Joel Kolb, I try to make a test with your sample script on Windows Server 2016. I find that i am getting similar error like yours. I try to change the script related settings to enable or prompt helps me to solve the issue. You can try to make a test with each option and try to enable it one by one to check which specific option solves the issue.
– Deepak-MSFT
Nov 29 '18 at 8:15
@JoelKolb Can you turn off Protected mode manually using these instructions ? I cannot test myself as I don't have server 2016
– Theo
Nov 29 '18 at 9:54
Protected mode is already disabled. Tried through the GUI and through Group Policy.
– Joel Kolb
Nov 29 '18 at 16:23
add a comment |
This may have something to do with Internet Explorer 'protected mode'.
If IE indeed is in protected mode, the $IE
object gets lost after the .Navigate()
command and any action after that will result in the error You cannot call a method on a null-valued expression.
To handle this, here's a function that tries to reconnect the $IE object.
function Connect-InternetExplorer {
# creates a new 'InternetExplorer.Application' object and navigates to the given url.
# If IE is in 'protected mode', the function tries to reconnect using the window handle
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, Position = 0)]
$Url,
[switch]$Visible
)
# test if Internet Explorer is in 'Protected Mode'
# see https://www.lifewire.com/how-to-disable-protected-mode-in-internet-explorer-2624507
$ieProtectedMode = ((Get-ItemProperty -Path 'HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet SettingsZones3' -Name '2500').2500 -ne 3)
$ie = New-Object -ComObject 'InternetExplorer.Application' -ErrorAction SilentlyContinue
$ie.Visible = [bool]$Visible
$ie.Silent = $true
$hwnd = $ie.Hwnd
$ie.Navigate($Url)
if ($ieProtectedMode) {
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
$objShell = New-Object -ComObject 'Shell.Application'
Start-Sleep -Milliseconds 100
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
# sometimes the Shell.Application does not find the window quickly enough,
Start-Sleep -Milliseconds 500
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
Write-Warning "Could not connect to the InternetExplorer ComObject."
}
}
finally {
$ErrorActionPreference = $oldErrorActionPreference
# clean up the Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objShell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
}
if (!$ie) { return $null }
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 50 }
return $ie
}
# this replaces the first three lines of your original code
$IE = Connect-InternetExplorer -Url $URL
if ($IE) {
$IE.Document.ParentWindow.ExecScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$Obj = $IE.Document.ParentWindow.GetType().InvokeMember("JSIEVariable", 4096, $Null, $IE.Document.parentWindow, $Null)
$HTML = $Obj.ToString()
$IE.Quit()
# clean up the $IE Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($IE) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
else {
Write-Warning "Could not connect Internet Explorer"
}
Hope that helps
That didn't work as hoped. Using your function I'm getting the same errors and then some.
– Joel Kolb
Nov 28 '18 at 16:47
The error text is too long to post here. I posted the full text on Pastebin. [link]pastebin.com/LKS7yvBm
– Joel Kolb
Nov 28 '18 at 16:57
@Joel Kolb, I try to make a test with your sample script on Windows Server 2016. I find that i am getting similar error like yours. I try to change the script related settings to enable or prompt helps me to solve the issue. You can try to make a test with each option and try to enable it one by one to check which specific option solves the issue.
– Deepak-MSFT
Nov 29 '18 at 8:15
@JoelKolb Can you turn off Protected mode manually using these instructions ? I cannot test myself as I don't have server 2016
– Theo
Nov 29 '18 at 9:54
Protected mode is already disabled. Tried through the GUI and through Group Policy.
– Joel Kolb
Nov 29 '18 at 16:23
add a comment |
This may have something to do with Internet Explorer 'protected mode'.
If IE indeed is in protected mode, the $IE
object gets lost after the .Navigate()
command and any action after that will result in the error You cannot call a method on a null-valued expression.
To handle this, here's a function that tries to reconnect the $IE object.
function Connect-InternetExplorer {
# creates a new 'InternetExplorer.Application' object and navigates to the given url.
# If IE is in 'protected mode', the function tries to reconnect using the window handle
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, Position = 0)]
$Url,
[switch]$Visible
)
# test if Internet Explorer is in 'Protected Mode'
# see https://www.lifewire.com/how-to-disable-protected-mode-in-internet-explorer-2624507
$ieProtectedMode = ((Get-ItemProperty -Path 'HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet SettingsZones3' -Name '2500').2500 -ne 3)
$ie = New-Object -ComObject 'InternetExplorer.Application' -ErrorAction SilentlyContinue
$ie.Visible = [bool]$Visible
$ie.Silent = $true
$hwnd = $ie.Hwnd
$ie.Navigate($Url)
if ($ieProtectedMode) {
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
$objShell = New-Object -ComObject 'Shell.Application'
Start-Sleep -Milliseconds 100
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
# sometimes the Shell.Application does not find the window quickly enough,
Start-Sleep -Milliseconds 500
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
Write-Warning "Could not connect to the InternetExplorer ComObject."
}
}
finally {
$ErrorActionPreference = $oldErrorActionPreference
# clean up the Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objShell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
}
if (!$ie) { return $null }
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 50 }
return $ie
}
# this replaces the first three lines of your original code
$IE = Connect-InternetExplorer -Url $URL
if ($IE) {
$IE.Document.ParentWindow.ExecScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$Obj = $IE.Document.ParentWindow.GetType().InvokeMember("JSIEVariable", 4096, $Null, $IE.Document.parentWindow, $Null)
$HTML = $Obj.ToString()
$IE.Quit()
# clean up the $IE Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($IE) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
else {
Write-Warning "Could not connect Internet Explorer"
}
Hope that helps
This may have something to do with Internet Explorer 'protected mode'.
If IE indeed is in protected mode, the $IE
object gets lost after the .Navigate()
command and any action after that will result in the error You cannot call a method on a null-valued expression.
To handle this, here's a function that tries to reconnect the $IE object.
function Connect-InternetExplorer {
# creates a new 'InternetExplorer.Application' object and navigates to the given url.
# If IE is in 'protected mode', the function tries to reconnect using the window handle
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, Position = 0)]
$Url,
[switch]$Visible
)
# test if Internet Explorer is in 'Protected Mode'
# see https://www.lifewire.com/how-to-disable-protected-mode-in-internet-explorer-2624507
$ieProtectedMode = ((Get-ItemProperty -Path 'HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet SettingsZones3' -Name '2500').2500 -ne 3)
$ie = New-Object -ComObject 'InternetExplorer.Application' -ErrorAction SilentlyContinue
$ie.Visible = [bool]$Visible
$ie.Silent = $true
$hwnd = $ie.Hwnd
$ie.Navigate($Url)
if ($ieProtectedMode) {
$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
$objShell = New-Object -ComObject 'Shell.Application'
Start-Sleep -Milliseconds 100
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
# sometimes the Shell.Application does not find the window quickly enough,
Start-Sleep -Milliseconds 500
try {
$ie = $objShell.Windows() | Where-Object {$_.HWND -eq $Hwnd}
$ie.Visible = [bool]$Visible
}
catch {
Write-Warning "Could not connect to the InternetExplorer ComObject."
}
}
finally {
$ErrorActionPreference = $oldErrorActionPreference
# clean up the Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objShell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
}
if (!$ie) { return $null }
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 50 }
return $ie
}
# this replaces the first three lines of your original code
$IE = Connect-InternetExplorer -Url $URL
if ($IE) {
$IE.Document.ParentWindow.ExecScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$Obj = $IE.Document.ParentWindow.GetType().InvokeMember("JSIEVariable", 4096, $Null, $IE.Document.parentWindow, $Null)
$HTML = $Obj.ToString()
$IE.Quit()
# clean up the $IE Com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($IE) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
else {
Write-Warning "Could not connect Internet Explorer"
}
Hope that helps
answered Nov 28 '18 at 10:58
TheoTheo
5,6313521
5,6313521
That didn't work as hoped. Using your function I'm getting the same errors and then some.
– Joel Kolb
Nov 28 '18 at 16:47
The error text is too long to post here. I posted the full text on Pastebin. [link]pastebin.com/LKS7yvBm
– Joel Kolb
Nov 28 '18 at 16:57
@Joel Kolb, I try to make a test with your sample script on Windows Server 2016. I find that i am getting similar error like yours. I try to change the script related settings to enable or prompt helps me to solve the issue. You can try to make a test with each option and try to enable it one by one to check which specific option solves the issue.
– Deepak-MSFT
Nov 29 '18 at 8:15
@JoelKolb Can you turn off Protected mode manually using these instructions ? I cannot test myself as I don't have server 2016
– Theo
Nov 29 '18 at 9:54
Protected mode is already disabled. Tried through the GUI and through Group Policy.
– Joel Kolb
Nov 29 '18 at 16:23
add a comment |
That didn't work as hoped. Using your function I'm getting the same errors and then some.
– Joel Kolb
Nov 28 '18 at 16:47
The error text is too long to post here. I posted the full text on Pastebin. [link]pastebin.com/LKS7yvBm
– Joel Kolb
Nov 28 '18 at 16:57
@Joel Kolb, I try to make a test with your sample script on Windows Server 2016. I find that i am getting similar error like yours. I try to change the script related settings to enable or prompt helps me to solve the issue. You can try to make a test with each option and try to enable it one by one to check which specific option solves the issue.
– Deepak-MSFT
Nov 29 '18 at 8:15
@JoelKolb Can you turn off Protected mode manually using these instructions ? I cannot test myself as I don't have server 2016
– Theo
Nov 29 '18 at 9:54
Protected mode is already disabled. Tried through the GUI and through Group Policy.
– Joel Kolb
Nov 29 '18 at 16:23
That didn't work as hoped. Using your function I'm getting the same errors and then some.
– Joel Kolb
Nov 28 '18 at 16:47
That didn't work as hoped. Using your function I'm getting the same errors and then some.
– Joel Kolb
Nov 28 '18 at 16:47
The error text is too long to post here. I posted the full text on Pastebin. [link]pastebin.com/LKS7yvBm
– Joel Kolb
Nov 28 '18 at 16:57
The error text is too long to post here. I posted the full text on Pastebin. [link]pastebin.com/LKS7yvBm
– Joel Kolb
Nov 28 '18 at 16:57
@Joel Kolb, I try to make a test with your sample script on Windows Server 2016. I find that i am getting similar error like yours. I try to change the script related settings to enable or prompt helps me to solve the issue. You can try to make a test with each option and try to enable it one by one to check which specific option solves the issue.
– Deepak-MSFT
Nov 29 '18 at 8:15
@Joel Kolb, I try to make a test with your sample script on Windows Server 2016. I find that i am getting similar error like yours. I try to change the script related settings to enable or prompt helps me to solve the issue. You can try to make a test with each option and try to enable it one by one to check which specific option solves the issue.
– Deepak-MSFT
Nov 29 '18 at 8:15
@JoelKolb Can you turn off Protected mode manually using these instructions ? I cannot test myself as I don't have server 2016
– Theo
Nov 29 '18 at 9:54
@JoelKolb Can you turn off Protected mode manually using these instructions ? I cannot test myself as I don't have server 2016
– Theo
Nov 29 '18 at 9:54
Protected mode is already disabled. Tried through the GUI and through Group Policy.
– Joel Kolb
Nov 29 '18 at 16:23
Protected mode is already disabled. Tried through the GUI and through Group Policy.
– Joel Kolb
Nov 29 '18 at 16:23
add a comment |
A coworker helped me figure this out. It doesn't have anything to do with IE security, at least not as far as anything that hasn't already been covered. The problem is 'Microsoft.mshtml.dll' is missing from the GAC. It won't be present on a clean install of Windows Server but installing something like Office or Visual Studio will add it. However, i would bet that most people running a Windows Server wouldn't want to do that just for the sake of getting this working. What I did was copy the following folder/file structure from my Windows 10 PC to my server, closed out all instances of PowerShell and ISE and when I opened PowerShell again and ran the script everything worked.
C:WindowsassemblyGACMicrosoft.mshtml
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3aMicrosoft.mshtml.dll
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a__AssemblyInfo__.ini
add a comment |
A coworker helped me figure this out. It doesn't have anything to do with IE security, at least not as far as anything that hasn't already been covered. The problem is 'Microsoft.mshtml.dll' is missing from the GAC. It won't be present on a clean install of Windows Server but installing something like Office or Visual Studio will add it. However, i would bet that most people running a Windows Server wouldn't want to do that just for the sake of getting this working. What I did was copy the following folder/file structure from my Windows 10 PC to my server, closed out all instances of PowerShell and ISE and when I opened PowerShell again and ran the script everything worked.
C:WindowsassemblyGACMicrosoft.mshtml
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3aMicrosoft.mshtml.dll
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a__AssemblyInfo__.ini
add a comment |
A coworker helped me figure this out. It doesn't have anything to do with IE security, at least not as far as anything that hasn't already been covered. The problem is 'Microsoft.mshtml.dll' is missing from the GAC. It won't be present on a clean install of Windows Server but installing something like Office or Visual Studio will add it. However, i would bet that most people running a Windows Server wouldn't want to do that just for the sake of getting this working. What I did was copy the following folder/file structure from my Windows 10 PC to my server, closed out all instances of PowerShell and ISE and when I opened PowerShell again and ran the script everything worked.
C:WindowsassemblyGACMicrosoft.mshtml
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3aMicrosoft.mshtml.dll
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a__AssemblyInfo__.ini
A coworker helped me figure this out. It doesn't have anything to do with IE security, at least not as far as anything that hasn't already been covered. The problem is 'Microsoft.mshtml.dll' is missing from the GAC. It won't be present on a clean install of Windows Server but installing something like Office or Visual Studio will add it. However, i would bet that most people running a Windows Server wouldn't want to do that just for the sake of getting this working. What I did was copy the following folder/file structure from my Windows 10 PC to my server, closed out all instances of PowerShell and ISE and when I opened PowerShell again and ran the script everything worked.
C:WindowsassemblyGACMicrosoft.mshtml
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3aMicrosoft.mshtml.dll
C:WindowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3a__AssemblyInfo__.ini
answered Nov 30 '18 at 20:29
Joel KolbJoel Kolb
33
33
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511837%2fusing-powershell-to-run-javascript-in-internet-explorer-on-windows-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown