1

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}​​​​​​​​
1
  • Something is wrong with the code you pasted. It is missing alll spaces in each row. Can you re-paste the code, please? Apr 14, 2021 at 2:48

1 Answer 1

0

It could be that the something is wrong with the accents or with casing. Or maybe there is some extra character in a string.

Always use single quotes when defining variables. Just in case there is a special character in your string:

[String] $string2='Wiadomości'

This is the best way to compare two strings:

[System.StringComparer]::InvariantCultureIgnoreCase.Compare($string1.trim(), $string2.trim())
1
  • There is a problem with the character encoding. The variable "Wiadomości" has a different number of bytes when printed than what I return from the web $webParts.Title. I am now retrieving the variables from a csv file and everything is fine.
    – Jelon3k
    Apr 15, 2021 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.