Execute Powershell Script from Script Task

  • Phil Parkin

    SSC Guru

    Points: 244426

    Has anyone managed to execute a PoSh script from within a Script Task?

    When I started looking at this, it seemed like it would be straightforward enough and the process is described here.

    But ... the DLL which that article refers to (and which appears to be necessary to execute PoSh from C#) is called System.Management.Automation.dll and, as far as I can see, it is not available by default in the .NET framework. In fact, I could not find it anywhere on my PC.

    As I need to get this done, I am thinking that I will have to change my plans a little to use an Execute Process task to achieve what I want. But I would still like to know whether others have managed this and whether they did so without having to reference an external (and elusive) DLL?

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.
    See https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help/ for details of how to post T-SQL code-related questions.

  • Thom A

    SSC Guru

    Points: 98614

    If it helps, I've found the package on NuGet: https://www.nuget.org/packages/System.Management.Automation/

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.

  • Phil Parkin

    SSC Guru

    Points: 244426

    Thom A wrote:

    If it helps, I've found the package on NuGet: https://www.nuget.org/packages/System.Management.Automation/%5B/quote%5D

    Thanks, Thom. I actually tried that already. It seems that this module works with .NET Core only.

    2020-05-20_10-21-27

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.
    See https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help/ for details of how to post T-SQL code-related questions.

  • Thom A

    SSC Guru

    Points: 98614

    Was worth a go. I've been struggling to install it, as the office's firewall is a real pain; it has to scan every file you download (in full) before you download it and it has no idea what to do with Nuget packages, so it basically refuses to let you download them.

    Guessing that there is an older non .Net Core version, as that is version 7.0.1, but really struggling to find anything prior to 6.0.4 (which is still .Net Core)

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.

  • frederico_fonseca

    SSChampion

    Points: 14632

    I think I have that working in one of my packages but through a start process e.g. new process kicked off which starts a new powershell instance.

    if that works for you I can get the code out

  • Phil Parkin

    SSC Guru

    Points: 244426

    Thanks to you both for your efforts.

    I now have this working via an Execute Process task (though I still have to do some serious parameterisation to make it production-ready).

    So I'm happy to let it go. Let's hope that a version of that library is added to the .NET framework at some point, because it would be useful to have the ability to do this, without having to faff around so much.

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.
    See https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help/ for details of how to post T-SQL code-related questions.

  • frederico_fonseca

    SSChampion

    Points: 14632

    I may be wrong - but installing POsh 5.1 should contain the automation namespace - and this will not be .net core

    https://www.microsoft.com/en-us/download/details.aspx?id=54616

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply