I create distribution group using PowerShell in a C# app.
I want ot change the parameter which allow external users to send mail to this group.
On the the technet I found https://technet.microsoft.com/fr-fr/library/aa998856(v=exchg.160).aspx
And I try to add a parameter to my PowerShell command. For executing PowerShell in my app I use the way :
ps.Commands = new PSCommand().AddCommand("New-DistributionGroup").AddParameter("Name", "GRP_DIF_" + textBox1.Text);
ps.Invoke();
This code create a distribution group successfully but when I want to change the parameter
RequireSenderAuthenticationEnabled
using
[...].AddParameter("RequireSenderAuthenticationEnabled", "$false")
I have an
Unknow parameter : RequireSenderAuthenticationEnabled
Is there a special way to change this parameter ?
Related
I am trying to call PowerShell ISE Script from the C#.
I have command that I am running it on the PowerShell
. .\Commands.ps1; Set-Product -bProduct 'Reg' -IPPoint 'ServerAddress' -Location 'testlocation' -Terminal 3
Now I am trying to create the Command with the c# I have wrote some code Like this.
//Set Execution Policy to un restrict
powershell.AddCommand("Set-ExecutionPolicy");
powershell.AddArgument("unrestricted");
powershell.Invoke();
powershell.Commands.Clear();
powershell.AddScript("K:\\Auto\\Cases\\Location\\Commands.ps1", false);
powershell.AddArgument("Set-Product").AddParameter("bProduct ", "Reg").
AddParameter("IPPoint", "ServerAddress").
AddParameter("Location", "testlocation").AddParameter("Terminal", 3);
powershell.Invoke();
I can see its running fine. But its not updating values in my xml file. It suppose to update my values in file. When I try to run it with powershell It does run and works file. But c# code does not work.
Any hint or clue will be appreciated.
Mind the semicolon, so this is basically two statements:
1.) Dot-sourcing the script Commands.ps1
. .\Commands.ps1
2.) Invoking the cmdlet Set-Product
Set-Product -bProduct 'Reg' -IPPoint 'ServerAddress' -Location 'testlocation' -Terminal 3
So, you have to treat them as such. Also, AddScript expects code, not a file name.
powershell
// dot-source the script
.AddScript(#". 'K:\Auto\Cases\Location\Commands.ps1'")
// this is the semicolon = add another statement
.AddStatement()
// add the cmdlet
.AddCommand("Set-Product")
.AddParameter("bProduct", "Reg")
.AddParameter("IPPoint", "ServerAddress")
.AddParameter("Location", "testlocation")
.AddParameter("Terminal", 3)
// invoke all statements
.Invoke();
(Alternatively to AddStatement() you can of course split this up in two calls and call Invoke() twice.)
I'm using postgres in a c# project and i'm trying to make some basic queries such as
qry.CommandText = "select * from LOGIN";
NpgsqlDataReader qryReader = qry.ExecuteReader();
But it says that the table LOGIN does not exist.
I already know that the following query works: qry.CommandText = "select * from \"myDataBase\".LOGIN"; but I would prefer not using it.
I also know from this thread that I can use SET search_path TO myschema,public; to access the table without prefix in psql command line, but I don't see how it would work for my c# projet.
Also, I have another project where I don't need the prefix but I don't know why it works for my other projet and not this one.
Any help would be very appreciated.
Thanks
While SET search_path TO myschema,public; sets the search_path for the current session only, you can make it permanent for the user running the command:
ALTER USER username SET search_path TO myschema, public;
I'm trying to run some Powershell cmdlet in a C# program I've been working on.
The cmdlet I've been trying to run is the following:
$cred = New-Object System.Management.Automation.PSCredential (user, (ConvertTo-SecureString pass –ASPlainText –Force));
And what I did in my C# programm was the following:
string user = textBox1.Text;
string pass = textBox2.Text;
PowerShell ps = PowerShell.Create();
ps.AddCommand("New-Object");
ps.AddArgument("System.Management.Automation.PSCredential ("+user+", (ConvertTo-SecureString "+pass+" –ASPlainText –Force))");
var cred = ps.Invoke();
But when i do this I get prompted with the following error:
A constructor was not found. Cannot find an appropriate constructor for type System.Management.Automation.PSCredential (user, (ConvertTo-SecureString pass –ASPlainText –Force)).
So my question is, how can I run this Powershell cmdlet from my C# program, and store the result in a variable inside the C# program?
Thank you!
This can be done without invoking powershell. Whether thats useful or not depends upon what you are up to.
var user = "username";
var pass = new System.Security.SecureString();
foreach (char c in "password")
{
pass.AppendChar(c);
}
var cred = new System.Management.Automation.PSCredential(user, pass);
Martin Brown's helpful answer is definitely the best solution in your case.
As for what you tried:
You didn't correctly translate your PowerShell command to a PowerShell SDK call.
Specifically, the way you add arguments is incorrect:
ps.AddArgument("System.Management.Automation.PSCredential ("+user+", (ConvertTo-SecureString "+pass+" –ASPlainText –Force))");
You must add arguments one by one, via .AddArgument(<val>), or, preferably, as named parameters via .AddParameter(<name>, <val>)).
You cannot use embedded PowerShell commands as arguments.
If we leave the issue of obtaining a SecureString instance aside and use just a dummy instance, this is what your statement would have to look like:
ps.AddCommand("New-Object")
.AddParameter("TypeName", "System.Management.Automation.PSCredential")
.AddParameter("ArgumentList", new object[] { user, new System.Security.SecureString() });
Note the use of parameter names and how parameter -ArgumentList must be passed as an array.
If you do need to execute PowerShell code via the SDK, use the .AddScript() method instead, but note that you can only pass a single string that contains the code to execute (note the use of an interpolated C# string, $"..." for embedding C# variable values):
ps.AddScript(
$"New-Object PSCredential \"{user}\", (ConvertTo-SecureString \"{pass}\" –AsPlainText –Force)"
);
Caveat: Unlike a command added with .AddCommand(), an .AddScript()-added command always fails silently on execution with .Invoke() - no exception occurs; you'll have to inspect ps.HadErrors and ps.Streams.Error to check for errors. By contrast, .AddCommand() does throw an exception if the target command reports a (statement-)terminating error (though these are rare; an example is passing an invalid parameter name).
I am having issues updating the ManagedBy property for Security Groups (works fine for Distribution Groups). I receive the following error: '{"You don't have sufficient permissions. This operation can only be performed by a manager of the group."}'
I am running with an account that has full access. The command works when running via PowerShell just not via C#. Here is the command I am running.
Command cmdCreateDR = new Command("Set-DistributionGroup");
cmdCreateDR.Parameters.Add("Identity", "GroupName");
cmdCreateDR.Parameters.Add(new CommandParameter("BypassSecurityGroupManagerCheck"));
Collection<PSObject> manByUsers = new Collection<PSObject>();
manByUsers.Add(new PSObject("domain\\UserName1"));
manByUsers.Add(new PSObject("domain\\UserName2"));
cmdCreateDR.Parameters.Add("ManagedBy", manByUsers);
Collection<PSObject> resultsEx = RunPowerShellCommand(cmdCreateDR);
BypassSecuirtyGroupManagerCheck is a switch and the way your using it your passing it in a bool set to false which won't achieve your desired results. You should be doing something like
SwitchParameter switchpara = new SwitchParameter(true);
cmdCreateDR.Parameters.Add("BypassSecurityGroupManagerCheck", switchpara);
Cheers
Glen
I am currently developing a system that will allow for an external piece of software to click a button and his will then execute some c#.net code that plans to call the Dynamics NAV RTC by using the following code.
Process.Start("Microsoft.Dynamics.Nav.Client.exe");
The external application contains variables that I would like to pass through to the NAV CRM.
Is there a way that i could do this by Passing the parameters like what you would with a web address similar to the way below:
Process.Start("Microsoft.Dynamics.Nav.Client.exe", "DynamicsNAV://localhost:7046/DynamicsNAV70/CRONUS%20UK%20Ltd./RunPage?Page=50000&No=10");
The above line doesn't work. I receive the followowing error:
Priming dictionary contains a key 'no' which is not allowed
Parameter name: primingDictionary
Does anyone in the community know how I could produce this functionality in a similar way?
you can use it like that:
ProcessStartInfo psi = new ProcessStartInfo("Microsoft.Dynamics.Nav.Client.exe",
"DynamicsNAV://localhost:7046/DynamicsNAV70/CRONUS%20UK%20Ltd./RunPage?Page=50000&No=10");
Process.Start(psi);
the first argument is the process itself, the secomd is the argument.
you can change them as you'd like
you can learn on the argument NAV accept here
Yes, just call the overload of Process.Start() that takes input arguments:
Process.Start("Microsoft.Dynamics.Nav.Client.exe", "DynamicsNAV://localhost:7046/DynamicsNAV70/CRONUS%20UK%20Ltd./RunPage?Page=50000&No=10");