{"id":19294996,"url":"https://github.com/strawbrryflurry/psyringe","last_synced_at":"2025-04-22T08:30:40.707Z","repository":{"id":143797745,"uuid":"463313442","full_name":"StrawbrryFlurry/psyringe","owner":"StrawbrryFlurry","description":"💉 [WIP] Bringing Dependency Injection to your PowerShell scripts","archived":false,"fork":false,"pushed_at":"2022-06-26T20:00:33.000Z","size":356,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-17T05:02:05.670Z","etag":null,"topics":["dependency-injection","powershell"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StrawbrryFlurry.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-02-24T21:55:10.000Z","updated_at":"2024-03-28T01:31:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"e11264b0-b0ee-43be-baf9-1cdc4146a231","html_url":"https://github.com/StrawbrryFlurry/psyringe","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StrawbrryFlurry%2Fpsyringe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StrawbrryFlurry%2Fpsyringe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StrawbrryFlurry%2Fpsyringe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StrawbrryFlurry%2Fpsyringe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StrawbrryFlurry","download_url":"https://codeload.github.com/StrawbrryFlurry/psyringe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250205989,"owners_count":21392161,"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":["dependency-injection","powershell"],"created_at":"2024-11-09T22:41:00.600Z","updated_at":"2025-04-22T08:30:40.700Z","avatar_url":"https://github.com/StrawbrryFlurry.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 💉 PSyringe\n\nPSyringe is a pre-processor, management tool and \"runtime\" for PowerShell scripts.\n\n## Warning!!\nThis framework, by design, enables **Arbitrary (Remote) Code Execution** if you expose it to an external layer! Please make sure you take the necessary precautions to protect your system. Only allow authenticated users restricted access to manage and call scripts loaded by PSyringe.\n\n# Goals\n\nPsyringe allows you to write less repetitive, secure PowerShell scripts by passing these concerns to the runtime.\n\n## Stop putting clear text secrets in your scripts\n```powershell\n[InjectCredential(\"SomeApiKey\")][SecureString]$RemoteApiKey;\n[InjectCredential(\"SomeCredential\")][PsCredential]$UserCredential;\n```\n\n## Enable more dynamic logging\nConfigure dynamic logging to any target through the framework and re-use it across all your scripts.\n```powershell\n[Inject([IPsLogger])][IPsLogger]$Logger;\n$Logger.Info(\"Hi!\");\n[LogExpression(Type = \"Info\")]$SomeVariable = \"Hi!\";\n# Logs: [21:05-15.03.2022] Value \"Hi!\" was assigned to variable \"SomeVariable\"\n\nfunction SomeFunction {\n  [LogInvocation(WithParams = $false)]\n  param([string]$a)\n}\n# Logs: [21:05-15.03.2022] Function \"SomeFunction\" was invoked.\n# WithParams: [21:05-15.03.2022] Function \"SomeFunction\" was invoked with parameters: [$a = \"Foo\"];\n```\n\n## Hook into your script's lifetime\n```powershell\nfunction HandleError {\n  [OnError()]\n  param([Exception]$Error)\n  # HandleError\n}\n\nfunction OnLoaded {\n  [OnLoaded()]\n  param()\n  # Do whatever when script is ready to be run\n}\n\nfunction BeforeUnload {\n  [BeforeUnload()]\n  param()\n  # Is called after the script was run. Do any cleanup work here\n}\n```\n\n## Inject any dependencies directly into your script\nLike we've seen before with in the logging example, you are able to inject any kind of dependency from the runtime into your script.\n```powershell\n[Inject([IPsLogger])][IPsLogger]$Logger;\n[Inject(Target = \"SomeCustomProvider\")][string]$SomeProvider;\n```\n\n## Make connecting to databases easier\n```powershell\n[InjectDatabase](\"KnownDatabase\")[DbContext]$DbConnection;\n[InjectDatabase(ConnectionString = \"...\")][DbContext]$DbConnection;\n```\n\n## Invoke your scripts with dynamic parameters\n```powershell\nfunction StartFunction {\n  [StartupFunction()]\n  param(\n    [InjectParameter(\"SomeParameter\")]\n    [string]$SomeParameter,\n    [InjectParameter(\"SomeOtherParameter\")]\n    [int]$SomeOtherParameter = 10 # Default if the parameter is not provided\n  )\n}\n```\n\n## Inject providers directly into your functions as defaults\nInjection sites, like the startup function, allow us to inject dynamic dependencies to functions as well as calling them like we're used to. We can also specify a scope in which the runtime will look for the dependency.\n```powershell\nfunction Get-Function {\n  [InjectionSite(Scope = \"Global\")]\n  param(\n    [Inject(\"SomeProvider\")]\n    [string]$SomeProvider\n    [string]$SomeParameter\n  )\n}\n\nGet-Function -SomeParameter \"Foo\" -SomeProvider \"Bar\" # Works\nGet-Function -SomeParameter \"Foo\" # Uses the default provided by the framework.\n```\n\n## ... And there is much more to come\n\n# Usage\nExample usage. The API is likely subject to change.\n```cs\nusing PSyringe.Core;\nusing PSyringe.Core.DI;\nusing PSyringe.Language.Parsing;\n\nvar parser = new ScriptParser();\nvar loader = new ScriptLoader();\nvar repository = new ScriptRepository();\n\nvar sm = new ScriptManager(parser, loader, repository);\nvar script = @\"\n  function Startup {\n    [StartupFunction()]\n    param(\n      [Inject([ILogger])]\n      [ILogger]$Logger\n      [InjectParameter(\"\"Foo\"\")]\n      [string]$Foo\n    )\n    $Logger.Info($Foo);\n  }\n\"\n\nvar scriptId = sm.Load(script);\nIInvokableScript scriptRef = sm.GetScriptById(scriptId);\nIScriptContext context = new ScriptContext {\n  Params = {\n    Foo = \"Foo\"\n  }\n};\n\nvar scriptInvocationResult = await scriptRef.Invoke(context);\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrawbrryflurry%2Fpsyringe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstrawbrryflurry%2Fpsyringe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrawbrryflurry%2Fpsyringe/lists"}