I have a simple powershell script intended to manipulate Sharepoint 2010. I have stripped it down to the minimum to illustrate the problem.
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$siteURL = "http://intranet/"
$site = Get-SPSite($siteURL)
foreach ($web in $site.AllWebs) {
Write-Host "Inspecting " $web.Title
}
It fails when I introduce the “foreach” loop (so the Snap-in loads OK and Get-SPSite($siteURL) doesn’t seem to cause an error).
The error message is
C:\temp\sp_dm.PS1 : Exception has been thrown by the target of an invocation.
At line:1 char:12
+ .\sp_dm.PS1 <<<<
+ CategoryInfo : NotSpecified: (:) [sp_dm.PS1], TargetInvocationExceptio
+ FullyQualifiedErrorId : System.Reflection.TargetInvocationException,sp_dm.PS1
The problem isn’t line:1 of the script, the message refers to line:1 of the command that I type to run it (I changed the name of the script and the char:12 changed accordingly). BUt as I say, the error is generated by the foreach loop, and the code occurs in many examples on the Net, so there is something starnge about my local SharePoint. Any suggestions?
try this change: