{"id":607,"date":"2019-02-24T14:06:21","date_gmt":"2019-02-24T12:06:21","guid":{"rendered":"http:\/\/www.flip-design.de\/?p=607"},"modified":"2019-02-24T14:06:21","modified_gmt":"2019-02-24T12:06:21","slug":"get-powershell-data-into-an-ssis-dataflow","status":"publish","type":"post","link":"https:\/\/www.flip-design.de\/?p=607","title":{"rendered":"Get PowerShell Data into an SSIS Dataflow"},"content":{"rendered":"\n<p>In my last project I need to capture some data from the Active Directory &#8211; especially user from the local security groups. So I decided to use a Power Shell Script. With PS it is very easy to capture all user from the groups and walk through nested groups. because in my interested are only user, not a group which is nested into another group&#8230;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-ADGroup -Filter {(name -eq  [groupname]) } | Get-ADGroupMember -Recursive | Where { $_.objectClass -eq \"user\" } | Get-ADUser -properties * | Where-Object { $_.DistinguishedName -notlike '*some comment*' }| select SamAccountName  -unique<\/code><\/pre>\n\n\n\n<p>The script above select the unique usernaqmes from a given group and excludes user, who have an special comment. The script iterates though any nested group and returns the username. Now I need the list not in PS, I need it into SSIS to transfer the data into a database.<\/p>\n\n\n\n<p>Next I created an SSIS Dataflow with a script task, which is used as a datasource<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"361\" height=\"389\" src=\"http:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_12-56-13.png\" alt=\"\" class=\"wp-image-609\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_12-56-13.png 361w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_12-56-13-278x300.png 278w\" sizes=\"(max-width: 361px) 100vw, 361px\" \/><figcaption>The dataflow &#8230;<\/figcaption><\/figure>\n\n\n\n<p>Next I created a variable who stores the groupname who I want to capture and then I configured the source &#8211; I used C# as my preferred programming language:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"887\" height=\"713\" src=\"http:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_12-57-41.png\" alt=\"\" class=\"wp-image-610\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_12-57-41.png 887w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_12-57-41-300x241.png 300w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_12-57-41-768x617.png 768w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_12-57-41-373x300.png 373w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><figcaption>The variable gruppenname stores the groupname &#8211; the screens are from a german project :-)<\/figcaption><\/figure>\n\n\n\n<p>Next I need the the C# script with my PS snippet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#region Namespaces\nusing System;\nusing System.Data;\nusing Microsoft.SqlServer.Dts.Pipeline.Wrapper;\nusing Microsoft.SqlServer.Dts.Runtime.Wrapper;\n#endregion\n\n#region Custom Namespaces\nusing System.Management.Automation;\nusing System.Management.Automation.Runspaces;\n#endregion\n\nusing System.IO;\nusing System.Collections.ObjectModel;\nusing System.Text;\n\n [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]\npublic class ScriptMain : UserComponent\n{\n    \n    private StreamReader sr = null;\n    Collection&lt;PSObject> results;\n\n    public override void PreExecute()\n    {\n        base.PreExecute();\n        string groupname = this.Variables.Gruppenname;\n        \/\/string PowerShellScript = \"Get-ADGroupMember -Identity \\\"\" + groupname + \"\\\" -Recursive | Select samAccountName\";\n        string PowerShellScript = \"Get-ADGroup -Filter {(name -eq  \\\"\" + groupname + \"\\\") } | Get-ADGroupMember -Recursive | Where { $_.objectClass -eq \\\"user\\\" } | Get-ADUser -properties * | Where-Object { $_.DistinguishedName -notlike '*any comment*' }| select SamAccountName  -unique\";\n        Runspace runspace = RunspaceFactory.CreateRunspace();\n        runspace.Open();\n        Pipeline pipeline = runspace.CreatePipeline();\n        pipeline.Commands.AddScript(PowerShellScript);\n        results = pipeline.Invoke();\n        runspace.Close();\n\n    }\n\n\n\n    public override void CreateNewOutputRows()\n    {\n        StringBuilder stringBuilder = new StringBuilder();\n        foreach (PSObject obj in results)\n        {\n            Ausgabe0Buffer.AddRow();\n            Ausgabe0Buffer.samAccountName = obj.ToString();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"873\" height=\"704\" src=\"http:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_13-02-49.png\" alt=\"\" class=\"wp-image-611\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_13-02-49.png 873w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_13-02-49-300x242.png 300w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_13-02-49-768x619.png 768w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2019\/02\/24-02-_2019_13-02-49-372x300.png 372w\" sizes=\"(max-width: 873px) 100vw, 873px\" \/><figcaption>The samaccountname in the output columns stores the username from the AD<\/figcaption><\/figure>\n\n\n\n<p>Thats it, now you have the PS output into an dataflow &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last project I need to capture some data from the Active Directory &#8211; especially user from the local security groups. So I decided to use a Power Shell Script. With PS it is very easy to capture all &hellip; <a href=\"https:\/\/www.flip-design.de\/?p=607\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/607"}],"collection":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=607"}],"version-history":[{"count":2,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/607\/revisions"}],"predecessor-version":[{"id":612,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/607\/revisions\/612"}],"wp:attachment":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}