I have a powershell script that I am trying to add webparts to a sharepoint page automatically. The script below is wrapped in a FOREach-Object and it works fine on the first iteration however on the second itereation it is returning some unwanted information to the powershell screen (and logs I am creating).
I am looking for a way to prevent that from returning anything other than the write-host statements. I originally thought it might be an exception however It looks like the web parts are still being added with no issue and I can’t find any reference to an exeption in the return info.
Here is what I have thus far:
$addCsv | ForEach-Object{
$tempwebURL = $_.'SiteURL'
$pageUrl = $_.'Page'
$varWebPartZone = $_.'WebPartZone'
$varChrome = $_.'Chrome'
$varTitle = $_.'Title'
$varPosition = [int]$_.'Position'
#get current site
$tempweb= Get-SPWeb -Identity $tempwebURL
Write-Host ("Adding Web Part to :"+ $tempweb.Title)
#add webpart
[System.Xml.XmlTextReader]$oxmlReader = new-object System.Xml.XmlTextReader("Path removed");
$webpartmanager=$tempweb.GetLimitedWebPartManager( $pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
[System.Web.UI.WebControls.WebParts.WebPart] $oWebPart
$err = ''
$oWebPart = new-object $webpartmanager.ImportWebPart($oxmlReader,[ref]$_.Exception.Message)
$oWebPart.Title = $varTitle
$oWebPart.ChromeType=$varChrome
$webpartmanager.AddWebPart($oWebPart, $varWebPartZone, $varPosition)| Out-Null
$oWebPart.Dispose()
$oxmlReader.Close()
$webpartmanager.Dispose()
$tempweb.Dispose()
$num++
}
Below is the information being printed to the screen which I do not want:
AllowClose : True
AllowConnect : True
AllowEdit : True
AllowHide : True
AllowMinimize : True
AllowZoneChange : True
AuthorizationFilter :
CatalogIconImageUrl :
ChromeState : Normal
ChromeType : None
ConnectErrorMessage :
Description :
Direction : NotSet
DisplayTitle : Last Date Modified Explorer
ExportMode : None
HasUserData : False
HasSharedData : False
Height :
HelpMode : Navigate
HelpUrl :
Hidden : False
IsClosed : False
ImportErrorMessage : Cannot import this Web Part.
IsShared : True
IsStandalone : False
IsStatic : False
Subtitle :
Title : Last Date Modified Explorer
TitleIconImageUrl :
TitleUrl :
Verbs : {}
WebBrowsableObject : Removed
Width :
Zone :
ZoneIndex : 0
Controls :
BackImageUrl :
DefaultButton :
GroupingText :
HorizontalAlign : NotSet
ScrollBars : None
Wrap : True
AccessKey :
Attributes : System.Web.UI.AttributeCollection
BackColor : Color [Empty]
BorderColor : Color [Empty]
BorderWidth :
BorderStyle : NotSet
ControlStyle : System.Web.UI.WebControls.PanelStyle
ControlStyleCreated : True
CssClass :
Style : System.Web.UI.CssStyleCollection
Enabled : True
EnableTheming : True
Font :
ForeColor : Color [Empty]
HasAttributes : False
SkinID :
TabIndex : 0
ToolTip :
ClientID : g_adea57ed_5d6d_40ee_b468_ce85e37f5e9a
ID : g_adea57ed_5d6d_40ee_b468_ce85e37f5e9a
EnableViewState : True
NamingContainer :
BindingContainer :
Page :
TemplateControl :
Parent :
TemplateSourceDirectory :
AppRelativeTemplateSourceDirectory :
Site :
Visible : True
UniqueID : g_adea57ed_5d6d_40ee_b468_ce85e37f5e9a
I also welcome any “you could do this better by…” comments. Thanks again.
My guess would be that the following line is the culprit:
In the first iteration this won’t do much, it’s just a cast of
$null. But in later iterations it retains the value of the previous iteration and outputs it. You don’t need that line anyway as it does nothing.