I have come across a weird issue while running a PowerShell script through task scheduler. Below is the simple script which would open all the sites of a SharePoint farm in IE browser.
Add-PsSnapin Microsoft.SharePoint.PowerShell
$ShowIE = $True
$LogFile = "E:\Scripts\Warmedup_Sites.log"
$ie=New-Object -ComObject "InternetExplorer.Application"
$ie.visible = $ShowIE
Get-SPSite -Limit ALL | Get-SPWeb -Limit ALL | ForEach-Object{
$ie.navigate($_.url)
while ($ie.Busy -eq $true){
Start-Sleep -Milliseconds 1000;
}
add-content -path $LogFile -value $("[" + (get-Date).ToString() + "] " + $_.url)
}
$ie.quit()
1. In the 1st case the script runs successfully only when the powershell console is opened as "Run as adminstrator". Otherwise it throws below error. The account am running is already part of server administrators group, is a SharePoint Farm administrator and is a domain account
The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_
At E:\Scripts\Warmup_AllRootSites.ps1:20 char:1
+ $ie.visible = $ShowIE
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Method invocation failed because [System.__ComObject] doesn't contain a method named 'navigate'.
At E:\Scripts\Warmup_AllRootSites.ps1:23 char:4
+ $ie.navigate($SiteURL)
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
add-content : Could not find a part of the path 'C:\Installs\Warmedup_Sites.log'.
At E:\Scripts\Warmup_AllRootSites.ps1:27 char:3
+ add-content -path $LogFile -value $("[" + (get-Date).ToString() + "] " + $Site ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Installs\Warmedup_Sites.log:String) [Add-Conten
Exception
+ FullyQualifiedErrorId : GetContentWriterDirectoryNotFoundError,Microsoft.PowerShell.Command
Method invocation failed because [System.__ComObject] doesn't contain a method named 'navigate'.
2. In the 2nd case, when i run the same PowerShell script through a task scheduler, it fails again with below error. The task is set to "Run with highest privileges". And in actions tab am running like "PowerShell.exe -ExecutionPolicy Bypass E:\Scripts\Warmup_AllSites.ps1". I have gone through several blogs and forums related to this error but nothing helped me. I dont think this is a permission issue anywhere. It seems like the COM object is losing the reference and throwing the error when referred. Can someone please help me in this regard.
New-Object : Retrieving the COM class factory for component with CLSID
{0002DF01-0000-0000-C000-000000000046} failed due to the following error:
80080005 Server execution failed (Exception from HRESULT: 0x80080005
(CO_E_SERVER_EXEC_FAILURE)).
At E:\Scripts\Warmup_AllSites.ps1:18 char:5
+ $ie=New-Object -ComObject "InternetExplorer.Application"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMExcept
ion
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Comman
ds.NewObjectCommand
Property 'visible' cannot be found on this object; make sure it exists and is
settable.
At E:\Scripts\Warmup_AllSites.ps1:20 char:1
+ $ie.visible = $ShowIE
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
You cannot call a method on a null-valued expression.
At E:\Scripts\Warmup_AllSites.ps1:23 char:4
+ $ie.navigate($_.url)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At E:\Scripts\Warmup_AllSites.ps1:23 char:4
+ $ie.navigate($_.url)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At E:\Scripts\Warmup_AllSites.ps1:23 char:4
+ $ie.navigate($_.url)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At E:\Scripts\Warmup_AllSites.ps1:23 char:4
+ $ie.navigate($_.url)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At E:\Scripts\Warmup_AllSites.ps1:23 char:4
+ $ie.navigate($_.url)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At E:\Scripts\Warmup_AllSites.ps1:23 char:4
+ $ie.navigate($_.url)
Thanks, Ramesh Mukka