├── .gitattributes ├── .github └── workflows │ └── dotnetcore.yml ├── .gitignore ├── LICENSE ├── PowerShellAsync.sln ├── PowerShellAsync ├── AsyncCmdlet.cs ├── PowerShellAsync.csproj └── TTRider.PowerShellAsync.dll.nuspec ├── PowerShellAsyncExample ├── PowerShellAsyncExample.sln └── PowerShellAsyncExample │ ├── MultiSqlCmdlet.cs │ ├── PowerShellAsyncExample.csproj │ ├── Properties │ └── AssemblyInfo.cs │ └── packages.config ├── UnitTests ├── AsyncCmdletTests.cs ├── Infrastructure │ ├── PsCommandContext.cs │ ├── TestPsHost.cs │ ├── TestPsHostRawUserInterface.cs │ └── TestPsHostUserInterface.cs └── UnitTests.csproj └── readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.github/workflows/dotnetcore.yml: -------------------------------------------------------------------------------- 1 | name: .NET Core 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | 17 | # Setup .NET 18 | - name: Setup .NET Core 19 | uses: actions/setup-dotnet@v1 20 | with: 21 | dotnet-version: 3.1.101 22 | 23 | - name: Restore 24 | run: dotnet restore 25 | 26 | - name: Build with dotnet 27 | run: dotnet build --no-restore --configuration Release 28 | 29 | - name: Test 30 | run: dotnet test --no-restore --verbosity normal 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # ========================= 18 | # Operating System Files 19 | # ========================= 20 | 21 | # OSX 22 | # ========================= 23 | 24 | .DS_Store 25 | .AppleDouble 26 | .LSOverride 27 | 28 | # Icon must end with two \r 29 | Icon 30 | 31 | 32 | # Thumbnails 33 | ._* 34 | 35 | # Files that might appear on external disk 36 | .Spotlight-V100 37 | .Trashes 38 | 39 | # Directories potentially created on remote AFP share 40 | .AppleDB 41 | .AppleDesktop 42 | Network Trash Folder 43 | Temporary Items 44 | .apdisk 45 | ======= 46 | *.user 47 | /PowerShellAsync/bin/Debug 48 | /PowerShellAsync/obj/Debug 49 | /UnitTests/bin/Debug 50 | /UnitTests/obj/Debug 51 | *.suo 52 | /packages 53 | /MigrationBackup 54 | 55 | /TestResults 56 | /PowerShellAsyncExample/packages 57 | /PowerShellAsyncExample/PowerShellAsyncExample/bin/Debug 58 | /PowerShellAsyncExample/PowerShellAsyncExample/obj/Debug 59 | 60 | # Visual Studio 2015/2017 cache/options directory 61 | .vs/ 62 | .vscode/ 63 | 64 | # Build results 65 | [Dd]ebug/ 66 | [Dd]ebugPublic/ 67 | [Rr]elease/ 68 | [Rr]eleases/ 69 | x64/ 70 | x86/ 71 | bld/ 72 | [Bb]in/ 73 | [Oo]bj/ 74 | [Ll]og/ 75 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 ttrider 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /PowerShellAsync.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30021.99 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerShellAsync", "PowerShellAsync\PowerShellAsync.csproj", "{07698A5B-BB2B-40BE-88A8-63486216E6EC}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "UnitTests\UnitTests.csproj", "{C0C70598-5BD7-4D71-944B-7CC8D2DFDA17}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{15E3F177-5F88-4053-8591-2B25CED3DE35}" 11 | ProjectSection(SolutionItems) = preProject 12 | LICENSE = LICENSE 13 | readme.md = readme.md 14 | EndProjectSection 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {07698A5B-BB2B-40BE-88A8-63486216E6EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {07698A5B-BB2B-40BE-88A8-63486216E6EC}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {07698A5B-BB2B-40BE-88A8-63486216E6EC}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {07698A5B-BB2B-40BE-88A8-63486216E6EC}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {C0C70598-5BD7-4D71-944B-7CC8D2DFDA17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {C0C70598-5BD7-4D71-944B-7CC8D2DFDA17}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {C0C70598-5BD7-4D71-944B-7CC8D2DFDA17}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {C0C70598-5BD7-4D71-944B-7CC8D2DFDA17}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {7D6544FA-4938-4572-BEAA-B9326E3E361F} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /PowerShellAsync/AsyncCmdlet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Management.Automation; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | //using JetBrains.Annotations; 7 | 8 | namespace TTRider.PowerShellAsync 9 | { 10 | /// 11 | /// Base class for async-enabled cmdlets 12 | /// 13 | public abstract class AsyncCmdlet : PSCmdlet 14 | { 15 | protected int BoundedCapacity { get; set; } 16 | 17 | protected AsyncCmdlet(int boundedCapacity = 50) 18 | { 19 | this.BoundedCapacity = Math.Max(1, boundedCapacity); 20 | } 21 | 22 | #region sealed overrides 23 | protected sealed override void BeginProcessing() 24 | { 25 | AsyncCmdletSynchronizationContext.Async(BeginProcessingAsync, BoundedCapacity); 26 | } 27 | 28 | protected sealed override void ProcessRecord() 29 | { 30 | AsyncCmdletSynchronizationContext.Async(ProcessRecordAsync, BoundedCapacity); 31 | } 32 | 33 | protected sealed override void EndProcessing() 34 | { 35 | AsyncCmdletSynchronizationContext.Async(EndProcessingAsync, BoundedCapacity); 36 | } 37 | 38 | protected sealed override void StopProcessing() 39 | { 40 | AsyncCmdletSynchronizationContext.Async(StopProcessingAsync, BoundedCapacity); 41 | } 42 | 43 | #endregion sealed overrides 44 | 45 | #region intercepted methods 46 | public new void WriteDebug(string text) 47 | { 48 | AsyncCmdletSynchronizationContext.PostItem(new MarshalItemAction(base.WriteDebug, text)); 49 | } 50 | 51 | public new void WriteError(ErrorRecord errorRecord) 52 | { 53 | AsyncCmdletSynchronizationContext.PostItem(new MarshalItemAction(base.WriteError, errorRecord)); 54 | } 55 | 56 | public new void WriteObject(object sendToPipeline) 57 | { 58 | AsyncCmdletSynchronizationContext.PostItem(new MarshalItemAction(base.WriteObject, sendToPipeline)); 59 | } 60 | 61 | public new void WriteObject(object sendToPipeline, bool enumerateCollection) 62 | { 63 | AsyncCmdletSynchronizationContext.PostItem(new MarshalItemAction(base.WriteObject, sendToPipeline, enumerateCollection)); 64 | } 65 | 66 | public new void WriteProgress(ProgressRecord progressRecord) 67 | { 68 | AsyncCmdletSynchronizationContext.PostItem(new MarshalItemAction(base.WriteProgress, progressRecord)); 69 | } 70 | 71 | public new void WriteVerbose(string text) 72 | { 73 | var workItem = new MarshalItemAction(base.WriteVerbose, text); 74 | AsyncCmdletSynchronizationContext.PostItem(workItem); 75 | } 76 | 77 | public new void WriteWarning(string text) 78 | { 79 | AsyncCmdletSynchronizationContext.PostItem(new MarshalItemAction(base.WriteWarning, text)); 80 | } 81 | 82 | public new void WriteCommandDetail(string text) 83 | { 84 | AsyncCmdletSynchronizationContext.PostItem(new MarshalItemAction(base.WriteCommandDetail, text)); 85 | } 86 | 87 | public new bool ShouldProcess(string target) 88 | { 89 | var workItem = new MarshalItemFunc(base.ShouldProcess, target); 90 | AsyncCmdletSynchronizationContext.PostItem(workItem); 91 | return workItem.WaitForResult(); 92 | } 93 | 94 | public new bool ShouldProcess(string target, string action) 95 | { 96 | var workItem = new MarshalItemFunc(base.ShouldProcess, target, action); 97 | AsyncCmdletSynchronizationContext.PostItem(workItem); 98 | return workItem.WaitForResult(); 99 | } 100 | 101 | public new bool ShouldProcess(string verboseDescription, string verboseWarning, string caption) 102 | { 103 | var workItem = new MarshalItemFunc(base.ShouldProcess, verboseDescription, 104 | verboseWarning, caption); 105 | AsyncCmdletSynchronizationContext.PostItem(workItem); 106 | return workItem.WaitForResult(); 107 | } 108 | 109 | public new bool ShouldProcess(string verboseDescription, string verboseWarning, string caption, 110 | out ShouldProcessReason shouldProcessReason) 111 | { 112 | var workItem = new MarshalItemFuncOut( 113 | base.ShouldProcess, verboseDescription, verboseWarning, caption); 114 | AsyncCmdletSynchronizationContext.PostItem(workItem); 115 | return workItem.WaitForResult(out shouldProcessReason); 116 | } 117 | 118 | public new bool ShouldContinue(string query, string caption) 119 | { 120 | var workItem = new MarshalItemFunc(base.ShouldContinue, query, caption); 121 | AsyncCmdletSynchronizationContext.PostItem(workItem); 122 | return workItem.WaitForResult(); 123 | } 124 | 125 | public new bool ShouldContinue(string query, string caption, ref bool yesToAll, ref bool noToAll) 126 | { 127 | var workItem = new MarshalItemFuncRef(base.ShouldContinue, query, caption, 128 | yesToAll, noToAll); 129 | AsyncCmdletSynchronizationContext.PostItem(workItem); 130 | return workItem.WaitForResult(ref yesToAll, ref noToAll); 131 | } 132 | 133 | public new bool TransactionAvailable() 134 | { 135 | var workItem = new MarshalItemFunc(base.TransactionAvailable); 136 | AsyncCmdletSynchronizationContext.PostItem(workItem); 137 | return workItem.WaitForResult(); 138 | } 139 | 140 | public new void ThrowTerminatingError(ErrorRecord errorRecord) 141 | { 142 | AsyncCmdletSynchronizationContext.PostItem(new MarshalItemAction(base.ThrowTerminatingError, errorRecord)); 143 | } 144 | 145 | #endregion 146 | 147 | #region async processing methods 148 | 149 | protected virtual Task BeginProcessingAsync() 150 | { 151 | return Task.FromResult(0); 152 | } 153 | 154 | 155 | protected virtual Task EndProcessingAsync() 156 | { 157 | return Task.FromResult(0); 158 | } 159 | 160 | 161 | protected virtual Task ProcessRecordAsync() 162 | { 163 | return Task.FromResult(0); 164 | } 165 | 166 | 167 | protected virtual Task StopProcessingAsync() 168 | { 169 | return Task.FromResult(0); 170 | } 171 | 172 | #endregion async processing methods 173 | 174 | private class AsyncCmdletSynchronizationContext : SynchronizationContext, IDisposable 175 | { 176 | private BlockingCollection workItems; 177 | private static AsyncCmdletSynchronizationContext currentAsyncCmdletContext; 178 | 179 | private AsyncCmdletSynchronizationContext(int boundedCapacity) 180 | { 181 | this.workItems = new BlockingCollection(boundedCapacity); 182 | } 183 | 184 | public static void Async(Func handler, int boundedCapacity) 185 | { 186 | var previousContext = SynchronizationContext.Current; 187 | 188 | try 189 | { 190 | using (var synchronizationContext = new AsyncCmdletSynchronizationContext(boundedCapacity)) 191 | { 192 | SetSynchronizationContext(synchronizationContext); 193 | currentAsyncCmdletContext = synchronizationContext; 194 | 195 | var task = handler(); 196 | if (task == null) 197 | { 198 | return; 199 | } 200 | 201 | var waitable = task.ContinueWith(t => synchronizationContext.Complete(), scheduler: TaskScheduler.Default); 202 | 203 | synchronizationContext.ProcessQueue(); 204 | 205 | waitable.GetAwaiter().GetResult(); 206 | } 207 | } 208 | finally 209 | { 210 | SetSynchronizationContext(previousContext); 211 | currentAsyncCmdletContext = previousContext as AsyncCmdletSynchronizationContext; 212 | } 213 | } 214 | 215 | internal static void PostItem(MarshalItem item) 216 | { 217 | currentAsyncCmdletContext.Post(item); 218 | } 219 | 220 | public void Dispose() 221 | { 222 | if (this.workItems != null) 223 | { 224 | this.workItems.Dispose(); 225 | this.workItems = null; 226 | } 227 | } 228 | 229 | private void EnsureNotDisposed() 230 | { 231 | if (this.workItems == null) 232 | { 233 | throw new ObjectDisposedException(nameof(AsyncCmdletSynchronizationContext)); 234 | } 235 | } 236 | 237 | private void Complete() 238 | { 239 | EnsureNotDisposed(); 240 | 241 | this.workItems.CompleteAdding(); 242 | } 243 | 244 | private void ProcessQueue() 245 | { 246 | MarshalItem workItem; 247 | while (this.workItems.TryTake(out workItem, Timeout.Infinite)) 248 | { 249 | workItem.Invoke(); 250 | } 251 | } 252 | 253 | public override void Post(SendOrPostCallback callback, object state) 254 | { 255 | if (callback == null) 256 | { 257 | throw new ArgumentNullException(nameof(callback)); 258 | } 259 | 260 | Post(new MarshalItemAction(s => callback(s), state)); 261 | } 262 | 263 | private void Post(MarshalItem item) 264 | { 265 | EnsureNotDisposed(); 266 | 267 | this.workItems.Add(item); 268 | } 269 | } 270 | 271 | #region items 272 | internal abstract class MarshalItem 273 | { 274 | internal abstract void Invoke(); 275 | } 276 | 277 | abstract class MarshalItemFuncBase : MarshalItem 278 | { 279 | private TRet retVal; 280 | private readonly Task retValTask; 281 | 282 | protected MarshalItemFuncBase() 283 | { 284 | this.retValTask = new Task(() => this.retVal); 285 | } 286 | 287 | internal sealed override void Invoke() 288 | { 289 | this.retVal = this.InvokeFunc(); 290 | this.retValTask.Start(); 291 | } 292 | 293 | internal TRet WaitForResult() 294 | { 295 | this.retValTask.Wait(); 296 | return this.retValTask.Result; 297 | } 298 | 299 | internal abstract TRet InvokeFunc(); 300 | } 301 | class MarshalItemAction : MarshalItem 302 | { 303 | private readonly Action action; 304 | private readonly T arg1; 305 | 306 | internal MarshalItemAction(Action action, T arg1) 307 | { 308 | this.action = action; 309 | this.arg1 = arg1; 310 | } 311 | 312 | internal override void Invoke() 313 | { 314 | this.action(this.arg1); 315 | } 316 | } 317 | class MarshalItemAction : MarshalItem 318 | { 319 | private readonly Action action; 320 | private readonly T1 arg1; 321 | private readonly T2 arg2; 322 | 323 | internal MarshalItemAction(Action action, T1 arg1, T2 arg2) 324 | { 325 | this.action = action; 326 | this.arg1 = arg1; 327 | this.arg2 = arg2; 328 | } 329 | 330 | internal override void Invoke() 331 | { 332 | this.action(this.arg1, this.arg2); 333 | } 334 | } 335 | class MarshalItemFunc : MarshalItemFuncBase 336 | { 337 | private readonly Func func; 338 | 339 | internal MarshalItemFunc(Func func) 340 | { 341 | this.func = func; 342 | } 343 | 344 | internal override TRet InvokeFunc() 345 | { 346 | return this.func(); 347 | } 348 | } 349 | class MarshalItemFunc : MarshalItemFuncBase 350 | { 351 | private readonly Func func; 352 | private readonly T1 arg1; 353 | 354 | internal MarshalItemFunc(Func func, T1 arg1) 355 | { 356 | this.func = func; 357 | this.arg1 = arg1; 358 | } 359 | 360 | internal override TRet InvokeFunc() 361 | { 362 | return this.func(this.arg1); 363 | } 364 | } 365 | class MarshalItemFunc : MarshalItemFuncBase 366 | { 367 | private readonly Func func; 368 | private readonly T1 arg1; 369 | private readonly T2 arg2; 370 | 371 | internal MarshalItemFunc(Func func, T1 arg1, T2 arg2) 372 | { 373 | this.func = func; 374 | this.arg1 = arg1; 375 | this.arg2 = arg2; 376 | } 377 | 378 | internal override TRet InvokeFunc() 379 | { 380 | return this.func(this.arg1, this.arg2); 381 | } 382 | } 383 | class MarshalItemFunc : MarshalItemFuncBase 384 | { 385 | private readonly Func func; 386 | private readonly T1 arg1; 387 | private readonly T2 arg2; 388 | private readonly T3 arg3; 389 | 390 | internal MarshalItemFunc(Func func, T1 arg1, T2 arg2, T3 arg3) 391 | { 392 | this.func = func; 393 | this.arg1 = arg1; 394 | this.arg2 = arg2; 395 | this.arg3 = arg3; 396 | } 397 | 398 | internal override TRet InvokeFunc() 399 | { 400 | return this.func(this.arg1, this.arg2, this.arg3); 401 | } 402 | } 403 | class MarshalItemFuncOut : MarshalItem 404 | { 405 | private readonly FuncOut func; 406 | private readonly T1 arg1; 407 | private readonly T2 arg2; 408 | private readonly T3 arg3; 409 | 410 | internal delegate TRet FuncOut(T1 t1, T2 t2, T3 t3, out TOut tout); 411 | 412 | private TRet retVal; 413 | private TOut outVal; 414 | private readonly Task retValTask; 415 | 416 | internal MarshalItemFuncOut(FuncOut func, T1 arg1, T2 arg2, T3 arg3) 417 | { 418 | this.func = func; 419 | this.arg1 = arg1; 420 | this.arg2 = arg2; 421 | this.arg3 = arg3; 422 | this.retValTask = new Task(() => this.retVal); 423 | } 424 | 425 | internal override void Invoke() 426 | { 427 | this.retVal = this.func(this.arg1, this.arg2, this.arg3, out this.outVal); 428 | this.retValTask.Start(); 429 | } 430 | 431 | internal TRet WaitForResult(out TOut val) 432 | { 433 | this.retValTask.Wait(); 434 | val = this.outVal; 435 | return this.retValTask.Result; 436 | } 437 | } 438 | class MarshalItemFuncRef : MarshalItem 439 | { 440 | internal delegate TRet FuncRef(T1 t1, T2 t2, ref TRef1 tref1, ref TRef2 tref2); 441 | 442 | private readonly Task retValTask; 443 | private readonly FuncRef func; 444 | private readonly T1 arg1; 445 | private readonly T2 arg2; 446 | private TRef1 arg3; 447 | private TRef2 arg4; 448 | private TRet retVal; 449 | 450 | internal MarshalItemFuncRef(FuncRef func, T1 arg1, T2 arg2, TRef1 arg3, TRef2 arg4) 451 | { 452 | this.func = func; 453 | this.arg1 = arg1; 454 | this.arg2 = arg2; 455 | this.arg3 = arg3; 456 | this.arg4 = arg4; 457 | this.retValTask = new Task(() => this.retVal); 458 | } 459 | 460 | internal override void Invoke() 461 | { 462 | this.retVal = this.func(this.arg1, this.arg2, ref this.arg3, ref this.arg4); 463 | this.retValTask.Start(); 464 | } 465 | 466 | // ReSharper disable RedundantAssignment 467 | internal TRet WaitForResult(ref TRef1 ref1, ref TRef2 ref2) 468 | { 469 | this.retValTask.Wait(); 470 | ref1 = this.arg3; 471 | ref2 = this.arg4; 472 | return this.retValTask.Result; 473 | } 474 | // ReSharper restore RedundantAssignment 475 | } 476 | #endregion items 477 | } 478 | } 479 | -------------------------------------------------------------------------------- /PowerShellAsync/PowerShellAsync.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | TTRider.PowerShellAsync 6 | 1.1.0.1 7 | TTRider, L.L.C. 8 | TTRider.PowerShellAsync 9 | PowerShellAsync 10 | 1.1.0.1 11 | TTRider, L.L.C. 12 | PowerShellAsync 13 | Base class for async-based cmdlets 14 | Copyright TTRider, L.L.C. 15 | Library 16 | 17 | 18 | 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /PowerShellAsync/TTRider.PowerShellAsync.dll.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | TTRider.PowerShellAsync.dll 5 | 1.1.0.1 6 | TTRider,tjrockefeller,pgrefviau,tig 7 | TTRider 8 | https://github.com/ttrider/powershellasync/blob/master/LICENSE 9 | https://github.com/ttrider/powershellasync 10 | false 11 | Base class for async-supported PowerShell cmdlets. 12 | 13 | 1.1.0.1 14 | * Fixed CI workflow 15 | 1.1.0.0 16 | * .NET Core 3.1 Support 17 | * Removed dependency on Jetbrains.Annoations 18 | 1.0.1.0 19 | * Bug fixes 20 | 1.0.0.0 21 | * Initial Release 22 | 23 | Copyright (C) 2014 - 2020 TTRider, L.L.C. 24 | PowerShell 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /PowerShellAsyncExample/PowerShellAsyncExample.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30723.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PowerShellAsyncExample", "PowerShellAsyncExample\PowerShellAsyncExample.csproj", "{D8BEDEB5-2000-40C7-BAF1-FA0FE90CDDB3}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {D8BEDEB5-2000-40C7-BAF1-FA0FE90CDDB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {D8BEDEB5-2000-40C7-BAF1-FA0FE90CDDB3}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {D8BEDEB5-2000-40C7-BAF1-FA0FE90CDDB3}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {D8BEDEB5-2000-40C7-BAF1-FA0FE90CDDB3}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /PowerShellAsyncExample/PowerShellAsyncExample/MultiSqlCmdlet.cs: -------------------------------------------------------------------------------- 1 | using System.Data.SqlClient; 2 | using System.Linq; 3 | using System.Management.Automation; 4 | using System.Threading.Tasks; 5 | using JetBrains.Annotations; 6 | using TTRider.PowerShellAsync; 7 | 8 | namespace PowerShellAsyncExample 9 | { 10 | [Cmdlet(VerbsLifecycle.Invoke, "MultiSql")] 11 | public class MultiSqlCmdlet : AsyncCmdlet 12 | { 13 | [NotNull, Parameter(Mandatory = true)] 14 | public string[] Server { get; set; } 15 | 16 | 17 | protected override Task ProcessRecordAsync() 18 | { 19 | return Task.WhenAll( 20 | this.Server.Select( 21 | server => 22 | this.ExecuteStatement(server, "select * from sys.objects"))); 23 | } 24 | 25 | [NotNull] 26 | async Task ExecuteStatement([NotNull] string server, [NotNull] string statement) 27 | { 28 | var connectionBuilding = new SqlConnectionStringBuilder 29 | { 30 | DataSource = server, 31 | IntegratedSecurity = true, 32 | AsynchronousProcessing = true 33 | }; 34 | 35 | var connection = new SqlConnection(connectionBuilding.ConnectionString); 36 | 37 | await connection.OpenAsync(); 38 | 39 | var cmd = connection.CreateCommand(); 40 | cmd.CommandText = statement; 41 | 42 | using (var reader = await cmd.ExecuteReaderAsync()) 43 | { 44 | if (await reader.ReadAsync()) 45 | { 46 | var names = new string[reader.FieldCount]; 47 | for (var i = 0; i < reader.FieldCount; i++) 48 | { 49 | names[i] = reader.GetName(i); 50 | } 51 | 52 | do 53 | { 54 | var item = new PSObject(); 55 | for (var i = 0; i < reader.FieldCount; i++) 56 | { 57 | var value = await reader.IsDBNullAsync(i) ? null : reader.GetValue(i); 58 | 59 | item.Properties.Add(new PSNoteProperty(names[i], value)); 60 | } 61 | 62 | this.WriteObject(item); 63 | 64 | } while (await reader.ReadAsync()); 65 | } 66 | } 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /PowerShellAsyncExample/PowerShellAsyncExample/PowerShellAsyncExample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {D8BEDEB5-2000-40C7-BAF1-FA0FE90CDDB3} 8 | Library 9 | Properties 10 | PowerShellAsyncExample 11 | PowerShellAsyncExample 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | ..\packages\ReSharper.Annotations.7.1.3.130415\lib\net\JetBrains.Annotations.dll 35 | 36 | 37 | 38 | 39 | True 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | ..\packages\TTRider.PowerShellAsync.dll.1.0.0.0\lib\net45\TTRider.PowerShellAsync.dll 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 65 | -------------------------------------------------------------------------------- /PowerShellAsyncExample/PowerShellAsyncExample/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("PowerShellAsyncExample")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("PowerShellAsyncExample")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("33a93a67-685e-47c8-8255-281a4393523d")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /PowerShellAsyncExample/PowerShellAsyncExample/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /UnitTests/AsyncCmdletTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.Management.Automation; 5 | using System.Management.Automation.Runspaces; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using Microsoft.VisualStudio.TestTools.UnitTesting; 9 | using TTRider.PowerShellAsync.UnitTests.Infrastructure; 10 | 11 | namespace TTRider.PowerShellAsync.UnitTests 12 | { 13 | [TestClass] 14 | public class TestPsBase 15 | { 16 | private static Runspace runspace; 17 | 18 | [ClassInitialize()] 19 | public static void Initialize(TestContext context) 20 | { 21 | runspace = RunspaceFactory.CreateRunspace(); 22 | runspace.Open(); 23 | ImportModule(); 24 | } 25 | 26 | [ClassCleanup()] 27 | public static void ClassCleanup() 28 | { 29 | runspace.Close(); 30 | } 31 | 32 | public static void ImportModule() 33 | { 34 | RunCommand(ps => 35 | { 36 | var path = new Uri(typeof(TestWriteObject).Assembly.CodeBase); 37 | ps.AddCommand("Import-Module").AddArgument(path.LocalPath); 38 | }); 39 | } 40 | 41 | public static List RunCommand(Action prepareAction, PsCommandContext context = null) 42 | { 43 | var ps = PowerShell.Create(); 44 | ps.Runspace = runspace; 45 | 46 | prepareAction(ps); 47 | 48 | var ret = new List(); 49 | 50 | var settings = new PSInvocationSettings 51 | { 52 | Host = new TestPsHost(context ?? new PsCommandContext()) 53 | }; 54 | 55 | foreach (var result in ps.Invoke(new Object[0], settings)) 56 | { 57 | Trace.WriteLine(result); 58 | ret.Add(result); 59 | } 60 | return ret; 61 | } 62 | 63 | [TestMethod] 64 | public void WriteObject() 65 | { 66 | var output = RunCommand(ps => ps.AddCommand("Test-TTRiderPSAWriteObject")); 67 | Assert.AreEqual("WriteObject00\r\nWriteObject01\r\nWriteObject02\r\nWriteObject03", 68 | string.Join("\r\n", output)); 69 | } 70 | 71 | [TestMethod] 72 | public void PropertyAccess() 73 | { 74 | var output = RunCommand(ps => ps.AddCommand("Test-TTRiderPSAPropertyAccess")); 75 | Assert.AreEqual(0, output.Count); 76 | } 77 | 78 | [TestMethod] 79 | public void SyncProcessing() 80 | { 81 | var output = RunCommand(ps => ps.AddCommand("Test-TTRiderPSASyncProcessing")); 82 | Assert.AreEqual("BeginProcessingAsync\r\nProcessRecordAsync\r\nEndProcessingAsync", 83 | string.Join("\r\n", output)); 84 | } 85 | 86 | [TestMethod] 87 | public void WriteAll() 88 | { 89 | var context = new PsCommandContext(); 90 | var output = RunCommand(ps => 91 | { 92 | ps.AddCommand("Test-TTRiderPSAWriteAll"); 93 | ps.AddParameter("Verbose"); 94 | ps.AddParameter("Debug"); 95 | }, context); 96 | 97 | Assert.AreEqual("WriteObject00\r\nWriteObject01\r\nWriteObject02\r\nWriteObject03", 98 | string.Join("\r\n", output)); 99 | 100 | Assert.AreEqual("WriteDebug", 101 | string.Join("\r\n", context.DebugLines)); 102 | 103 | 104 | Assert.AreEqual("WriteWarning", 105 | string.Join("\r\n", context.WarningLines)); 106 | 107 | Assert.AreEqual("WriteVerbose", 108 | string.Join("\r\n", context.VerboseLines)); 109 | 110 | Assert.AreEqual(1, context.ProgressRecords.Count); 111 | 112 | } 113 | 114 | [TestMethod] 115 | public void SynchronizationContext() 116 | { 117 | var context = new PsCommandContext(); 118 | var output = RunCommand(ps => ps.AddCommand("Test-TTRiderPSSynchronisationContext"), context); 119 | 120 | Assert.AreEqual(2, output.Count); 121 | 122 | var initialProcessId = output[0]; 123 | var finalProcessId = output[1]; 124 | 125 | Assert.AreEqual(initialProcessId.ToString(), finalProcessId.ToString()); 126 | } 127 | } 128 | 129 | 130 | 131 | [Cmdlet("Test", "TTRiderPSAWriteObject")] 132 | public class TestWriteObject : AsyncCmdlet 133 | { 134 | protected override Task ProcessRecordAsync() 135 | { 136 | return Task.Run(() => 137 | { 138 | this.WriteObject("WriteObject00"); 139 | this.WriteObject(new[] { "WriteObject01", "WriteObject02", "WriteObject03" }, true); 140 | }); 141 | } 142 | } 143 | 144 | 145 | [Cmdlet("Test", "TTRiderPSAPropertyAccess")] 146 | public class TestPropertyAccess : AsyncCmdlet 147 | { 148 | protected override Task ProcessRecordAsync() 149 | { 150 | return Task.Run(() => 151 | { 152 | var commandOrigin = this.CommandOrigin; 153 | var commandRuntime = this.CommandRuntime; 154 | var events = this.Events; 155 | ProviderInfo pi; 156 | var psp = this.GetResolvedProviderPathFromPSPath(@"c:\", out pi); 157 | var pathInfo = this.CurrentProviderLocation(pi.Name); 158 | var psp2 = this.GetUnresolvedProviderPathFromPSPath(@"c:\"); 159 | var varErr = this.GetVariableValue("$error"); 160 | var varErr2 = this.GetVariableValue("$error", "default"); 161 | var host = this.Host; 162 | var invokeCommand = this.InvokeCommand; 163 | var invokeProvider = this.InvokeProvider; 164 | var jobRepository = this.JobRepository; 165 | var myInvoke = this.MyInvocation; 166 | var parameterSetName = this.ParameterSetName; 167 | var sessionState = this.SessionState; 168 | var stopping = this.Stopping; 169 | var transactionAvailable = this.TransactionAvailable(); 170 | }); 171 | } 172 | } 173 | 174 | 175 | [Cmdlet("Test", "TTRiderPSASyncProcessing")] 176 | public class TestSyncProcessing : AsyncCmdlet 177 | { 178 | protected override Task BeginProcessingAsync() 179 | { 180 | this.WriteObject("BeginProcessingAsync"); 181 | return base.BeginProcessingAsync(); 182 | } 183 | 184 | protected override Task EndProcessingAsync() 185 | { 186 | this.WriteObject("EndProcessingAsync"); 187 | return base.EndProcessingAsync(); 188 | } 189 | 190 | protected override Task StopProcessingAsync() 191 | { 192 | this.WriteObject("StopProcessingAsync"); 193 | return base.StopProcessingAsync(); 194 | } 195 | 196 | protected override Task ProcessRecordAsync() 197 | { 198 | this.WriteObject("ProcessRecordAsync"); 199 | return base.ProcessRecordAsync(); 200 | } 201 | } 202 | 203 | 204 | 205 | 206 | [Cmdlet("Test", "TTRiderPSAWriteAll")] 207 | public class TestWriteAll : AsyncCmdlet 208 | { 209 | protected override Task ProcessRecordAsync() 210 | { 211 | return Task.Run(() => 212 | { 213 | this.WriteCommandDetail("WriteCommandDetail"); 214 | this.WriteDebug("WriteDebug"); 215 | this.WriteError(new ErrorRecord(new Exception(), "errorId", ErrorCategory.SyntaxError, "targetObject")); 216 | this.WriteObject("WriteObject00"); 217 | this.WriteObject(new[] { "WriteObject01", "WriteObject02", "WriteObject03" }, true); 218 | this.WriteProgress(new ProgressRecord(0, "activity", "statusDescription")); 219 | this.WriteVerbose("WriteVerbose"); 220 | this.WriteWarning("WriteWarning"); 221 | }); 222 | } 223 | } 224 | 225 | [Cmdlet("Test", "TTRiderPSSynchronisationContext")] 226 | public class TestSynchronisationContext : AsyncCmdlet 227 | { 228 | protected override async Task ProcessRecordAsync() 229 | { 230 | this.WriteObject(Thread.CurrentThread.ManagedThreadId); 231 | 232 | await Task.Delay(1); 233 | 234 | this.WriteObject(Thread.CurrentThread.ManagedThreadId); 235 | } 236 | } 237 | } -------------------------------------------------------------------------------- /UnitTests/Infrastructure/PsCommandContext.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Management.Automation; 3 | 4 | namespace TTRider.PowerShellAsync.UnitTests.Infrastructure 5 | { 6 | public class PsCommandContext 7 | { 8 | public PsCommandContext() 9 | { 10 | Lines = new List(); 11 | ErrorLines = new List(); 12 | DebugLines = new List(); 13 | VerboseLines = new List(); 14 | WarningLines = new List(); 15 | ProgressRecords = new List(); 16 | } 17 | 18 | public List Lines { get; private set; } 19 | public List ErrorLines { get; private set; } 20 | public List DebugLines { get; private set; } 21 | public List VerboseLines { get; private set; } 22 | public List WarningLines { get; private set; } 23 | public List ProgressRecords { get; private set; } 24 | } 25 | } -------------------------------------------------------------------------------- /UnitTests/Infrastructure/TestPsHost.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Globalization; 3 | using System.Management.Automation.Host; 4 | using UnitTests; 5 | 6 | namespace TTRider.PowerShellAsync.UnitTests.Infrastructure 7 | { 8 | class TestPsHost : PSHost 9 | { 10 | private readonly Guid instanceId = Guid.NewGuid(); 11 | private readonly TestPsHostUserInterface ui; 12 | 13 | public TestPsHost(PsCommandContext context) 14 | { 15 | ui = new TestPsHostUserInterface(context); 16 | } 17 | 18 | public override void SetShouldExit(int exitCode) 19 | { 20 | } 21 | 22 | public override void EnterNestedPrompt() 23 | { 24 | } 25 | 26 | public override void ExitNestedPrompt() 27 | { 28 | } 29 | 30 | public override void NotifyBeginApplication() 31 | { 32 | } 33 | 34 | public override void NotifyEndApplication() 35 | { 36 | } 37 | 38 | public override string Name 39 | { 40 | get { return "TestHost"; } 41 | } 42 | 43 | public override Version Version 44 | { 45 | get { return new Version(1, 0); } 46 | } 47 | 48 | public override Guid InstanceId 49 | { 50 | get { return instanceId; } 51 | } 52 | 53 | public override PSHostUserInterface UI 54 | { 55 | get { return this.ui; } 56 | } 57 | 58 | public override CultureInfo CurrentCulture 59 | { 60 | get { return CultureInfo.CurrentCulture; } 61 | } 62 | 63 | public override CultureInfo CurrentUICulture 64 | { 65 | get { return CultureInfo.CurrentUICulture; } 66 | } 67 | } 68 | } -------------------------------------------------------------------------------- /UnitTests/Infrastructure/TestPsHostRawUserInterface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Management.Automation.Host; 3 | 4 | namespace TTRider.PowerShellAsync.UnitTests.Infrastructure 5 | { 6 | class TestPsHostRawUserInterface : PSHostRawUserInterface 7 | { 8 | public override KeyInfo ReadKey(ReadKeyOptions options) 9 | { 10 | throw new NotImplementedException(); 11 | } 12 | 13 | public override void FlushInputBuffer() 14 | { 15 | throw new NotImplementedException(); 16 | } 17 | 18 | public override void SetBufferContents(Coordinates origin, BufferCell[,] contents) 19 | { 20 | throw new NotImplementedException(); 21 | } 22 | 23 | public override void SetBufferContents(Rectangle rectangle, BufferCell fill) 24 | { 25 | throw new NotImplementedException(); 26 | } 27 | 28 | public override BufferCell[,] GetBufferContents(Rectangle rectangle) 29 | { 30 | throw new NotImplementedException(); 31 | } 32 | 33 | public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill) 34 | { 35 | throw new NotImplementedException(); 36 | } 37 | 38 | public override ConsoleColor ForegroundColor { get; set; } 39 | public override ConsoleColor BackgroundColor { get; set; } 40 | public override Coordinates CursorPosition { get; set; } 41 | public override Coordinates WindowPosition { get; set; } 42 | public override int CursorSize { get; set; } 43 | public override Size BufferSize { get; set; } 44 | public override Size WindowSize { get; set; } 45 | 46 | public override Size MaxWindowSize 47 | { 48 | get { throw new NotImplementedException(); } 49 | } 50 | 51 | public override Size MaxPhysicalWindowSize 52 | { 53 | get { throw new NotImplementedException(); } 54 | } 55 | 56 | public override bool KeyAvailable 57 | { 58 | get { throw new NotImplementedException(); } 59 | } 60 | 61 | public override string WindowTitle { get; set; } 62 | } 63 | } -------------------------------------------------------------------------------- /UnitTests/Infrastructure/TestPsHostUserInterface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Management.Automation; 5 | using System.Management.Automation.Host; 6 | using System.Security; 7 | using TTRider.PowerShellAsync.UnitTests.Infrastructure; 8 | 9 | namespace UnitTests 10 | { 11 | class TestPsHostUserInterface : PSHostUserInterface 12 | { 13 | PsCommandContext host; 14 | public TestPsHostUserInterface(PsCommandContext host) 15 | { 16 | this.host = host; 17 | } 18 | 19 | public override string ReadLine() 20 | { 21 | return "SomeInput"; 22 | } 23 | 24 | public override SecureString ReadLineAsSecureString() 25 | { 26 | return new SecureString(); 27 | } 28 | 29 | public override void Write(string value) 30 | { 31 | this.host.Lines.Add(value); 32 | } 33 | 34 | public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) 35 | { 36 | this.host.Lines.Add(value); 37 | } 38 | 39 | public override void WriteLine(string value) 40 | { 41 | this.host.Lines.Add(value); 42 | } 43 | 44 | public override void WriteErrorLine(string value) 45 | { 46 | this.host.ErrorLines.Add(value); 47 | } 48 | 49 | public override void WriteDebugLine(string message) 50 | { 51 | this.host.DebugLines.Add(message); 52 | } 53 | 54 | public override void WriteProgress(long sourceId, ProgressRecord record) 55 | { 56 | this.host.ProgressRecords.Add(record); 57 | } 58 | 59 | public override void WriteVerboseLine(string message) 60 | { 61 | this.host.VerboseLines.Add(message); 62 | } 63 | 64 | public override void WriteWarningLine(string message) 65 | { 66 | this.host.WarningLines.Add(message); 67 | } 68 | 69 | public override Dictionary Prompt(string caption, string message, Collection descriptions) 70 | { 71 | throw new NotImplementedException(); 72 | } 73 | 74 | public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName) 75 | { 76 | throw new NotImplementedException(); 77 | } 78 | 79 | public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, 80 | PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) 81 | { 82 | throw new NotImplementedException(); 83 | } 84 | 85 | public override int PromptForChoice(string caption, string message, Collection choices, int defaultChoice) 86 | { 87 | return defaultChoice; 88 | } 89 | 90 | public override PSHostRawUserInterface RawUI 91 | { 92 | get { return new TestPsHostRawUserInterface(); } 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /UnitTests/UnitTests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | all 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ![.NET Core](https://github.com/ttrider/PowerShellAsync/workflows/.NET%20Core/badge.svg?branch=master) 2 | 3 | # PowerShellAsync 4 | base class for async-enabled PowerShell Cmdlets. 5 | 6 | When you build PowerShell Cmdlets, it is required that calls to WriteObject, WriteVerbose, WriteWarning, etc be originated from BeginProcessing/ProcessRecord/EndProcessing method on __the main thread__!. 7 | 8 | With a general move towards async code, it's become hard to use newer libraries inside the traditional PowerShell Cmdlets. Up until now :) 9 | 10 | With 3 easy steps you can take advantage of the async programming: 11 | 12 | 1. Install PowerShellAsync from nuget.org 13 | * ``PM> Install-Package PowerShellAsync`` 14 | 15 | 16 | 2. Replace base class of your Cmdlet from _PSCmdlet_ to _AsyncCmdlet_ 17 | * ``[Cmdlet("Test", "SomethingCool")] 18 | public class TestSomethingCool : AsyncCmdlet`` 19 | 20 | 21 | 3. Replace BeginProcessing/ProcessRecord/EndProcessing methods with their xxxAsync counterparts 22 | * ``protected override async Task ProcessRecordAsync()`` 23 | 24 | 25 | 4. Enjoy!!! 26 | 27 | 28 | ## Contributors 29 | 30 | * [Vladimir Yangurskiy](https://github.com/ttrider) 31 | * [pgrefviau](https://github.com/pgrefviau) 32 | * [tig](https://github.com/tig) 33 | 34 | # Installation 35 | 36 | To install PowerShellAsync, run the following command in the Package Manager Console 37 | 38 | ``` 39 | PM> Install-Package PowerShellAsync 40 | ``` 41 | 42 | # Examples 43 | 44 | please take a look at PowerShellAsyncExamples project, it contains an demo Cmdlet that can execute SQL statement on multiple servers takeing advantage of async API. 45 | 46 | # Contact 47 | 48 | 49 | [Vladimir (software@ttrider.com)](mailto:software@ttrider.com) 50 | 51 | 52 | # License 53 | 54 | [MIT](./LICENSE) 55 | --------------------------------------------------------------------------------