Skip to content

Commit a26ef6a

Browse files
authored
Adding Add-AzVmssRunCommand And Remove-AzVmssRunCommand (#16318)
* Adding manual run commands * Adding Test and Recording * small changes and changelog.md * adding help * fixing help * change from parameters to parameter
1 parent 8d299b0 commit a26ef6a

File tree

11 files changed

+4032
-2
lines changed

11 files changed

+4032
-2
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,12 @@ public void TestVirtualMachineScaleSetFlexibleOModeDefaulting()
220220
{
221221
TestRunner.RunTestScript("Test-VirtualMachineScaleSetFlexibleOModeDefaulting");
222222
}
223+
224+
[Fact]
225+
[Trait(Category.AcceptanceType, Category.CheckIn)]
226+
public void TestAddAzVmssRunCommand()
227+
{
228+
TestRunner.RunTestScript("Test-AddAndRemoveAzVmssRunCommand");
229+
}
223230
}
224231
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,3 +2873,45 @@ function Test-VirtualMachineScaleSetFlexibleOModeDefaulting
28732873
}
28742874
}
28752875

2876+
<#
2877+
.SYNOPSIS
2878+
Test Add and remove Vmss Run Command.
2879+
#>
2880+
function Test-AddAndRemoveAzVmssRunCommand
2881+
{
2882+
2883+
# Setup
2884+
$rgname = Get-ComputeTestResourceName;
2885+
2886+
try
2887+
{
2888+
# Common
2889+
$loc = "eastus";
2890+
2891+
$loc = Get-ComputeVMLocation;
2892+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
2893+
2894+
# Setup variables
2895+
$vmssname = "vmss" + $rgname;
2896+
$domainNameLabel = "dnl" + $rgname;
2897+
$username = "admin01"
2898+
$securePassword = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force
2899+
2900+
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword);
2901+
2902+
# Create VMSS with minimal inputs to allow defaulting
2903+
$vmss = New-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssname -ImageName 'Win2016Datacenter' -Credential $credential -InstanceCount 1 -DomainNameLabel $domainNameLabel
2904+
#$vmss = New-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssname -Credential $credential -OrchestrationMode $omode -DomainNameLabel $domainNameLabel;
2905+
2906+
$runcmds = Add-AzVmssRunCommand -ResourceGroupName $rgname -VMScaleSetName $vmssname -Location eastus -Name myruncommand -Script "Write-Host Hello World" -TimeOutInSeconds 3600 -RunAsUser "admin01"
2907+
Assert-AreEqual $runcmds.RunAsUser "admin01";
2908+
Assert-AreEqual $runcmds.TimeOutInSeconds "3600";
2909+
Remove-AzVmssRunCommand -ResourceGroupName $rgname -VMScaleSetName $vmssname -Name myruncommand
2910+
2911+
}
2912+
finally
2913+
{
2914+
# Cleanup
2915+
Clean-ResourceGroup $rgname;
2916+
}
2917+
}

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineScaleSetTests/TestAddAzVmssRunCommand.json

Lines changed: 3050 additions & 0 deletions
Large diffs are not rendered by default.

src/Compute/Compute/Az.Compute.psd1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,11 @@ CmdletsToExport = 'Remove-AzAvailabilitySet', 'Get-AzAvailabilitySet',
180180
'New-AzRestorePointCollection', 'Get-AzRestorePointCollection',
181181
'Update-AzRestorePointCollection',
182182
'Remove-AzRestorePointCollection', 'New-AzRestorePoint',
183-
'Get-AzRestorePoint', 'Remove-AzRestorePoint',
183+
'Get-AzRestorePoint', 'Remove-AzRestorePoint',
184184
'New-AzVmGalleryApplication', 'New-AzVmssGalleryApplication',
185185
'Add-AzVmGalleryApplication', 'Add-AzVmssGalleryApplication',
186-
'Remove-AzVmGalleryApplication', 'Remove-AzVmssGalleryApplication'
186+
'Remove-AzVmGalleryApplication', 'Remove-AzVmssGalleryApplication',
187+
'Add-AzVmssRunCommand', 'Remove-AzVmssRunCommand'
187188

