I want to identify the type of web part by its name, in this case "news". If the title of the web part was the same as "string2" I would like to delete this web part. I don't understand why I keep getting false. I also tried compareTo.
If I write the value of the variable -> $webParts.Title
I get the string "Wiadmości" I do a comparison with the string "Wiadmości" I get false
$global:importFileName="sitesURL.csv"
functionmain()
{
$sites=import-csv$global:importFileName
$global:credentials=Get-Credential
ForEach($sitein$sites)
{
RemoveWebParts-sourceUrl$site.spUrl-location$pwd
}
}
functionRemoveWebParts(){
Param( $sourceUrl
) Process{
Write-Host"###################### '$sourceUrl' ######################"-ForegroundColor Green
Connect-PnPOnline-Url$sourceUrl-Credentials$global:credentials
$page=Get-PnPClientSidePage-Identity"Home.aspx"
Write-Host"Search web parts:"-ForegroundColor Cyan
$webParts=$page.Controls
$loopCounter=0
foreach($webpartin$webparts) {
[String] $string1=$webParts.Title
[String] $string2="Wiadomości"
if( $string1-eq$string2) {
Remove-PnPClientSideComponent-Page$page-InstanceId$webpart.InstanceId
Write-Host"Deleted '$webpart.Title'"-ForegroundColor Green
$loopCounter++
}
}
if($loopCounter-gt0){
Write-Host"Removed web parts '$loopCounter'"-ForegroundColor Green
}
Write-Host"No items were found for removal "-ForegroundColor Yellow
}
}
Measure-Command {main}