{"id":80855,"date":"2016-11-18T00:01:25","date_gmt":"2016-11-18T08:01:25","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=80855"},"modified":"2019-02-18T09:10:20","modified_gmt":"2019-02-18T16:10:20","slug":"80855","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/80855\/","title":{"rendered":"Open Source PowerShell \u2013 Part 3"},"content":{"rendered":"<p><strong>Summary<\/strong>: Pass and return data correctly between Bash and Open Source PowerShell.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> Could you show me some examples of actually passing and returning values between PowerShell and Bash?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/> Honorary Scripting Guy, Sean Kearney, is here today and we\u2019ll do exactly that. It isn\u2019t difficult to actually to do this either, but there are two things to keep in mind to make all of this easier.<\/p>\n<p>When working with data:<\/p>\n<ul>\n<li>PowerShell lives in the world of Objects<\/li>\n<li>Bash lives in the world of text<\/li>\n<\/ul>\n<p>If you keep this in mind, much of the challenge is in just understanding that when PowerShell parameters are set, they should be typed correctly by using the appropriate .NET class.<\/p>\n<p>You\u2019ll also need to keep in mind that when returning objects to Bash (or any other text-based environment like even CMD or VBScript in Windows as an example), the result should be a string.<\/p>\n<p>Look at this simple script in PowerShell as an example. The script does the following:<\/p>\n<ul>\n<li>Receives three parameters<\/li>\n<li>Does a simple Write-Host to the screen to echo the values<\/li>\n<\/ul>\n<p>Returns an exit code of 42 (Well, we have to have a little fun here!)<\/p>\n<p style=\"padding-left: 60px\"><code>param\n(\n$user,\n$birthday,\n$age\n)<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Write-Output \u201cThe Username is $user\u201d\nWrite-Output \u201cTheir Birthday is on $birthday\u201d\nWrite-Output \u201cThe age of the user is $age\u201d<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Exit 42<\/code><\/p>\n<p>We can start this PowerShell script from Bash (We\u2019ll call it fooparam.ps1), and pass three values in the following manner:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-111816.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-111816.png\" alt=\"Results of the executed script\" width=\"529\" height=\"99\" class=\"alignnone size-full wp-image-80865\" \/><\/a><\/p>\n<p>What you can see is some basic parameter passing. Out of the box, it works quite fine.<\/p>\n<p>But, consider this. Perhaps later in the script the value for <code>$Birthday<\/code> is meant to be processed as a proper [datetime] or that PowerShell is expecting an actual Integer. A well written script should normally deal with this.<\/p>\n<p>In PowerShell, it is very easy to forget this sometimes because it can automatically see a \u2018123\u2019 and realize that is an <strong>[int]<\/strong>.\u00a0 Or, that \u2018Loud Noisy Parrot\u2019 is a <strong>[string]<\/strong>. We don\u2019t normally need to pull off any extra effort to make it happen.<\/p>\n<p>In Bash (like in VBScript), all of the output is just text. It has no clue about the difference between <strong>[System.Management.Automation.PSCredential]<\/strong> or <strong>[String]<\/strong>. Bash only receives and sends text.<\/p>\n<p>In PowerShell, you\u2019ll need to make sure the correct definition and validation of parameters is in there.\u00a0\u00a0 But, that\u2019s the part that most of us will be missing.<\/p>\n<p>It\u2019s the actual <u>returning<\/u> of data that can trip us up. Take, for example, this little Script that simply returns some assigned variables to the screen.<\/p>\n<p style=\"padding-left: 60px\"><code>$c=New-Object System.Management.Automation.PsCredential -ArgumentList ('me',(Convertto-Securestring -asPlainText -force 'badpass'))<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$Date=Get-Date\n$Name='John Smith'<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Return [pscustomobject]@{Credential=$c;Date=$Date;Name=$name}<\/code><\/p>\n<p>If you ran this script directly from Bash, you\u2019d see the output in default string values. For example, <strong>DateTime<\/strong> produces a full suite of the date and time as it\u2019s default <strong>String<\/strong> object.<\/p>\n<p>In the case where <strong>Object<\/strong> is more complex than a simple string, we get the default .NET class name, such as <strong>System.Management.Automation.PSCredential<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-111816.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-111816.png\" alt=\"Bash returns the default .NET class name as string values\" width=\"540\" height=\"123\" class=\"alignnone size-full wp-image-80875\" \/><\/a><\/p>\n<p>But, this can be dealt with quite easily. When returning an <strong>Object<\/strong> to Bash, ensure that you clean up the <strong>String<\/strong> output to what Linux\/macOS might be expecting. A Password or the User ID (or perhaps both) might be required for the Bash script that calls up PowerShell.<\/p>\n<p>It could also be that the calling environment only wants the exact date in \u201cMonth Day Year\u201d format. Our challenge is quite simple. Validate and clean up the output to correct strings.<\/p>\n<p>Here is the sample script that\u2019s slightly modified to clean up the output for the calling Bash system.<\/p>\n<p style=\"padding-left: 60px\"><code>$c=New-Object System.Management.Automation.PsCredential -ArgumentList ('me',(Convertto-Securestring -asPlainText -force 'badpass'))<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$Date=Get-Date\n$Name='John Smith'<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$Password=$c.getnetworkcredential().\n$ShortDate=Get-Date $Date -format \u2018MM\/dd\/yyyy\u2019\n$LowerName=$Name.tolower()<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Return [pscustomobject]@{Credential=$Password;Date=$ShortDate;Name=$Lowername}<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-111816.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-111816.png\" alt=\"Bash returns credential, date, and name\" width=\"437\" height=\"219\" class=\"alignnone size-full wp-image-80885\" \/><\/a><\/p>\n<p>With very little change to the script, we can keep the string output clean and useful for the Bash environment.<\/p>\n<p>Stop by next time as we look at getting PowerShell remote to work between Linux and Windows.<\/p>\n<p>I invite you to follow the Scripting Guys on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to them at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow.<\/p>\n<p>Until then, always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney\n<\/strong>Honorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Pass and return data correctly between Bash and Open Source PowerShell. Could you show me some examples of actually passing and returning values between PowerShell and Bash? Honorary Scripting Guy, Sean Kearney, is here today and we\u2019ll do exactly that. It isn\u2019t difficult to actually to do this either, but there are two things [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,641],"tags":[56,154,45],"class_list":["post-80855","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Pass and return data correctly between Bash and Open Source PowerShell. Could you show me some examples of actually passing and returning values between PowerShell and Bash? Honorary Scripting Guy, Sean Kearney, is here today and we\u2019ll do exactly that. It isn\u2019t difficult to actually to do this either, but there are two things [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=80855"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80855\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=80855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=80855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=80855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}