|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
15 | 15 | using System;
|
| 16 | +using System.Diagnostics; |
16 | 17 | using System.IO;
|
17 | 18 | using System.Linq;
|
18 | 19 | using System.Management.Automation;
|
@@ -46,15 +47,32 @@ public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseC
|
46 | 47 | Mandatory = true,
|
47 | 48 | Position = 2,
|
48 | 49 | ValueFromPipelineByPropertyName = true,
|
49 |
| - HelpMessage = "Path and name of the output RDP file.")] |
| 50 | + HelpMessage = "Path and name of the output RDP file.", |
| 51 | + ParameterSetName = "Download")] |
| 52 | + [Parameter( |
| 53 | + Mandatory = false, |
| 54 | + Position = 2, |
| 55 | + ValueFromPipelineByPropertyName = true, |
| 56 | + HelpMessage = "Path and name of the output RDP file.", |
| 57 | + ParameterSetName = "Launch")] |
50 | 58 | [ValidateNotNullOrEmpty]
|
51 |
| - public string LocalPath { get; set;} |
| 59 | + public string LocalPath { get; set; } |
| 60 | + |
| 61 | + [Parameter( |
| 62 | + Mandatory = true, |
| 63 | + Position = 3, |
| 64 | + HelpMessage = "Start a remote desktop session to the specified role instance.", |
| 65 | + ParameterSetName = "Launch")] |
| 66 | + public SwitchParameter Launch |
| 67 | + { |
| 68 | + get; |
| 69 | + set; |
| 70 | + } |
52 | 71 |
|
53 | 72 | public override void ExecuteCmdlet()
|
54 | 73 | {
|
55 | 74 | base.ExecuteCmdlet();
|
56 | 75 |
|
57 |
| - |
58 | 76 | ExecuteClientAction(() =>
|
59 | 77 | {
|
60 | 78 | const string fullAddressPrefix = "full address:s:";
|
@@ -138,11 +156,46 @@ public override void ExecuteCmdlet()
|
138 | 156 | }
|
139 | 157 |
|
140 | 158 | // Write to file
|
141 |
| - using (var file = new StreamWriter(this.LocalPath)) |
| 159 | + string rdpFilePath = this.LocalPath ?? Path.GetTempFileName(); |
| 160 | + |
| 161 | + using (var file = new StreamWriter(rdpFilePath)) |
142 | 162 | {
|
143 | 163 | file.WriteLine(fullAddressPrefix + address + ":" + port);
|
144 | 164 | file.WriteLine(promptCredentials);
|
145 | 165 | }
|
| 166 | + |
| 167 | + if (Launch.IsPresent) |
| 168 | + { |
| 169 | + var startInfo = new ProcessStartInfo |
| 170 | + { |
| 171 | + CreateNoWindow = true, |
| 172 | + WindowStyle = ProcessWindowStyle.Hidden |
| 173 | + }; |
| 174 | + |
| 175 | + if (this.LocalPath == null) |
| 176 | + { |
| 177 | + string scriptGuid = Guid.NewGuid().ToString(); |
| 178 | + |
| 179 | + string launchRDPScript = Path.GetTempPath() + scriptGuid + ".bat"; |
| 180 | + using (var scriptStream = File.OpenWrite(launchRDPScript)) |
| 181 | + { |
| 182 | + var writer = new StreamWriter(scriptStream); |
| 183 | + writer.WriteLine("start /wait mstsc.exe " + rdpFilePath); |
| 184 | + writer.WriteLine("del " + rdpFilePath); |
| 185 | + writer.WriteLine("del " + launchRDPScript); |
| 186 | + writer.Flush(); |
| 187 | + } |
| 188 | + |
| 189 | + startInfo.FileName = launchRDPScript; |
| 190 | + } |
| 191 | + else |
| 192 | + { |
| 193 | + startInfo.FileName = "mstsc.exe"; |
| 194 | + startInfo.Arguments = rdpFilePath; |
| 195 | + } |
| 196 | + |
| 197 | + Process.Start(startInfo); |
| 198 | + } |
146 | 199 | });
|
147 | 200 | }
|
148 | 201 |
|
|
0 commit comments