0

I have made a script that I would distribute to my college's. It's a Powershell script using CSOM to connect to a SharePoint-Online environment. What I would like is to create an input box where my college's can enter their credentials. Because now I have put my userid en password in the code itself. How can I change the CSOM code to create an inputbox? I know that the command: “$credential = get-credential” generates a inputbox too. How can I change the code below that a user can logon through an inputbox.

$SiteUrl = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$UserName="xxxxxxxxxxxxxxxxxxxxxx"
$Password ="xxxxxxxxxxxxxxxxxxxxx"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
$Context.Credentials = $credentials

Gr, P

0

Use this code:

$SiteUrl = "https://company.sharepoint.com"
$Credentials = Get-Credential

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName,$Credentials.Password)

or this:

$SiteUrl = "https://company.sharepoint.com"
$UserName = Read-Host -Prompt "Enter username"
$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
$Context.Credentials = $Credentials

if you have enabled 2FA you need to use something like THIS.

6
  • Both doesn't work
    – user78519
    Jan 27 '19 at 10:13
  • edited, working for me now Jan 27 '19 at 10:16
  • I get error: "Exception calling "ExecuteQuery" with "0" argument(s): "The partner returned a bad sign-in name or password error. For more information, see Federation Error-handling Scenarios."
    – user78519
    Jan 27 '19 at 10:20
  • Have you set Modern authentization? This could be a problem, cause you need to pop-up the new login window? or 2FA authentication? Jan 27 '19 at 10:22
  • I have read your link to 2FA and found the solution. Thx. P
    – user78519
    Jan 27 '19 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy