K
knightshadow72
Guest
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,
Continue reading...
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,
Continue reading...