{"id":16243241,"url":"https://github.com/svrooij/powershell.dependencyinjection","last_synced_at":"2025-03-19T17:32:07.377Z","repository":{"id":216639578,"uuid":"741576275","full_name":"svrooij/PowerShell.DependencyInjection","owner":"svrooij","description":"Asynchronous PsCmdLet with Dependency Injection and ILogger","archived":false,"fork":false,"pushed_at":"2024-12-18T22:02:35.000Z","size":127,"stargazers_count":7,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-26T23:04:02.765Z","etag":null,"topics":["asynchronous","cmdlet","dependency-injection","powershell"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/Svrooij.PowerShell.DependencyInjection","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/svrooij.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-10T17:20:20.000Z","updated_at":"2024-12-18T21:53:28.000Z","dependencies_parsed_at":"2024-01-11T14:14:34.989Z","dependency_job_id":"d296c992-a81f-4c83-b1f3-18bfd49388b0","html_url":"https://github.com/svrooij/PowerShell.DependencyInjection","commit_stats":null,"previous_names":["svrooij/powershell.dependencyinjection"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svrooij%2FPowerShell.DependencyInjection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svrooij%2FPowerShell.DependencyInjection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svrooij%2FPowerShell.DependencyInjection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svrooij%2FPowerShell.DependencyInjection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/svrooij","download_url":"https://codeload.github.com/svrooij/PowerShell.DependencyInjection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244010926,"owners_count":20383333,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asynchronous","cmdlet","dependency-injection","powershell"],"created_at":"2024-10-10T14:14:16.475Z","updated_at":"2025-03-19T17:32:07.054Z","avatar_url":"https://github.com/svrooij.png","language":"C#","readme":"# Svrooij.PowerShell.DependencyInjection\n\nA library that allows you to use dependency injection in binary PowerShell modules.\n\n- Dependency injection (based on Microsoft.Extensions.DependencyInjection)\n- Using `ILogger` throughout your PowerShell Module.\n- Run any asynchronous code in your commandlets.\n\nI choose to only support a Task, I guess that all code that you're executing using this package will by async anyway.\n\n## Create a new PowerShell module\n\n```shell\n# Install the correct template (only once)\ndotnet new install Microsoft.PowerShell.Standard.Module.Template\n\n# Create a new directory and enter it\nmkdir MyNewModule\ncd MyNewModule\n\n# Create a new PowerShell module\ndotnet new psmodule\n```\n\n## Add Package\n\nAdd the pacakge `dotnet add package Svrooij.PowerShell.DependencyInjection`\n\n## Edit project file\n\nAdd the `CopyLocalLockFileAssemblies` to your project file, this will make sure that the dependencies are copied to the output folder.\n\n```xml\n  \u003cPropertyGroup\u003e\n    \u003cTargetFramework\u003enetstandard2.0\u003c/TargetFramework\u003e\n    \u003cAssemblyName\u003eSvrooij.PowerShell.DependencyInjection.SamplePs5\u003c/AssemblyName\u003e\n    \u003cCopyLocalLockFileAssemblies\u003etrue\u003c/CopyLocalLockFileAssemblies\u003e\n  \u003c/PropertyGroup\u003e\n```\n\nAnd change the version of `PowerShellStandard.Library` to `5.1.1`.\n\n```xml\n  \u003cItemGroup\u003e\n    \u003cPackageReference Include=\"PowerShellStandard.Library\" Version=\"5.1.1\" /\u003e\n    \u003cPackageReference Include=\"Svrooij.PowerShell.DependencyInjection\" Version=\"1.0.1\" /\u003e\n  \u003c/ItemGroup\u003e\n```\n\n## Create a Startup class\n\n```csharp\nusing Microsoft.Extensions.DependencyInjection;\nusing Svrooij.PowerShell.DependencyInjection;\n\npublic class Startup : PsStartup\n    {\n        // You need to override this method.\n        public override void ConfigureServices(IServiceCollection services)\n        {\n            services.AddTransient\u003cITestService, TestService\u003e();\n        }\n    }\n```\n\n## Create a CmdLet\n\n1. Instead of inheriting from `PsCmdLet`, you need to inherit `DependencyCmdlet\u003cYourStartupClass\u003e`.\n2. And then you put the `[ServiceDependency]` attribute above every private (or internal), field or property you want loaded from dependency injection.\n3. Override the `Task ProcessRecordAsync(CancellationToken cancellationToken)` method and call all the async stuff you want.\n\n```csharp\n    [Cmdlet(VerbsDiagnostic.Test, \"SampleCmdlet\")]\n    [OutputType(typeof(FavoriteStuff))]\n    public class TestSampleCmdletCommand : DependencyCmdlet\u003cYourStartupClass\u003e\n    {\n        [Parameter(\n            Mandatory = true,\n            Position = 0,\n            ValueFromPipeline = true,\n            ValueFromPipelineByPropertyName = true)]\n        public int FavoriteNumber { get; set; }\n\n        [Parameter(\n            Position = 1,\n            ValueFromPipelineByPropertyName = true)]\n        [ValidateSet(\"Cat\", \"Dog\", \"Horse\")]\n        public string FavoritePet { get; set; } = \"Dog\";\n\n        // Give your dependencies the ServiceDependency attribute\n        [ServiceDependency]\n        private ITestService TestService { get; set; }\n\n        // Logging using Microsoft.Extensions.Logging is supported (and configured automatically)\n        // You can alse use the regular WriteVerbose(), WriteDebug(), WriteInformation(), WriteWarning() and WriteError() methods\n        [ServiceDependency]\n        private ILogger\u003cTestSampleCmdletCommand\u003e _logger;\n\n        // This method will be called automatically by DependencyCmdlet which is called by ProcessRecord()\n        public override async Task ProcessRecordAsync(CancellationToken cancellationToken)\n        {\n            _logger.LogInformation(\"Starting ProcessRecordAsync()\");\n\n            await TestService.DoSomethingAsync(cancellationToken);\n\n            WriteObject(new FavoriteStuff\n            {\n                FavoriteNumber = this.FavoriteNumber,\n                FavoritePet = this.FavoritePet\n            });\n        }\n    }\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvrooij%2Fpowershell.dependencyinjection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsvrooij%2Fpowershell.dependencyinjection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvrooij%2Fpowershell.dependencyinjection/lists"}