0

I have a function requiring a secure password as a parameter:

function Create-List
{
 param (
[Parameter(Mandatory=$true)]
    [string]$Username,
   [Parameter(Mandatory=$True)]
 [Security.SecureString]$password,
    [Parameter(Mandatory=$true)]
    [string]$Url

)

 $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
 $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)

}

The intention is that instead of the password, Powershell will display *** when a user is entering

Create-List -password Passsword2144

Create-List -password ***********

but the password is still visible when the cmdlet is entered and after hitting enter, I get an error that Powershell cannot convert Passsword2144 of type String to System.Security.SecureString. What am I doing wrong? Did I forget some reference?

0

I do it this way, the globals are stored in my profile and then I can make all the functions operate in this way:

    #Global Variables
    $CAprod = "https://company-admin.sharepoint.com"
    $me = "myemail@company.com"
    $credential = Get-Credential -Message "Please enter your Office 365 Administrative credentials" -UserName $me
    Connect-SPOService -Url $CAprod -Credential $credential

    function Foo-Bar{
param (
    [Parameter(Mandatory=$true)]
    [string]$SiteUrl

)
      $context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
      $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
    }
  • This is a valid workaround, but not an answer why SecureString parameter is not read as a SecureString. Do you know why [SecureString] is not working or how to create a parameter of SecureString type? – grisha Sep 23 '15 at 8:14
  • 1
    because a plain text password is not a secure string, it'd be best to use the get-credential and pass around a credential object instead of having a user supply their password in that fashion. If you are going to pass a string like that, then your function has to do the conversion of the string to a secure string. – Eric Alexander Sep 23 '15 at 12:20
  • so basically SecureString will never work as a parameter type? – grisha Sep 29 '15 at 9:53
  • It could I suppose, it just isn't that optimal in my opinion. It's best to use the get-credential commandlet and pass around a credential object or use the get-credential inside of your function. Supplying a plain text password that you then converto-securestring doesn't give me any warm fuzzies about your library. – Eric Alexander Sep 29 '15 at 11:02

Your Answer

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

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