Click here to Skip to main content
15,892,059 members
Home / Discussions / C#
   

C#

 
GeneralRe: VB.net dll use with C# windows form Pin
BobbyStrain4-Sep-18 17:17
BobbyStrain4-Sep-18 17:17 
GeneralRe: VB.net dll use with C# windows form Pin
Mycroft Holmes4-Sep-18 19:21
professionalMycroft Holmes4-Sep-18 19:21 
GeneralRe: VB.net dll use with C# windows form Pin
BobbyStrain6-Sep-18 11:12
BobbyStrain6-Sep-18 11:12 
QuestionCostura.Fody fails on govt. network Pin
Member 1348136130-Aug-18 10:37
Member 1348136130-Aug-18 10:37 
AnswerRe: Costura.Fody fails on govt. network Pin
Dave Kreskowiak30-Aug-18 10:51
mveDave Kreskowiak30-Aug-18 10:51 
GeneralRe: Costura.Fody fails on govt. network Pin
Member 1348136130-Aug-18 15:09
Member 1348136130-Aug-18 15:09 
GeneralRe: Costura.Fody fails on govt. network Pin
Dave Kreskowiak30-Aug-18 15:52
mveDave Kreskowiak30-Aug-18 15:52 
QuestionRemove-DistributionGroupMember not found in Powershell Runspace Pin
Chad Underwood30-Aug-18 2:45
Chad Underwood30-Aug-18 2:45 
I am unable to get this command to work via a powershell runspace in c#. it works fine in powershell.

Remove-DistributionGroupMember -Identity <groupname> -Member <upn> -Confirm:$false

My guess is that I am not loading the correct command set but I am unable to determine how to do that in this case. This code works for adding users to groups, deleting users, etc, I just change the powershell code to send but for Remove-DistributionGroupMember I get the general command not found error.

any ideas?



C#
public void RemoveUserFromGroup()
    {
        InitialSessionState iss = InitialSessionState.CreateDefault();
        iss.ImportPSModule(new[] { "MSOnline" });
        using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
        {
            myRunSpace.Open();
            // Execute the Get-CsTrustedApplication cmdlet.
            using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
            {
                powershell.Runspace = myRunSpace;
                Command connect = new Command("Connect-MsolService");
                System.Security.SecureString secureString = new System.Security.SecureString();
                string myPassword = loginPassword;
                foreach (char c in myPassword)
                    secureString.AppendChar(c);
                connect.Parameters.Add("Credential", new PSCredential(IdSenderEmail, secureString));
                powershell.Commands.AddCommand(connect);
                Collection<PSObject> results = null;
                Collection<ErrorRecord> errors = null;
                results = powershell.Invoke();
                errors = powershell.Streams.Error.ReadAll();

                var PSCommand = "Remove-DistributionGroupMember -Identity '"+IdGroupName+"' -Member '"+IdUPN+"' -Confirm:$false";

                powershell.Commands.Clear();
//                Command getuser = new Command("Invoke-Command");
//                getuser.Parameters.Add("ScriptBlock", System.Management.Automation.ScriptBlock.Create(PSCommand));
                Command getuser = new Command(PSCommand);
                powershell.Commands.AddCommand(getuser);

                results = null;
                errors = null;
                try
                {
                    results = powershell.Invoke();
                    if (results != null && results.Count > 0)
                    {
                        foreach (PSObject item in results)
                        {
                            IdExceptionError = item.ToString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    IdExceptionError = ex.ToString();
                }
                finally
                {
                    myRunSpace.Close();
                    myRunSpace.Dispose();
                }
            }
        }
    }

AnswerRe: Remove-DistributionGroupMember not found in Powershell Runspace Pin
Richard Deeming30-Aug-18 6:41
mveRichard Deeming30-Aug-18 6:41 
GeneralRe: Remove-DistributionGroupMember not found in Powershell Runspace Pin
Chad Underwood30-Aug-18 8:15
Chad Underwood30-Aug-18 8:15 
AnswerRe: Remove-DistributionGroupMember not found in Powershell Runspace Pin
Chad Underwood31-Aug-18 4:57
Chad Underwood31-Aug-18 4:57 
QuestionEnclosing line for a list of points Pin
Bernhard Hiller28-Aug-18 21:36
Bernhard Hiller28-Aug-18 21:36 
AnswerRe: Enclosing line for a list of points Pin
Luc Pattyn28-Aug-18 22:15
sitebuilderLuc Pattyn28-Aug-18 22:15 
GeneralRe: Enclosing line for a list of points Pin
Peter_in_278028-Aug-18 22:17
professionalPeter_in_278028-Aug-18 22:17 
AnswerRe: Enclosing line for a list of points Pin
Peter_in_278028-Aug-18 22:16
professionalPeter_in_278028-Aug-18 22:16 
Questionsystem.drawing.image condition Pin
Xavier Escalante28-Aug-18 9:15
Xavier Escalante28-Aug-18 9:15 
AnswerRe: system.drawing.image condition Pin
Richard Deeming28-Aug-18 10:44
mveRichard Deeming28-Aug-18 10:44 
GeneralRe: system.drawing.image condition Pin
Xavier Escalante31-Aug-18 6:13
Xavier Escalante31-Aug-18 6:13 
QuestionDetect Router IP Address Change Pin
Kevin Marois28-Aug-18 7:25
professionalKevin Marois28-Aug-18 7:25 
AnswerRe: Detect Router IP Address Change Pin
Dave Kreskowiak28-Aug-18 7:40
mveDave Kreskowiak28-Aug-18 7:40 
GeneralRe: Detect Router IP Address Change Pin
Kevin Marois28-Aug-18 8:24
professionalKevin Marois28-Aug-18 8:24 
AnswerRe: Detect Router IP Address Change Pin
OriginalGriff28-Aug-18 8:07
mveOriginalGriff28-Aug-18 8:07 
GeneralRe: Detect Router IP Address Change Pin
Kevin Marois28-Aug-18 8:24
professionalKevin Marois28-Aug-18 8:24 
GeneralCall WebApi from MVC application and pass the value to a model class Pin
Member 1396461028-Aug-18 3:55
Member 1396461028-Aug-18 3:55 
GeneralRe: Call WebApi from MVC application and pass the value to a model class Pin
Simon_Whale28-Aug-18 4:43
Simon_Whale28-Aug-18 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.