188189
# Variables to export from this module
189190
# VariablesToExport = @()

src/Compute/Compute/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
-->
2222
## Upcoming Release
2323
* Update-AzVM will update ApplicationProfile.
24+
* Adding new cmdlets:
25+
- Add-AzVmssRunCommand
26+
- Remove-AzVmssRunCommand
2427

2528
## Version 4.18.0
2629
* Added cmdlets for adding VMGalleryApplication property to VM/VMSS

src/Compute/Compute/Generated/ComputeAutomationBaseCmdlet.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ public IVirtualMachineScaleSetVMsOperations VirtualMachineScaleSetVMsClient
234234
}
235235
}
236236

237+
public IVirtualMachineScaleSetVMRunCommandsOperations VirtualMachineScaleSetVMRunCommandsClient
238+
{
239+
get
240+
{
241+
return ComputeClient.ComputeManagementClient.VirtualMachineScaleSetVMRunCommands;
242+
}
243+
}
244+
237245
public IVirtualMachinesOperations VirtualMachinesClient
238246
{
239247
get
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
//
2+
// Copyright (c) Microsoft and contributors. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
//
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
// Warning: This code was generated by a tool.
18+
//
19+
// Changes to this file may cause incorrect behavior and will be lost if the
20+
// code is regenerated.
21+
22+
using System;
23+
using System.Collections;
24+
using System.Collections.Generic;
25+
using System.Linq;
26+
using System.Management.Automation;
27+
using Microsoft.Azure.Commands.Compute.Automation.Models;
28+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
29+
using Microsoft.Azure.Management.Compute;
30+
using Microsoft.Azure.Management.Compute.Models;
31+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
32+
using System.Security;
33+
34+
namespace Microsoft.Azure.Commands.Compute.Automation
35+
{
36+
[Cmdlet("Add", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VmssRunCommand", DefaultParameterSetName = "DefaultParameter", SupportsShouldProcess = true)]
37+
[OutputType(typeof(List<PSVirtualMachineRunCommand>))]
38+
public partial class AddAzVmssRunCommand : ComputeAutomationBaseCmdlet
39+
{
40+
public override void ExecuteCmdlet()
41+
{
42+
base.ExecuteCmdlet();
43+
ExecuteClientAction(() =>
44+
{
45+
if (ShouldProcess(this.VMScaleSetName, "Add"))
46+
{
47+
string resourceGroupName;
48+
string vmScaleSetName;
49+
switch (this.ParameterSetName)
50+
{
51+
case "ObjectParameter":
52+
resourceGroupName = GetResourceGroupName(this.VirtualMachineScaleSetVM.Id);
53+
vmScaleSetName = GetResourceName(this.VirtualMachineScaleSetVM.Id, "Microsoft.Compute/virtualMachineScaleSets", "virtualMachines");
54+
break;
55+
default:
56+
resourceGroupName = this.ResourceGroupName;
57+
vmScaleSetName = this.VMScaleSetName;
58+
break;
59+
}
60+
VirtualMachineRunCommand vmruncommand;
61+
62+
vmruncommand = new VirtualMachineRunCommand
63+
{
64+
OutputBlobUri = this.IsParameterBound(c => c.OutputBlobUri) ? this.OutputBlobUri : null,
65+
ErrorBlobUri = this.IsParameterBound(c => c.ErrorBlobUri) ? this.ErrorBlobUri : null,
66+
RunAsPassword = this.IsParameterBound(c => c.RunAsPassword) ? this.RunAsPassword.ToString() : null,
67+
RunAsUser = this.IsParameterBound(c => c.RunAsUser) ? this.RunAsUser : null,
68+
TimeoutInSeconds = this.IsParameterBound(c => c.TimeOutInSeconds) ? Convert.ToInt32(this.TimeOutInSeconds) : (int?)null,
69+
Location = this.IsParameterBound(c => c.Location) ? this.Location : null,
70+
AsyncExecution = this.IsParameterBound(c => c.AsyncExecution) ? this.AsyncExecution : null
71+
};
72+
73+
74+
if (this.IsParameterBound(c => c.ScriptUri)) {
75+
vmruncommand.Source = new VirtualMachineRunCommandScriptSource
76+
{
77+
Script = this.IsParameterBound(c => c.Script) ? this.Script : null,
78+
ScriptUri = this.IsParameterBound(c => c.ScriptUri) ? this.ScriptUri : null,
79+
CommandId = this.IsParameterBound(c => c.CommandId) ? this.CommandId : null
80+
};
81+
}
82+
else if (this.ScriptPath != null && this.IsParameterBound(c => c.ScriptPath))
83+
{
84+
List<string> Script = new List<string>();
85+
PathIntrinsics currentPath = SessionState.Path;
86+
var filePath = new System.IO.FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(this.ScriptPath));
87+
string fileContent = Commands.Common.Authentication.Abstractions.FileUtilities.DataStore.ReadFileAsText(filePath.FullName);
88+
Script = fileContent.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries).ToList();
89+
90+
vmruncommand.Source = new VirtualMachineRunCommandScriptSource(this.Script, this.ScriptPath, this.CommandId);
91+
}
92+
93+
if (this.Parameter != null)
94+
{
95+
var vParameter = new List<RunCommandInputParameter>();
96+
foreach (var key in this.Parameter.Keys)
97+
{
98+
RunCommandInputParameter p = new RunCommandInputParameter();
99+
100+
p.Name = key.ToString();
101+
p.Value = this.Parameter[key].ToString();
102+
vParameter.Add(p);
103+
}
104+
vmruncommand.Parameters = vParameter;
105+
}
106+
else if (this.ProtectedParameter != null)
107+
{
108+
var vParameter = new List<RunCommandInputParameter>();
109+
foreach (var key in this.ProtectedParameter.Keys)
110+
{
111+
RunCommandInputParameter p = new RunCommandInputParameter();
112+
113+
p.Name = key.ToString();
114+
p.Value = this.ProtectedParameter[key].ToString();
115+
vParameter.Add(p);
116+
}
117+
vmruncommand.ProtectedParameters = vParameter;
118+
}
119+
120+
var expand = new Microsoft.Rest.Azure.OData.ODataQuery<VirtualMachineScaleSetVM>();
121+
expand.Expand = "instanceView";
122+
var vmList = VirtualMachineScaleSetVMsClient.List(resourceGroupName, vmScaleSetName, expand);
123+
var resultList = vmList.ToList();
124+
var nextPageLink = vmList.NextPageLink;
125+
while (!string.IsNullOrEmpty(nextPageLink))
126+
{
127+
var pageResult = VirtualMachineScaleSetVMsClient.ListNext(nextPageLink);
128+
foreach (var pageItem in pageResult)
129+
{
130+
resultList.Add(pageItem);
131+
}
132+
nextPageLink = pageResult.NextPageLink;
133+
}
134+
List<VirtualMachineScaleSetVM> listOfVms = new List<VirtualMachineScaleSetVM>(resultList);
135+
List<PSVirtualMachineRunCommand> output = new List<PSVirtualMachineRunCommand>();
136+
137+
138+
foreach (VirtualMachineScaleSetVM vm in listOfVms)
139+
{
140+
var resultOfRunCmd = VirtualMachineScaleSetVMRunCommandsClient.BeginCreateOrUpdateAsync(resourceGroupName, vmScaleSetName, vm.InstanceId, this.Name, vmruncommand);
141+
var Pstemp = new PSVirtualMachineRunCommand();
142+
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineRunCommand, PSVirtualMachineRunCommand>(resultOfRunCmd.Result, Pstemp);
143+
output.Add(Pstemp);
144+
}
145+
146+
WriteObject(output);
147+
}
148+
});
149+
}
150+
151+
[Parameter(
152+
ParameterSetName = "DefaultParameter",
153+
Position = 0,
154+
Mandatory = true,
155+
ValueFromPipelineByPropertyName = true)]
156+
[ResourceGroupCompleter]
157+
public string ResourceGroupName { get; set; }
158+
159+
[Parameter(
160+
ParameterSetName = "DefaultParameter",
161+
Position = 1,
162+
Mandatory = true,
163+
ValueFromPipelineByPropertyName = true)]
164+
[ResourceNameCompleter("Microsoft.Compute/virtualMachineScaleSets", "ResourceGroupName")]
165+
public string VMScaleSetName { get; set; }
166+
167+
[Parameter(
168+
ParameterSetName = "DefaultParameter",
169+
Position = 2,
170+
Mandatory = true,
171+
ValueFromPipelineByPropertyName = true)]
172+
public string Location { get; set; }
173+
174+
[Parameter(
175+
Mandatory = false)]
176+
[AllowNull]
177+
public string CommandId { get; set; }
178+
179+
[Parameter(
180+
Mandatory = true)]
181+
[AllowNull]
182+
public string Name { get; set; }
183+
184+
[Parameter(
185+
Mandatory = false)]
186+
[AllowNull]
187+
public string ScriptPath { get; set; }
188+
189+
[Parameter(
190+
Mandatory = false)]
191+
[AllowNull]
192+
public string ScriptUri { get; set; }
193+
194+
[Parameter(
195+
Mandatory = false)]
196+
[AllowNull]
197+
public string Script { get; set; }
198+
199+
[Parameter(
200+
Mandatory = false)]
201+
[AllowNull]
202+
public string TimeOutInSeconds { get; set; }
203+
204+
[Parameter(
205+
Mandatory = false)]
206+
[AllowNull]
207+
public string ErrorBlobUri { get; set; }
208+
209+
[Parameter(
210+
Mandatory = false)]
211+
[AllowNull]
212+
public string OutputBlobUri { get; set; }
213+
214+
[Parameter(
215+
Mandatory = false)]
216+
[AllowNull]
217+
public string RunAsUser { get; set; }
218+
219+
[Parameter(
220+
Mandatory = false)]
221+
[AllowNull]
222+
public SecureString RunAsPassword { get; set; }
223+
224+
[Parameter(
225+
Mandatory = false)]
226+
[AllowNull]
227+
public Hashtable Parameter { get; set; }
228+
229+
[Parameter(
230+
Mandatory = false)]
231+
[AllowNull]
232+
public Hashtable ProtectedParameter { get; set; }
233+
234+
[Parameter(
235+
Mandatory = false)]
236+
public Nullable<Boolean> AsyncExecution { get; set; }
237+
238+
239+
[Parameter(
240+
ParameterSetName = "ObjectParameter",
241+
Position = 0,
242+
Mandatory = true,
243+
ValueFromPipelineByPropertyName = true,
244+
ValueFromPipeline = true)]
245+
[ValidateNotNullOrEmpty]
246+
public PSVirtualMachineScaleSetVM VirtualMachineScaleSetVM { get; set; }
247+
248+
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
249+
public SwitchParameter AsJob { get; set; }
250+
}
251+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+

2+
using Microsoft.Rest.Serialization;
3+
using Newtonsoft.Json;
4+
using System.Collections.Generic;
5+
6+
namespace Microsoft.Azure.Management.Compute.Models
7+
{
8+
9+
public class PSVirtualMachineRunCommand : Resource
10+
{
11+
public VirtualMachineRunCommandScriptSource Source { get; set; }
12+
public IList<RunCommandInputParameter> Parameters { get; set; }
13+
public IList<RunCommandInputParameter> ProtectedParameters { get; set; }
14+
public bool? AsyncExecution { get; set; }
15+
public string RunAsUser { get; set; }
16+
public string RunAsPassword { get; set; }
17+
public int? TimeoutInSeconds { get; set; }
18+
public string OutputBlobUri { get; set; }
19+
public string ErrorBlobUri { get; set; }
20+
public string ProvisioningState { get; }
21+
public VirtualMachineRunCommandInstanceView InstanceView { get; set; }
22+
}
23+
}

0 commit comments

Comments
 (0)