none
Cannot Process argument because the value of argument "password" is null RRS feed

  • Question

  • Hello Guys,

    I'm pretty new to c# and powershell, and I'm at a stage where I want to explore the capability of integrating c# and powershell.

    So just for testing purposes, basically I have a Test.ps1 script and here is the content:

    #AD Credentials
    $username = "Admin1"
    $encrypted = Get-Content "encrypted_password1.txt" | ConvertTo-SecureString
    $creds = New-Object System.Management.Automation.PsCredential($username, $encrypted)
    
    New-ADUser -samAccountName test1 -DisplayName test1 -Credential $creds
    
    

    the content of my encypted_password1.txt is generated by :

    $credential = Get-Credential
    $credential.Password | ConvertFrom-SecureString | Set-Content c:\encrypted_password1.txt

    and in C# I have a FileSystemWatcher Service that when a new file is created it will execute the Test.ps1 Script :

    PowerShell ps = PowerShell.Create();
    ps.AddScript("Test1.ps1");
    ps.Invoke();


    The script did get executed but no User is generated in AD, so I did a debug and there's an error saying :

    Inner Exception 1:
    MethodInvocationException: Exception calling ".ctor" with "2" argument(s): "Cannot process argument because the value of argument "password" is null. Change the value of argument "password" to a non-null value."

    Inner Exception 2:
    PSArgumentNullException: Cannot process argument because the value of argument "password" is null. Change the value of argument "password" to a non-null value.

    I've searched the internet and looks like people are saying that C# is unable to decrypt the secure string password ? I thought that's powershell's job ? Is there anyway around this ? 

    Any help would be appreciated.

    Regards,

    Tuesday, September 10, 2019 2:48 PM

All replies

  • Hi knightshadow72,

    Thank you for posting here.

    Based on my test, your powershell code has some problems. We must have name parameter when we create AD user by using new-aduser command.

    If you want to know more about it, you could refer to the following link.

    New-ADUser

    Here is my test result in windows powershell.

    Therefore, it is best for you to execute powershell code in c# when you are sure that the powershell code is right.

    Best Regards,

    Jack

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 11, 2019 2:29 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 17, 2019 7:58 AM
    Moderator
  • Have you executed these command one by one in a PowerShell shell, to see exactly which command is failing?  I only see one constructor in there (the PsCredential object), and it doesn't have a "password" argument.

    Are you absolutely sure that your Test.ps1 script is being run in the same folder where you saved encrypted_password1.txt?  You might "echo $username, $encrypted" just so you can see what you're sending.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Tuesday, September 17, 2019 5:35 PM