public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            ExecuteClientAction(() =>
            {
                if (ShouldProcess(this.VMScaleSetName, VerbsLifecycle.Invoke))
                {
                    string resourceGroupName;
                    string vmScaleSetName;
                    string instanceId;
                    switch (this.ParameterSetName)
                    {
                    case "ResourceIdParameter":
                        resourceGroupName = GetResourceGroupName(this.ResourceId);
                        vmScaleSetName    = GetResourceName(this.ResourceId, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines");
                        instanceId        = GetInstanceId(this.ResourceId, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines");
                        break;

                    case "ObjectParameter":
                        resourceGroupName = GetResourceGroupName(this.VirtualMachineScaleSetVM.Id);
                        vmScaleSetName    = GetResourceName(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines");
                        instanceId        = GetInstanceId(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines");
                        break;

                    default:
                        resourceGroupName = this.ResourceGroupName;
                        vmScaleSetName    = this.VMScaleSetName;
                        instanceId        = this.InstanceId;
                        break;
                    }
                    RunCommandInput parameters = new RunCommandInput();
                    parameters.CommandId       = this.CommandId;
                    if (this.ScriptPath != null)
                    {
                        parameters.Script          = new List <string>();
                        PathIntrinsics currentPath = SessionState.Path;
                        var filePath       = new System.IO.FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(this.ScriptPath));
                        string fileContent = Commands.Common.Authentication.Abstractions.FileUtilities.DataStore.ReadFileAsText(filePath.FullName);
                        parameters.Script  = fileContent.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
                    }
                    if (this.Parameter != null)
                    {
                        var vParameter = new List <RunCommandInputParameter>();
                        foreach (var key in this.Parameter.Keys)
                        {
                            RunCommandInputParameter p = new RunCommandInputParameter();
                            p.Name  = key.ToString();
                            p.Value = this.Parameter[key].ToString();
                            vParameter.Add(p);
                        }
                        parameters.Parameters = vParameter;
                    }

                    var result   = VirtualMachineScaleSetVMsClient.RunCommand(resourceGroupName, vmScaleSetName, instanceId, parameters);
                    var psObject = new PSRunCommandResult();
                    ComputeAutomationAutoMapperProfile.Mapper.Map <RunCommandResult, PSRunCommandResult>(result, psObject);
                    WriteObject(psObject);
                }
            });
        }
Beispiel #2
0
        protected override void ProcessRecord()
        {
            AutoMapper.Mapper.AddProfile <ComputeAutomationAutoMapperProfile>();
            ExecuteClientAction(() =>
            {
                if (ShouldProcess(this.VMName, VerbsLifecycle.Invoke))
                {
                    string resourceGroupName   = this.ResourceGroupName;
                    string vmName              = this.VMName;
                    RunCommandInput parameters = new RunCommandInput();
                    parameters.CommandId       = this.CommandId;
                    if (this.ScriptPath != null)
                    {
                        parameters.Script          = new List <string>();
                        PathIntrinsics currentPath = SessionState.Path;
                        var filePath       = new System.IO.FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(this.ScriptPath));
                        string fileContent = WindowsAzure.Commands.Common.FileUtilities.DataStore.ReadFileAsText(filePath.FullName);
                        parameters.Script  = fileContent.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
                    }
                    if (this.Parameter != null)
                    {
                        var vParameter = new List <RunCommandInputParameter>();
                        foreach (var key in this.Parameter.Keys)
                        {
                            RunCommandInputParameter p = new RunCommandInputParameter();
                            p.Name  = key.ToString();
                            p.Value = this.Parameter[key].ToString();
                            vParameter.Add(p);
                        }
                        parameters.Parameters = vParameter;
                    }
                    if (this.VM != null)
                    {
                        vmName            = VM.Name;
                        resourceGroupName = VM.ResourceGroupName;
                    }

                    var result   = VirtualMachinesClient.RunCommand(resourceGroupName, vmName, parameters);
                    var psObject = new PSRunCommandResult();
                    Mapper.Map <RunCommandResult, PSRunCommandResult>(result, psObject);
                    WriteObject(psObject);
                }
            });
        }