// Additional custom code for this activity /// <summary> /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run. /// </summary> /// <param name="context">The NativeActivityContext for the currently running activity.</param> /// <returns>A populated instance of System.Management.Automation.PowerShell</returns> /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks> protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context) { System.Management.Automation.PowerShell invoker = global::System.Management.Automation.PowerShell.Create(); System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName); // Initialize the arguments if (Namespace.Expression != null) { targetCommand.AddParameter("Namespace", Namespace.Get(context)); } if (OperationTimeoutSec.Expression != null) { targetCommand.AddParameter("OperationTimeoutSec", OperationTimeoutSec.Get(context)); } if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (Query.Expression != null) { targetCommand.AddParameter("Query", Query.Get(context)); } if (QueryDialect.Expression != null) { targetCommand.AddParameter("QueryDialect", QueryDialect.Get(context)); } if (Property.Expression != null) { targetCommand.AddParameter("Property", Property.Get(context)); } if (PassThru.Expression != null) { targetCommand.AddParameter("PassThru", PassThru.Get(context)); } if (ResourceUri != null) { targetCommand.AddParameter("ResourceUri", ResourceUri.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
/// <summary> /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run. /// </summary> /// <param name="context">The NativeActivityContext for the currently running activity.</param> /// <returns>A populated instance of System.Management.Automation.PowerShell</returns> /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks> protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context) { System.Management.Automation.PowerShell invoker = global::System.Management.Automation.PowerShell.Create(); System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName); // Initialize the arguments if (ClassName.Expression != null) { targetCommand.AddParameter("ClassName", ClassName.Get(context)); } if (Key.Expression != null) { targetCommand.AddParameter("Key", Key.Get(context)); } if (CimClass.Expression != null) { targetCommand.AddParameter("CimClass", CimClass.Get(context)); } if (Property.Expression != null) { targetCommand.AddParameter("Property", Property.Get(context)); } if (Namespace.Expression != null) { targetCommand.AddParameter("Namespace", Namespace.Get(context)); } if (OperationTimeoutSec.Expression != null) { targetCommand.AddParameter("OperationTimeoutSec", OperationTimeoutSec.Get(context)); } if (ResourceUri != null) { targetCommand.AddParameter("ResourceUri", ResourceUri.Get(context)); } if (ClientOnly.Expression != null) { // Retrieve our host overrides var hostValues = context.GetExtension <HostParameterDefaults>(); string[] computerName = null; if (hostValues != null) { Dictionary <string, object> incomingArguments = hostValues.Parameters; if (incomingArguments.ContainsKey("PSComputerName")) { computerName = incomingArguments["PSComputerName"] as string[]; } } if (computerName == null) { targetCommand.AddParameter("ClientOnly", ClientOnly.Get(context)); } } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
protected override List <ActivityImplementationContext> GetImplementation(NativeActivityContext context) { bool needRunspace = !typeof(GenericCimCmdletActivity).IsAssignableFrom(this.GetType()); string[] computernames = PSComputerName.Get(context); CimSession[] sessions = this.CimSession.Get(context); Uri resourceUri = null; if (ResourceUri != null) { resourceUri = ResourceUri.Get(context); } List <ActivityImplementationContext> commands = new List <ActivityImplementationContext>(); // Configure the remote connectivity options... if (computernames != null && computernames.Length > 0) { WSManSessionOptions sessionOptions = new WSManSessionOptions(); // Set a timeout on the connection... uint?timeout = PSActionRunningTimeoutSec.Get(context); if (timeout.HasValue) { sessionOptions.Timeout = TimeSpan.FromSeconds((double)(timeout.Value)); } // See if we should use SSL or not... bool?useSsl = PSUseSsl.Get(context); bool sessionOptionUseSsl = false; if (useSsl.HasValue) { sessionOptions.UseSsl = useSsl.Value; sessionOptionUseSsl = useSsl.Value; } // Set the port to use uint?port = PSPort.Get(context); uint sessionOptionPort = 0; if (port.HasValue) { sessionOptions.DestinationPort = port.Value; sessionOptionPort = port.Value; } // Map over options from PSSessionConfig to WSManSessionOptions PSSessionOption pso = PSSessionOption.Get(context); if (pso != null) { sessionOptions.NoEncryption = pso.NoEncryption; sessionOptions.CertCACheck = pso.SkipCACheck; sessionOptions.CertCNCheck = pso.SkipCNCheck; sessionOptions.CertRevocationCheck = pso.SkipRevocationCheck; if (pso.UseUTF16) { sessionOptions.PacketEncoding = PacketEncoding.Utf16; } if (pso.Culture != null) { sessionOptions.Culture = pso.Culture; } if (pso.UICulture != null) { sessionOptions.UICulture = pso.UICulture; } if (pso.ProxyCredential != null) { string[] parts = pso.ProxyCredential.UserName.Split('\\'); string domain, userid; if (parts.Length < 2) { domain = string.Empty; userid = parts[0]; } else { domain = parts[0]; userid = parts[1]; } sessionOptions.AddProxyCredentials( new CimCredential(ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(pso.ProxyAuthentication), domain, userid, pso.ProxyCredential.Password)); } switch (pso.ProxyAccessType) { case ProxyAccessType.WinHttpConfig: sessionOptions.ProxyType = ProxyType.WinHttp; break; case ProxyAccessType.AutoDetect: sessionOptions.ProxyType = ProxyType.Auto; break; case ProxyAccessType.IEConfig: sessionOptions.ProxyType = ProxyType.InternetExplorer; break; } } PSCredential pscreds = PSCredential.Get(context); string certificateThumbprint = PSCertificateThumbprint.Get(context); if (pscreds != null && certificateThumbprint != null) { throw new ArgumentException(Resources.CredentialParameterCannotBeSpecifiedWithPSCertificateThumbPrint); } PasswordAuthenticationMechanism passwordAuthenticationMechanism = PasswordAuthenticationMechanism.Default; AuthenticationMechanism? authenticationMechanism = PSAuthentication.Get(context); if (authenticationMechanism.HasValue) { passwordAuthenticationMechanism = ConvertPSAuthenticationMechanismToCimPasswordAuthenticationMechanism(authenticationMechanism.Value); } if (certificateThumbprint != null) { sessionOptions.AddDestinationCredentials(new CimCredential(CertificateAuthenticationMechanism.Default, certificateThumbprint)); } if (pscreds != null) { string[] parts = pscreds.UserName.Split('\\'); string domain, userid; if (parts.Length < 2) { domain = string.Empty; userid = parts[0]; } else { domain = parts[0]; userid = parts[1]; } sessionOptions.AddDestinationCredentials(new CimCredential(passwordAuthenticationMechanism, domain, userid, pscreds.Password)); } // Create the PowerShell instance, and add the script to it. if (sessions != null && sessions.Length > 0) { foreach (CimSession session in sessions) { ActivityImplementationContext configuredCommand = GetPowerShell(context); CimActivityImplementationContext activityImplementationContext = new CimActivityImplementationContext( configuredCommand, session.ComputerName, pscreds, certificateThumbprint, authenticationMechanism, sessionOptionUseSsl, sessionOptionPort, pso, session, sessionOptions, ModuleDefinition, resourceUri); commands.Add(activityImplementationContext); //if (needRunspace) // GetRunspaceForCimCmdlet(context, activityImplementationContext); } } else if (this.PSCommandName.Equals("CimCmdlets\\New-CimSession", StringComparison.OrdinalIgnoreCase)) { // NewCimSession activity is a special one as it creates the required sessions based on number of computers specified in one go. ActivityImplementationContext baseContext = GetPowerShell(context); CimActivityImplementationContext activityImplementationContext = new CimActivityImplementationContext(baseContext, null, // ComputerName pscreds, certificateThumbprint, authenticationMechanism, sessionOptionUseSsl, sessionOptionPort, pso, null, // session sessionOptions, ModuleDefinition, resourceUri); commands.Add(activityImplementationContext); } else { foreach (string computer in computernames) { ActivityImplementationContext baseContext = GetPowerShell(context); CimActivityImplementationContext activityImplementationContext = new CimActivityImplementationContext(baseContext, computer, pscreds, certificateThumbprint, authenticationMechanism, sessionOptionUseSsl, sessionOptionPort, pso, null, // session sessionOptions, ModuleDefinition, resourceUri); commands.Add(activityImplementationContext); } } } // Configure the local invocation options else { // Create the PowerShell instance, and add the script to it. ActivityImplementationContext baseContext = GetPowerShell(context); CimActivityImplementationContext activityImplementationContext = new CimActivityImplementationContext(baseContext, null, // ComputerName null, // Credential null, // CertificateThumbprint AuthenticationMechanism.Default, false, // UseSsl 0, // Port null, // PSSessionOption null, // Session null, // CimSessionOptions ModuleDefinition, resourceUri); commands.Add(activityImplementationContext); } return(commands); }