Powershell "Cannot process argument"

Sara 401 Reputation points
2021-09-13T08:59:37.787+00:00

I am running into below error while trying to run this piece of code, can someone throw some light on what I am missing here? Thanks!

$SecurePassword = ConvertTo-SecureString "password" -AsPlainText -Force -ErrorAction Stop
$SSCred = New-Object System.Management.Automation.PSCredential ("PROD\username", $SecurePassword) -ErrorAction Stop
Write-Host "$(Get-date): Done Secret Server Login"

$AzureProdLock = Get-SecretServerCredential -SamAccountName pjob9001 -OperatorCredentials $SSCred -Verbose:$True -ErrorAction Stop
$Null = Connect-AzAccount -ServicePrincipal -Credential $AzureProdLock -Tenant "tenantId" -ErrorAction Stop -WarningAction SilentlyContinue

Error msg:
"Exception calling ".ctor" with "2" argument(s): "Cannot process argument because

the value of argument "userName" is not valid. Change the value of the "userName" argument and run the operation again.""

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 39,351 Reputation points
    2021-09-14T11:53:15.157+00:00

    Hello Saravanaraj,

    PowerShell protects access to variables, aliases, functions, and PowerShell drives (PSDrives) by limiting where they can be read and changed. PowerShell uses scope rules to ensure that you do not inadvertently change an item that should not be changed.

    In the second line for PROD\ avoid double quotes ("PROD\username") and replace them with single quotes ( 'PROD\username' )

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.1#parent-and-child-scopes

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.1#double-quoted-strings

    Hope this answers all your queries, if not please do repost back.
    If an Answer is helpful, please click "Accept Answer" and upvote it : )