Showing posts with label Powershell sripts. Show all posts
Showing posts with label Powershell sripts. Show all posts

Monday, 19 February 2024

PowerShell - Cannot convert the value of type "System.String" to type "System.Security.SecureString"

Issue:

Recently, my colleague was working on a migration project. He faced a really strange issue with 

PowerShell. We have created that script for big site migration and we have done tons of site migration with that in the past. But this time it was throwing an error while we were trying to connect the site. Below is the sample code portion:

 #Prompt the user for the source environment username  
 $sourceUserName = "domain\username"  
 # Prompt the user for the source environment password  
 $password = 'My&$P@ssw0rd'  
 $securePassword = $password | ConvertTo-SecureString -AsPlainText -Force  
 Connect-Site "Site URL" -Username $sourceUserName -Password $securePassword  

Error - Connect-Site : Cannot bind parameter 'Password'. Cannot convert the value of type "System.String" to type "System.Security.SecureString". At line:1 char:131


Analysis:

The first thing I checked on my end was whether there was any change in the "connect-site" command. But there was no update there. So, we try to run the connect-site command without passing a password. Powershell showcased the dialog for the password and it worked like a charm. That means the password was also correct. But we were not able to pass it correctly. 

Generally, when we get any projects, we used to set our passwords but this time it was set by the client and it contained many special characters. In the above code, we have showcased 2 of them - & and $. These 2 characters are not handled when you try to treat them normally in convert to secure string. 


Resolution:

So, we copy the error and google it. But there were tons of sites. So, we go to the copilot in the edge browser and ask resolution of the same error. It provided a response with possible resolutions and the source sites list where it has gathered the information. The response was correct but still I went through the reference site. I found one stack overflow site which I mentioned in reference that explained things in much easier language than copilot. 

The PowerShell herestring exists for just such an occasion.

 $password = @'  
 My&$P@ssw0rd  
 '@  

The @" and "@ characters have to be on their own line, but allow for any characters inside of them.

Once we handled the password like that, it was able to run successfully. The full code will be like below:

 #Prompt the user for the source environment username  
 $sourceUserName = "domain\username"  
 # Prompt the user for the source environment password  
 $password = @'  
 My&$P@ssw0rd  
 '@  
 $securePassword = $password | ConvertTo-SecureString -AsPlainText -Force  
 Connect-Site "Site URL" -Username $sourceUserName -Password $securePassword  


References:

Saturday, 24 June 2023

Copy-PNPFile – “Failed to verify the existence of destination location at” Error.

Error:

Recently, I was working on a migration task. I needed to migrate a few pages from the dev site to production. As this was not full migration, we were not going to use any 3rd party tool like Sharegate. So, we decided to use PowerShell to achieve our goal. Luckily, we found the “Copy-PNPFile” command in PNP PowerShell.

But when we use this to migrate, we found the below error: 
Copy-PnPFile: {"odata.error":{"code":"-2147213249, Microsoft.SharePoint.Deployment.SPMigrationQosException", "message":{"lang": "en-US", "value": "Failed to verify the existence of destination location at


Solution:

The solution to this problem was simple. Make sure you didn’t add “/” at the end of the target. Once I removed this, the error resolved itself.

Changing the below code from

 $TargetFolderURL = "/sites/Department2/Shared%20Documents/"  

To this

 $TargetFolderURL = "/sites/Department2/Shared%20Documents"  

And running the script again solved the problem.






Resources

Saturday, 22 May 2021

How to start User Profile Service stuck on Starting or Stopping stage

Issue:

Recently, we have requirement to change the farm admin account due to some issue with user. We have changed the user but it broke the User Profile Service. The issue we face is that when we start it, it stuck on Starting. If we try to stop then it stuck on stopping. 

Solution:

So, finally I can across PowerShell script that is working for me that starts & stops user profile and user profile synchronisation service. 

  • To stop the service, run using powershell command for user profile service,

 stsadm -o provisionservice -action stop -servicetype “Microsoft.Office.Server.Administration.UserProfileService, Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” -servicename  

  •   To stop the service, run using powershell command for user profile synchronization service,

 stsadm -o provisionservice -action stop -servicetype “Microsoft.Office.Server.Administration.UserProfileService, Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” -servicename FIMSynchronizationService  

Now to start the service, you just need to change the action parameter to start in above powershell command.

  • To start the service, run using powershell command for user profile service,

 stsadm -o provisionservice -action start -servicetype “Microsoft.Office.Server.Administration.UserProfileService, Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” -servicename  

  • To start the service, run using powershell command for user profile synchronization service,

 stsadm -o provisionservice -action start -servicetype “Microsoft.Office.Server.Administration.UserProfileService, Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” -servicename FIMSynchronizationService  

Reference:

  • http://sharepointhyfy.blogspot.com/2016/04/user-profile-service-stuck-in.html#gsc.tab=0
  • https://social.technet.microsoft.com/wiki/contents/articles/26067.sharepoint-2013-user-profile-stuck-stopping.aspx

Sunday, 30 June 2019

SharePoint Online Allow custom scripts - Part 3

We have looked at why Microsoft disabled custom scripts in SharePoint Online and where it affects. We have looked in previous two blogs why and what is alternative. If you have not gone through that, please check below links.




In this post, we are going to review how to enable custom scripting on site collection level and what will be its impact.

So if we enable scripting at tenant level we may end up loosing control over security. Instead Microsoft Provided a way to enable scripting at site collection level.

Prerequisite:


  • SharePoint Online Management Shell 


Follow below steps to enable scripting at site collection level:

  • Open the SharePoint Online Management Shell console. 
  • Execute the command to connect the Office 365 :

    Connect-SPOService -Url https://tenant-admin.sharepoint.com
  • Execute the command bellow with the url for the site collection where you want to enable scripting:

    Set-SPOSite -Identity https://tenant.sharepoint.com/sites/contoso -DenyAddAndCustomizePages 0


Pros:


  • Tenant level scripting enable may take up to 24 hours. But this approach disable the scripting block immediately. 
  • As this settings are enabled at specific site collection, you can make sure the scripting do not affect other site collections or tenant.

Conclusion:

We have seen limitations and how to overcome that. If you need to enable scripting, choose the settings as per your need.

Wednesday, 4 February 2015

Get List of event receiver attached to List/Library using powershell sharepoint

To get list of event receiver attached to list /Library use followingpowershell script:

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$web=Get-SPWeb "WebURL"
$list=$web.Lists["ListName"]
$list.EventReceivers | Select Name, Assembly, Type

Thursday, 27 November 2014

Get Site Template GUID using Powershell

Sometimes we need to find the GUID of save site template.

Open Power shell management shell in administrator mode.
 here is the power shell script that provide you the functionality. pate it.

 $url = "http://localhost:mysite/"

$site= new-Object Microsoft.SharePoint.SPSite($url )

$loc= [System.Int32]::Parse(1033)

$templates= $site.GetWebTemplates($loc)

foreach ($child in $templates){ write-host $child.Name "  " $child.Title}

click Enter. And you will get the list of site template with the GUID.
Enjoy

SharePoint Server 2016 and 2019 Retirement: What IT Professionals Need to Know

Microsoft SharePoint Server 2016 and 2019 are reaching end of support on July 14, 2026. With the April 2026 feature retirement deadline now ...