1

I am using the PowerShell script mentioned here: http://www.sharepointdiary.com/2016/11/sharepoint-online-import-terms-to-termset-using-powershell.html to update the term store values and its working perfectly.

However, I need to schedule this Powershell to run through task scheduler so that it can run every hour. For this reason, I want to hard-code credentials that connects to SPOnline. But for some reason I am seeing below error.

enter image description here

I tried to make below change in Powershell to hard-code credentials:

   #Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"

#Variables for Processing

$AdminURL = "https://abc-admin.sharepoint.com/"
$TermGroupName= "Site Collection - abs.sharepoint.com-sites-123site"
$TermSetName="Due Diligence"
$CSVFile ="C:\Users\dm123\Downloads\TermStore.csv"
$TermHeaderInCSV ="Due Diligence"

Try {
    #Get Credentials to connect
    $Username = "[email protected]"
    $Password ="LE$yrts1m$."

    $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force


    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL) 
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $securePassword) 
    $Ctx.Credentials = $Credentials

    #Get the term store
    $TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx)
    $TaxonomySession.UpdateCache()
    $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
    $Ctx.Load($TaxonomySession)
    $Ctx.Load($TermStore)
    $Ctx.ExecuteQuery()

    #Get Termstore data from CSV and iterate through each row
    Import-Csv $CSVFile | ForEach-Object {

        #Get the Term Group
        $TermGroup=$TermStore.Groups.GetByName($TermGroupName)

        #Get the term set
        $TermSet = $TermGroup.TermSets.GetByName($TermSetName)

        #CSV File Header Row in Term to Add
        $TermName = $_.$($TermHeaderInCSV)

        #Check if the given term exists already
        $Terms = $TermSet.Terms
        $Ctx.Load($Terms)
        $Ctx.ExecuteQuery()
        $Term = $Terms | Where-Object {$_.Name -eq $TermName}

        If(-not $Term)
        {
            #Create Term Set
            Write-host "Creating Term '$TermName'" -ForegroundColor Cyan
            $Term = $TermSet.CreateTerm($TermName,1033,[System.Guid]::NewGuid().toString())
            $Ctx.Load($Term)
            $Ctx.ExecuteQuery()
            $Term.TermStore.CommitAll()
            $TaxonomySession.UpdateCache()
            Write-host "New Term '$TermName' Added Successfully!" -ForegroundColor Green
        }
        else
        {
            Write-host "Term '$TermName' Exists Already!" -ForegroundColor Yellow
        }
    }
 }
Catch {
    write-host -f Red "Error Importing Term store Data!" $_.Exception.Message
}

enter image description here Can someone please help me resolve the error, I simply need to hard-code the credentials. Thanks in advance.

3 Answers 3

1

You can use the ConvertTo-SecureString method to do that and pass that value.

Modify the try block in your code as below :

Try {
    #Get Credentials to connect

    $Username = "[email protected]"
    $Password ="LE$b1yuio$!."

    $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force

    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL) 
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $securePassword) 
    $Ctx.Credentials = $Credentials

    # rest of your existing code.
2
  • Thanks for the help Gautam. For some reason I am now seeing a different error about ExecuteQuery and Credentials. The email and password are not wrong I can assure you of that, maybe something you can help here. Please see the updated script in question description and screenshot of the error.
    – mdevm
    Commented Jan 15, 2019 at 14:26
  • Are you by any chance an external user ? I usually saw this error whenever i tried as external user. Also, if you have enabled 2 factor auth in your tenant, then would suggest the you use app password and then try Commented Jan 16, 2019 at 6:32
0

I haven't checked your full script but try putting a backtick (`) before the $ in your password variable.

PS C:\Temp> $Password ="LE$b1yuio$!."
PS C:\Temp> $Password
LE$!.
PS C:\Temp> $Password ="LE`$b1yuio$!."
PS C:\Temp> $Password
LE$b1yuio$!.
PS C:\Temp>

Or use a password without a $ sign.

0

Sample demo for your reference.

#Credentials to connect to office 365 site collection url 
$siteUrl ="https://tenant.sharepoint.com/sites/Developer"
$loginname="[email protected]"
$password = "pw"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($loginname, $secstr)
$web = $ctx.Web 
$ctx.Load($web)
$ctx.ExecuteQuery()

Update:

Below script works in my local.

Write-Host "Load CSOM libraries" -foregroundcolor black -backgroundcolor yellow
Set-Location $PSScriptRoot
Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")

#Credentials to connect to office 365 site collection url 
$siteUrl ="https://tenant.sharepoint.com/sites/Developer"
$loginname="[email protected]"
$password = "pw"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($loginname, $secstr)
$web = $ctx.Web 
$ctx.Load($web)
$ctx.ExecuteQuery()
write-host $web.Title

enter image description here

1
  • Hi Lee, I tried modifying my script as yours but the error still persists. I have added the error screenshot right below the script in the question description.
    – mdevm
    Commented Jan 15, 2019 at 14:27

Your Answer

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

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