{"id":1405,"date":"2022-09-09T13:35:55","date_gmt":"2022-09-09T18:35:55","guid":{"rendered":"http:\/\/zewwy.ca\/?p=1405"},"modified":"2022-09-09T13:40:13","modified_gmt":"2022-09-09T18:40:13","slug":"hypertext-string-validation-via-powershell","status":"publish","type":"post","link":"https:\/\/zewwy.ca\/index.php\/2022\/09\/09\/hypertext-string-validation-via-powershell\/","title":{"rendered":"Hypertext String Validation via Powershell"},"content":{"rendered":"<p>So I had this running code:<\/p>\n<pre>function isURL($URL) \r\n{\r\n$uri = $URL -as [System.URI]\r\n$uri.AbsoluteURI -ne $null -and $uri.Scheme -match \"http|https\"\r\n}\r\n\r\nisURL('http:\/\/www.powershell.com')\r\nisURL('test')\r\nisURL($null)\r\nisURL('zzz:\/\/zumsel.zum')\r\nisURL('hp:')\r\nisURL('https:')\r\nisURL('http')\r\nisURL('http:\/incomplete')\r\nisURL('Maybenot.http:\/\/complete') #our function has an outliar here\r\nisURL('http:\/\/complete.should.return.true')\r\nisURL('https:\/\/also.complete.should.return.true')<\/pre>\n<p>Though there was one outliar, lets fix that&#8230;<\/p>\n<p>I was having some issues playing around with different things, till I got me head out my ass and followed KISS principal..<\/p>\n<p>Found <a href=\"https:\/\/www.snippset.com\/check-if-string-starts-with-in-powershell-e1494\">this simple reference<\/a>&#8230; and made a simple change in my code&#8230;<\/p>\n<pre>function isURL($URL) \r\n{\r\n$uri = $URL -as [System.URI]\r\n$uri.AbsoluteURI -ne $null -and $uri.Scheme -like \"http*\"\r\n\r\n}\r\n\r\nisURL('http:\/\/www.powershell.com')\r\nisURL('test')\r\nisURL($null)\r\nisURL('zzz:\/\/zumsel.zum')\r\nisURL('hp:')\r\nisURL('https:')\r\nisURL('http')\r\nisURL('http:\/incomplete')\r\nisURL('Maybenot.http:\/\/complete') #All Good now :)\r\nisURL('http:\/\/complete.should.return.true')\r\nisURL('https:\/\/also.complete.should.return.true')<\/pre>\n<p>Normally if your doing coding in other languages and not writing scripts, you&#8217;d usually want to write actual test code blocks. In scripting usually just keep things simple by utilizing input validation. If you look online you can use Invoke-Request but that requires being dependent on proper network stack and puts a load on the server or something that could easily be validated client side before any server requests are made.<\/p>\n<p>Hope this helps someone.<\/p>\n<p>Bonus (getting all sub paths from a URL string):<\/p>\n<pre>$Tet = \"http:\/\/somesite.notorg\/subsite\/subite2\/s3\/doc\/folder\/no\/matter\/how\/deep?\"\r\n$Array = ($Tet -split \"\/\")\r\n$Array = $Array[3..($Array.length -1)]\r\nforeach($Item in $Array)\r\n{\r\n$FullLine = $FullLine + \"\\\" + $Item\r\n}\r\n$FullLine<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So I had this running code: function isURL($URL) { $uri = $URL -as [System.URI] $uri.AbsoluteURI -ne $null -and $uri.Scheme -match &#8220;http|https&#8221; } isURL(&#8216;http:\/\/www.powershell.com&#8217;) isURL(&#8216;test&#8217;) isURL($null) isURL(&#8216;zzz:\/\/zumsel.zum&#8217;) isURL(&#8216;hp:&#8217;) isURL(&#8216;https:&#8217;) isURL(&#8216;http&#8217;) isURL(&#8216;http:\/incomplete&#8217;) isURL(&#8216;Maybenot.http:\/\/complete&#8217;) #our function has an outliar here isURL(&#8216;http:\/\/complete.should.return.true&#8217;) isURL(&#8216;https:\/\/also.complete.should.return.true&#8217;) Though there was one outliar, lets fix that&#8230; I was having some issues playing around with &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/zewwy.ca\/index.php\/2022\/09\/09\/hypertext-string-validation-via-powershell\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Hypertext String Validation via Powershell&#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":[120,422,419,423],"class_list":["post-1405","post","type-post","status-publish","format-standard","hentry","category-powershell","category-server-administration","tag-http","tag-powerhshell","tag-string","tag-validation"],"_links":{"self":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/1405","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=1405"}],"version-history":[{"count":2,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/1405\/revisions"}],"predecessor-version":[{"id":1407,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/1405\/revisions\/1407"}],"wp:attachment":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/media?parent=1405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/categories?post=1405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/tags?post=1405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}