{"id":514,"date":"2019-01-30T22:38:20","date_gmt":"2019-01-31T04:38:20","guid":{"rendered":"http:\/\/zewwy.ca\/?p=514"},"modified":"2019-02-01T20:41:20","modified_gmt":"2019-02-02T02:41:20","slug":"working-on-powershell-scripts-ise-w-github","status":"publish","type":"post","link":"https:\/\/zewwy.ca\/index.php\/2019\/01\/30\/working-on-powershell-scripts-ise-w-github\/","title":{"rendered":"Working on PowerShell scripts (ISE) w\/ GitHub"},"content":{"rendered":"<h1 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"GitHub\"><\/span><strong>GitHub<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>So as you all probably know GitHub has been acquired by Microsoft. I had initially groaned at this acquisition as  usually a lot of things Microsoft has done lately has really bothered me (locking down APIs to O365 and not providing them to on Prem, for example) but then they also have done some good moves&#8230; .Net Core 2.0 and all the open source incentives are a nice change of pace.<\/p>\n<p>And to top that with some sugar how about some private Repositories for free members! Yeah That&#8217;s right, now that this is an option I&#8217;m going to use GitHub more. Now I&#8217;ve played with it before, however this time I wanted to write this up for my own memories. Hopefully it helps someone out there too.<\/p>\n<p>Let&#8217;s have some fun saving our PowerShell scripts on GitHub!<\/p>\n<h1 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"PowerShell_ISE_and_GIT\"><\/span><strong>PowerShell ISE and GIT<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h1 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Dependencies\"><\/span>Dependencies<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>So for this demo you&#8217;ll need:<\/p>\n<p>1)  A GitHub Account (Free)<br \/>\n2) PowerShell ISE (Free with Windows)<br \/>\n3) <a href=\"https:\/\/git-scm.com\/download\/win\">Git for Windows<\/a><\/p>\n<p>First, install and configure Git for Windows. Mike previously covered this topic in <a href=\"http:\/\/mikefrobbins.com\/2016\/01\/21\/getting-started-with-the-git-version-control-system\/\" target=\"_blank\" rel=\"noopener\">another blog article<\/a>. In this scenario, I ran the Git installer elevated so I could install it in the program files folder and I took the option to add the path for Git to the system environment variable path:<\/p>\n<p><a href=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2016\/02\/posh-git0a.png\" rel=\"attachment wp-att-13277\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-13277 size-full\" src=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2016\/02\/posh-git0a.png\" alt=\"posh-git0a\" width=\"499\" height=\"387\" \/><\/a><\/p>\n<p>Make sure that you\u2019ve configured Git as the user who is running PowerShell (I ran these commands from within my elevated PowerShell session):<\/p>\n<div id=\"crayon-5c51d93bdf3ea660146502\" class=\"crayon-syntax crayon-theme-powershell-ise crayon-font-droid-sans-mono crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\">\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5c51d93bdf3ea660146502-1\">1<\/div>\n<div class=\"crayon-num crayon-striped-num\" data-line=\"crayon-5c51d93bdf3ea660146502-2\">2<\/div>\n<div class=\"crayon-num\" data-line=\"crayon-5c51d93bdf3ea660146502-3\">3<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5c51d93bdf3ea660146502-1\" class=\"crayon-line\"><span class=\"crayon-e\">git <\/span><span class=\"crayon-i\">config<\/span> <span class=\"crayon-o\">&#8212;<\/span><span class=\"crayon-e\">global <\/span><span class=\"crayon-i\">user<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-i\">name<\/span> <span class=\"crayon-s\">&#8216;your name&#8217;<\/span><\/div>\n<div id=\"crayon-5c51d93bdf3ea660146502-2\" class=\"crayon-line crayon-striped-line\"><span class=\"crayon-e\">git <\/span><span class=\"crayon-i\">config<\/span> <span class=\"crayon-o\">&#8212;<\/span><span class=\"crayon-e\">global <\/span><span class=\"crayon-i\">user<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-i\">email<\/span> <span class=\"crayon-s\">&#8216;your email address&#8217;<\/span><\/div>\n<div id=\"crayon-5c51d93bdf3ea660146502-3\" class=\"crayon-line\"><span class=\"crayon-e\">git <\/span><span class=\"crayon-i\">config<\/span> <span class=\"crayon-o\">&#8212;<\/span><span class=\"crayon-e\">global <\/span><span class=\"crayon-i\">push<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">default <\/span><span class=\"crayon-i\">simple<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>4) Install the Posh-Git PowerShell module from the PowerShell Gallery:<\/p>\n<div id=\"crayon-5c51d93bdf3fa655903076\" class=\"crayon-syntax crayon-theme-powershell-ise crayon-font-droid-sans-mono crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\">\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5c51d93bdf3fa655903076-1\">1<\/div>\n<div class=\"crayon-num crayon-striped-num\" data-line=\"crayon-5c51d93bdf3fa655903076-2\">2<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5c51d93bdf3fa655903076-1\" class=\"crayon-line\"><span class=\"crayon-r \">Install-Module<\/span> <span class=\"crayon-cn\">-Name<\/span> <span class=\"crayon-i\">posh<\/span><span class=\"crayon-cn\">-git<\/span> <span class=\"crayon-cn\">-Force<\/span><\/div>\n<div id=\"crayon-5c51d93bdf3fa655903076-2\" class=\"crayon-line crayon-striped-line\"><span class=\"crayon-r \">Get-Module<\/span> <span class=\"crayon-cn\">-Name<\/span> <span class=\"crayon-i\">posh<\/span><span class=\"crayon-cn\">-git<\/span> <span class=\"crayon-cn\">-ListAvailable<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a href=\"https:\/\/mikefrobbins.com\/wp-content\/uploads\/2016\/02\/posh-git1a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/mikefrobbins.com\/wp-content\/uploads\/2016\/02\/posh-git1a.png\" alt=\"\" width=\"859\" height=\"191\" \/><\/a><\/p>\n<\/div>\n<\/div>\n<h1 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"The_Fun_Stuff\"><\/span>The Fun Stuff<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>So I originally follow <a href=\"https:\/\/mikefrobbins.com\/2016\/02\/09\/configuring-the-powershell-ise-for-use-with-git-and-github\/\">this guys blog post<\/a> on how to accomplish this.<\/p>\n<p>Now I had already installed git for windows so I was set there.<\/p>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"SharePoint_Profiles\"><\/span>SharePoint Profiles<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>I liked the part where he had altered his console display depending on where he was located to not ensue confusion, however I wasn&#8217;t exactly sure what he meant by <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/components\/ise\/how-to-use-profiles-in-windows-powershell-ise?view=powershell-6\">Profiles<\/a> a lil searching and education session later I was able to verify my profile path:<\/p>\n<pre>$profile<\/pre>\n<p><a href=\"https:\/\/i.imgur.com\/OYdmbDm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/OYdmbDm.png\" alt=\"\" width=\"602\" height=\"157\" \/><\/a><\/p>\n<p>Then simply edit that Microsoft.PowerShell_profile.ps1 with Mikes script:<\/p>\n<pre>Set-Location -Path $env:SystemDrive\\\r\nClear-Host\r\n$Error.Clear()\r\nImport-Module -Name posh-git -ErrorAction SilentlyContinue\r\nif (-not($Error[0])) {\r\n    $DefaultTitle = $Host.UI.RawUI.WindowTitle\r\n    $GitPromptSettings.BeforeText = '('\r\n    $GitPromptSettings.BeforeForegroundColor = [ConsoleColor]::Cyan\r\n    $GitPromptSettings.AfterText = ')'\r\n    $GitPromptSettings.AfterForegroundColor = [ConsoleColor]::Cyan\r\n    function prompt {\r\n        if (-not(Get-GitDirectory)) {\r\n            $Host.UI.RawUI.WindowTitle = $DefaultTitle\r\n            \"PS $($executionContext.SessionState.Path.CurrentLocation)$('&gt;' * ($nestedPromptLevel + 1)) \"\r\n        }\r\n        else {\r\n            $realLASTEXITCODE = $LASTEXITCODE\r\n            Write-Host 'PS ' -ForegroundColor Green -NoNewline\r\n            Write-Host \"$($executionContext.SessionState.Path.CurrentLocation) \" -ForegroundColor Yellow -NoNewline\r\n            Write-VcsStatus\r\n            $LASTEXITCODE = $realLASTEXITCODE\r\n            return \"`n$('$' * ($nestedPromptLevel + 1)) \"\r\n        }\r\n    }\r\n}\r\nelse {\r\n    Write-Warning -Message 'Unable to load the Posh-Git PowerShell Module'\r\n}<\/pre>\n<p>Now that we&#8217;ll have the same special console to avoid confusion let&#8217;s link a directory!<\/p>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Linking_GitHub_Repo_to_Your_local_Directory\"><\/span>Linking GitHub Repo to Your local Directory<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Then I cloned my new private Repo:<\/p>\n<pre>git clone https:\/\/github.com\/Zewwy\/Remove-SPFeature Remove-SPFeature -q<\/pre>\n<p><a href=\"https:\/\/i.imgur.com\/1jTba4I.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/1jTba4I.png\" alt=\"\" width=\"771\" height=\"378\" \/><\/a><\/p>\n<p>That felt awesome&#8230;<\/p>\n<p><a href=\"https:\/\/i.imgur.com\/v0vqbrY.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/v0vqbrY.png\" alt=\"\" width=\"472\" height=\"172\" \/><\/a><\/p>\n<p>Nice, nice&#8230;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Opening_scripts_from_the_ISE\"><\/span>Opening scripts from the ISE<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Alright. Well now that we have a repo, and are in it, how do I open a file in the very ISE we are running to edit them? Now Mike didn&#8217;t exactly cover this, cause I suppose to him this was already common knowledge&#8230; well not to me haha so it&#8217;s actually pretty <a href=\"https:\/\/stackoverflow.com\/questions\/1338969\/how-can-i-open-a-ps1-script-in-powershell-ise-from-the-command-line\">simple once you know how<\/a>.<\/p>\n<pre>psEdit .\\Remove-SPFeature.ps1<\/pre>\n<p><a href=\"https:\/\/i.imgur.com\/2cCzO7p.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/2cCzO7p.png\" alt=\"\" width=\"1705\" height=\"686\" \/><\/a><\/p>\n<p>Woah! Epic, it can be bothersome when dealing with length scripts, so ensure you utilize regions (w\/ endregions) to allow for quick named areas to access, as you can use this command in ISE to collapse all regions once a script is loaded.<\/p>\n<pre>$psISE.CurrentFile.Editor.ToggleOutliningExpansion()<\/pre>\n<p>lets start making some changes *changes made*<\/p>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Committing_and_Pushing\"><\/span>Committing and Pushing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Get your mind out of the gutter!<\/p>\n<p>Now I had originally did a git push, and instantly got everything is up-to-date alert, so awesome, I did not have to fight through his whole schpeal about auth (I got a prompt the very first time I attempted to clone my repo, requesting me to login into my GitHub Account). So my tokens were good right from the start after that happened. However I did make some updates to one file and was now instead presented with this after a commit:<\/p>\n<p><a href=\"https:\/\/i.imgur.com\/HNaEuQb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/HNaEuQb.png\" alt=\"\" width=\"906\" height=\"477\" \/><\/a><\/p>\n<p>Again a bit of searching I was able <a href=\"https:\/\/stackoverflow.com\/questions\/23401652\/fatal-the-current-branch-master-has-no-upstream-branch\">to find the answer<\/a>, seem usually to be some form of ignorance, that is why I&#8217;m doing this.. to learn \ud83d\ude1b<\/p>\n<p>Now again I got a bit confused at how this worked and when I did some searching I discovered:<\/p>\n<p>Don&#8217;t do a &#8220;git commit -a&#8221; from the ISE, it&#8217;ll crash asking you for a line to provide in for the description.<\/p>\n<p>Do proper staged commits <a href=\"https:\/\/stackoverflow.com\/questions\/21134960\/what-does-changes-not-staged-for-commit-mean\">as described here<\/a>. \ud83d\ude42<\/p>\n<p><a href=\"https:\/\/i.imgur.com\/aCcPJl6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/aCcPJl6.png\" alt=\"\" width=\"1209\" height=\"337\" \/><\/a><\/p>\n<p>I hope this maybe gets some more people power shelling!<\/p>\n<p>Next I should learn to use Visual Studio for more app building&#8230; but I&#8217;m more of a sys admin then a dev&#8230;<\/p>\n<p>I recently took a course in resiliency, and they basically said be a tree&#8230; ok.<\/p>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Branching\"><\/span>Branching<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Whats is branching? well pretty much, try stuff without changing the source code. Backups anyone? it&#8217;s a nice way to try stuff without breaking the original code, and once tested, can be merged.<\/p>\n<p>unlike a tree it&#8217;s not often a branch just becomes the trunk, but whatever&#8230;<\/p>\n<p>Following <a href=\"https:\/\/confluence.atlassian.com\/bitbucket\/branching-a-repository-223217999.html\">this guide<\/a>:<\/p>\n<p>To create a branch locally<\/p>\n<p>You can create a branch locally as long as you have a cloned version of the repo.<\/p>\n<p>From your terminal window, list the branches on your repository.<\/p>\n<pre>$ git branch \r\n* master<\/pre>\n<p>This output indicates there is a single branch, the master and the asterisk indicates it is currently active.<\/p>\n<p>Create a new feature branch in the repository<\/p>\n<pre>$ git branch &lt;feature_branch&gt;<\/pre>\n<p>Switch to the feature branch to work on it.<\/p>\n<pre>$ git checkout &lt;feature_branch&gt;<\/pre>\n<p>You can list the branches again with the git branch command.<\/p>\n<p>Commit the change to the feature branch:<\/p>\n<pre>$ git add . \r\n$ git commit -m \"adding a change from the feature branch\"<\/pre>\n<p>Results:<\/p>\n<p><a href=\"https:\/\/i.imgur.com\/QIILGF4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/QIILGF4.png\" alt=\"\" width=\"1176\" height=\"1211\" \/><\/a><\/p>\n<p>Hopefully tomorrow I can cover merging. \ud83d\ude42<\/p>\n<p>Cheers for now!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GitHub So as you all probably know GitHub has been acquired by Microsoft. I had initially groaned at this acquisition as usually a lot of things Microsoft has done lately has really bothered me (locking down APIs to O365 and not providing them to on Prem, for example) but then they also have done some &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/zewwy.ca\/index.php\/2019\/01\/30\/working-on-powershell-scripts-ise-w-github\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Working on PowerShell scripts (ISE) w\/ GitHub&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sfsi_plus_gutenberg_text_before_share":"","sfsi_plus_gutenberg_show_text_before_share":"","sfsi_plus_gutenberg_icon_type":"","sfsi_plus_gutenberg_icon_alignemt":"","sfsi_plus_gutenburg_max_per_row":"","footnotes":""},"categories":[12,8],"tags":[152,95,151,150],"class_list":["post-514","post","type-post","status-publish","format-standard","hentry","category-powershell","category-server-administration","tag-git","tag-github","tag-ise","tag-powershell"],"_links":{"self":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/comments?post=514"}],"version-history":[{"count":8,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/514\/revisions"}],"predecessor-version":[{"id":516,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/514\/revisions\/516"}],"wp:attachment":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/media?parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/categories?post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/tags?post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}