{"id":7635,"date":"2013-04-19T16:24:42","date_gmt":"2013-04-19T21:24:42","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=7635"},"modified":"2021-05-11T09:57:05","modified_gmt":"2021-05-11T14:57:05","slug":"windows-powershell","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=7635","title":{"rendered":"Windows PowerShell"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Overview\">Overview<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Getting_Started\">Getting Started<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Run_Scripts\">Run Scripts<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#PowerShell_Editors\">PowerShell Editors<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Special_Variables\">Special Variables<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Special_characters\">Special characters<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Set-Aliase\">Set-Aliase<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Examples\">Examples<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Print_out_Processes\">Print out Processes<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Print_out_DLLs_Loaded\">Print out DLLs Loaded<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Semicolons_and_Return_keyword_are_Optional\">Semicolons and Return keyword are Optional<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Data_Types\">Data Types<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Exception_Handling\">Exception Handling<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Quotes\">Quotes<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#PowerShell_Subexpression\">PowerShell Subexpression<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Here_Strings\">Here Strings<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Closures_Functions_and_Lambdas\">Closures, Functions, and Lambdas<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Arrays\">Arrays<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Parentheses\">Parentheses<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Hash_Tables\">Hash Tables<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Get-Member\">Get-Member<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Pipe_to_Grid_View\">Pipe to Grid View<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#New-Object\">New-Object<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Add-Member\">Add-Member<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Add-Type\">Add-Type<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Modules\">Modules<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Scan_for_Name_Value_Pairs\">Scan for Name Value Pairs<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Template_Engines\">Template Engines<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Generate_PowerShell_Functions_From_C_Methods\">Generate PowerShell Functions From C# Methods<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Calling_PowerShell_Functions_from_C\">Calling PowerShell Functions from C#<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#\"><\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#_1\"><\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Overview\"><h2>Overview<\/h2><\/span>\n<p>* PowerShell is a glue language for distributed automation in Windows environment<br \/>\n* Leverage .NET framework<br \/>\n&#8211; Can be embedded in .NET applications<br \/>\n&#8211; Can embed .NET code directly in a PowerShell script<br \/>\n&#8211; Access C# class methods: [System.Math]::Pow(2,3)<br \/>\n* cmdlets are the basic units<br \/>\n* Pre-installed in Windows 7 and above<\/p>\n<span id=\"Getting_Started\"><h2>Getting Started<\/h2><\/span>\n<p>* Start PowerShell from <em>Accessories -> Windows PowerShell<\/em> menu folder<br \/>\n* Find version:<\/p>\n<pre lang=\"ps\">\r\nWindows PowerShell\r\nCopyright (C) 2009 Microsoft Corporation. All rights reserved.\r\n\r\nPS C:\\Users\\Jimmy> $PSVersionTable\r\n\r\nName                           Value\r\n----                           -----\r\nCLRVersion                     2.0.50727.4984\r\nBuildVersion                   6.1.7600.16385\r\nPSVersion                      2.0\r\nWSManStackVersion              2.0\r\nPSCompatibleVersions           {1.0, 2.0}\r\nSerializationVersion           1.1.0.1\r\nPSRemotingProtocolVersion      2.1\r\n<\/pre>\n<p>* As calculator:<\/p>\n<pre lang=\"ps\">\r\nPS C:\\Users\\Jimmy> 2 + 2 * 3\r\n8\r\nPS C:\\Users\\Jimmy> \"Hello World\"\r\nHello World\r\nPS C:\\Users\\Jimmy> [System.Math]::Pow(2,3)\r\n8\r\n<\/pre>\n<p>* Use variable:<\/p>\n<pre lang=\"ps\">\r\nPS C:\\Users\\Jimmy> $a =  \"hello\"\r\nPS C:\\Users\\Jimmy> $a.GetType()\r\n\r\nIsPublic IsSerial Name    BaseType\r\n-------- -------- ----    --------\r\nTrue     True     String  System.Object\r\n<\/pre>\n<span id=\"Run_Scripts\"><h3>Run Scripts<\/h3><\/span>\n<p>* <em>Set-ExecutionPolicy<\/em><br \/>\n&#8211; <em>Restricted<\/em> (default): does not allow scripts or load config files<br \/>\n&#8211; <em>AllSigned<\/em>: all scripts must be signed<br \/>\n&#8211; <em>RemoteSigned<\/em>: remote scripts must be signed<br \/>\n&#8211; <em>Unrestricted<\/em>: no restriction<br \/>\n&#8211; <em>Bypass<\/em>: bypass all checks, even no warnings or prompts<br \/>\n&#8211; <em>Undefined<\/em>: remove currently assigned execution policy from the current scope <strong>except<\/strong> those in Group Policy scope<\/p>\n<pre lang=\"ps\">\r\nPS C:\\Windows\\system32> mkdir c:\\temp\r\n\r\n    Directory: C:\\\r\n\r\nMode                LastWriteTime     Length Name\r\n----                -------------     ------ ----\r\nd----         4\/19\/2013   4:11 PM            temp\r\n\r\n\r\nPS C:\\Windows\\system32> dir c:\\temp\r\nPS C:\\Windows\\system32> cd c:\\temp\r\nPS C:\\temp> Set-ExecutionPolicy Unrestricted\r\n\r\nPS C:\\temp> Get-Content .\\test.ps1\r\n\"Hello World\"\r\nPS C:\\temp> .\\test.ps1\r\nHello World\r\nPS C:\\temp>\r\n<\/pre>\n<span id=\"PowerShell_Editors\"><h3>PowerShell Editors<\/h3><\/span>\n<p>* PowerShell ISE (built-in)<br \/>\n* PowerGUI<br \/>\n* PowerShell Analyzer<br \/>\n* Professional PowerShell Script Editor (PowerSE)<br \/>\n* PrimalScript<br \/>\n* PowerShell Plus<br \/>\n* Visual Studio Plugins:<br \/>\n&#8211; NuGet<br \/>\n&#8211; StudioShell<\/p>\n<span id=\"Special_Variables\"><h3>Special Variables<\/h3><\/span>\n<p>$_ \u2013 Contains the current pipeline object, used in script blocks, filters, and the where statement.<br \/>\n$Args \u2013 Contains an array of the parameters passed to a function.<br \/>\n$Error \u2013 Contains objects for which an error occurred while being processed in a cmdlet.<br \/>\n$Home \u2013 Specifies the user\u2019s home directory.<br \/>\n$PsHome \u2013 The directory where the Windows PowerShell is installed.<\/p>\n<span id=\"Special_characters\"><h3>Special characters<\/h3><\/span>\n<p>* <em>#<\/em>: Comment character<\/p>\n<pre lang=\"ps\">\r\n# This is a comment\r\n<\/pre>\n<p>* <em>%<\/em>: is an alias for <em>ForEach<\/em><br \/>\n* <em>?<\/em>: is an alias for <em>Where<\/em><\/p>\n<pre lang=\"ps\">\r\n1..10 | ? {$_ % 2 -eq 0} | % {$_* 2}\r\n\r\n4\r\n8\r\n12\r\n16\r\n20\r\n<\/pre>\n<span id=\"Set-Aliase\"><h3>Set-Aliase<\/h3><\/span>\n<pre lang=\"ps\">\r\nSet-Alias np \"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"\r\nnp\r\n<\/pre>\n<span id=\"Examples\"><h2>Examples<\/h2><\/span>\n<span id=\"Print_out_Processes\"><h3>Print out Processes<\/h3><\/span>\n<pre lang=\"ps\">\r\nPS C:\\temp> Get-Process | Where {$_.Handles -gt 750} | Sort PM -Descending\r\n\r\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName\r\n-------  ------    -----      ----- -----   ------     -- -----------\r\n    979     211  1154988    1204292  1755 ...70.65   5168 firefox\r\n    986      36   309332     327200   507 4,377.56   1688 svchost\r\n   1208     117   159196     176844   542   108.06   9228 VpxClient\r\n   1685      61   154912     128292   585 9,778.64   4508 FlashPlayerPlugin_11_6_602_180\r\n   1477     105   107580     224284   563 1,144.64   5328 explorer\r\n   2409      57    58104      89468   510   127.56   1728 svchost\r\n   1003      58    45220      23216   290   278.49   9456 avgui\r\n    824      49    42332      45240   632    43.27   1144 svchost\r\n   1357      27    38520      45224   147   541.70   2028 avgrsa\r\n    880      25    38184      30512   210   610.51   1140 csrss\r\n    823      50    23032      38368   113    18.77   1872 svchost\r\n    755      17    18516      42500   123    43.84   2136 nvxdsync\r\n    942      38    15232      29568   146 1,090.90   5792 avgwdsvc\r\n    993      30    13300      30016   133 1,989.00   3296 AcSvc\r\n    944      31    12444      58312    84   227.12   1212 lsass\r\n    857      16     4092      15068   123    11.95    772 csrss\r\n   1103      10     2600       6712    53     1.45   2596 CamMute\r\n   1435       0      424      10816   143               4 System\r\n<\/pre>\n<span id=\"Print_out_DLLs_Loaded\"><h3>Print out DLLs Loaded<\/h3><\/span>\n<pre lang=\"ps\">\r\nPS C:\\temp> [System.AppDomain]::CurrentDomain.GetAssemblies() |\r\n>> Where {$_.location} |\r\n>> ForEach { Split-Path -Leaf $_.location } |\r\n>> Sort\r\n>>\r\nMicrosoft.PowerShell.Commands.Diagnostics.dll\r\nMicrosoft.PowerShell.Commands.Management.dll\r\nMicrosoft.PowerShell.Commands.Utility.dll\r\nMicrosoft.PowerShell.ConsoleHost.dll\r\nMicrosoft.PowerShell.Security.dll\r\nMicrosoft.WSMan.Management.dll\r\nmscorlib.dll\r\nSystem.Configuration.Install.dll\r\nSystem.Core.dll\r\nSystem.Data.dll\r\nSystem.DirectoryServices.dll\r\nSystem.dll\r\nSystem.Management.Automation.dll\r\nSystem.Management.dll\r\nSystem.Transactions.dll\r\nSystem.Xml.dll\r\n<\/pre>\n<span id=\"Semicolons_and_Return_keyword_are_Optional\"><h3>Semicolons and Return keyword are Optional<\/h3><\/span>\n<pre lang=\"ps\">\r\nPS C:\\temp> function SayHello($p) {\r\n>>     return \"Hello $p\"\r\n>> }\r\n>>\r\nPS C:\\temp> SayHello World\r\nHello World\r\nPS C:\\temp> function SayHello($p) {\r\n>>     \"Hello $p\"\r\n>> }\r\n>>\r\nPS C:\\temp> SayHello World\r\nHello World\r\n<\/pre>\n<span id=\"Data_Types\"><h3>Data Types<\/h3><\/span>\n<pre lang=\"ps\">\r\nPS C:\\temp> $a = \"Hello\"\r\nPS C:\\temp> $a\r\nHello\r\nPS C:\\temp> $a = 1\r\nPS C:\\temp> $a += 1\r\nPS C:\\temp> $a\r\n2\r\nPS C:\\temp> $a = 1,2,3\r\nPS C:\\temp> $a\r\n1\r\n2\r\n3\r\nPS C:\\temp> [int]$a = 1\r\nPS C:\\temp> $a\r\n1\r\nPS C:\\temp> [int]$a = \"Hello\"\r\nCannot convert value \"Hello\" to type \"System.Int32\". Error: \"Input string was not in a correct format.\"\r\nAt line:1 char:8\r\n+ [int]$a <<<<  = \"Hello\"\r\n    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException\r\n    + FullyQualifiedErrorId : RuntimeException\r\n<\/pre>\n<p>* Strong typed parameters:<\/p>\n<pre lang=\"ps\">\r\nPS C:\\temp> function Do-PrecisionCalculation {\r\n>>     param (\r\n>>         [Double]$Latitude,\r\n>>         [Double]$Longitude\r\n>>     )\r\n>>\r\n>>     [Math]::Sin($Latitude) * [Math]::Sin($Longitude)\r\n>> }\r\n>>\r\nPS C:\\temp> Do-PrecisionCalculation -Latitude 100 -Longitude 100\r\n0.256406162496497\r\n<\/pre>\n<span id=\"Exception_Handling\"><h3>Exception Handling<\/h3><\/span>\n<p>* Supports <em>try\/catch\/finally<\/em>:<\/p>\n<pre lang=\"ps\">\r\nPS C:\\Users\\Jimmy> function div-by-zero($a, $b){\r\n    try {\r\n        $a\/$b\r\n        \"Hello World\"\r\n    } catch {\r\n        \"Error caught: $($error[0])\"\r\n    } finally {\r\n        \"Finally, Hello World\"\r\n    }\r\n}\r\n\r\nPS C:\\Users\\Jimmy> div-by-zero -a 1 -b 0\r\nError caught: Attempted to divide by zero.\r\nFinally, Hello World\r\n<\/pre>\n<span id=\"Quotes\"><h3>Quotes<\/h3><\/span>\n<pre lang=\"ps\">\r\nPS C:\\temp>\r\nPS C:\\temp> \"A string\"\r\nA string\r\nPS C:\\temp>\r\nPS C:\\temp> \"A string with 'Quotes'\"\r\nA string with 'Quotes'\r\nPS C:\\temp>\r\nPS C:\\temp> \"A string with `\"Escaped Quotes`\"\"\r\nA string with \"Escaped Quotes\"\r\nPS C:\\temp>\r\nPS C:\\temp> $s = \"PowerShell\"\r\nPS C:\\temp> \"A string with a variable: $s\"\r\nA string with a variable: PowerShell\r\nPS C:\\temp>\r\nPS C:\\temp> \"A string with a quoted variable: '$s'\"\r\nA string with a quoted variable: 'PowerShell'\r\nPS C:\\temp>\r\nPS C:\\temp> 'Variables are not replaced inside single quotes: $s'\r\nVariables are not replaced inside single quotes: $s\r\n<\/pre>\n<span id=\"PowerShell_Subexpression\"><h3>PowerShell Subexpression<\/h3><\/span>\n<pre lang=\"ps\">\r\nPS C:\\temp> $process = (Get-Process)[0]\r\nPS C:\\temp> $process.PM\r\n6443008\r\nPS C:\\temp> \"$process.PM\"\r\nSystem.Diagnostics.Process (AcDeskBandHlpr).PM\r\nPS C:\\temp> \"$($process.PM)\"\r\n6443008\r\n<\/pre>\n<span id=\"Here_Strings\"><h3>Here Strings<\/h3><\/span>\n<pre lang=\"ps\">\r\n$name = \"World\"\r\n\r\n$HereString = @\"\r\nThis is a here-string\r\nIt can contain multiple lines\r\n\"Quotes don't need to be escaped\"\r\nPlus, you can include variables 'Hello $name'\r\n\"@\r\n\r\nPS C:\\Users\\Jimmy> $HereString\r\nThis is a here-string\r\nIt can contain multiple lines\r\n\"Quotes don't need to be escaped\"\r\nPlus, you can include variables 'Hello World'\r\n<\/pre>\n<span id=\"Closures_Functions_and_Lambdas\"><h3>Closures, Functions, and Lambdas<\/h3><\/span>\n<p>* Closures:<br \/>\n- surrounded by <em>{}<\/em><br \/>\n- executed by <em>&<\/em> operator<\/p>\n<pre lang=\"ps\">\r\nPS C:\\temp> $n = \"PowerShell\"\r\nPS C:\\temp> $closure = {\"Hello $n\"}\r\nPS C:\\temp> & $closure\r\nHello PowerShell\r\nPS C:\\temp>\r\n<\/pre>\n<p>* Functions (closures or script blocks <strong>with<\/strong> names):<\/p>\n<pre lang=\"ps\">\r\nPS C:\\temp> function Add5 ($num) {\r\n>>     $num + 5\r\n>> }\r\n>>\r\nPS C:\\temp> Add5 5\r\n10\r\n<\/pre>\n<p>* Lambdas (closures or script blocks <strong>without<\/strong> names):<\/p>\n<pre lang=\"ps\">\r\nPS C:\\temp> $add5 = {param($num) $num + 5}\r\nPS C:\\temp> & $add5 5\r\n10\r\n<\/pre>\n<span id=\"Arrays\"><h3>Arrays<\/h3><\/span>\n<p>* Same as .NET <em>System.Array<\/em><\/p>\n<pre lang=\"ps\">\r\n$animals = \"cat\", \"dog\", \"fish\"\r\n$animals.GetType()\r\n$animals\r\n\r\nIsPublic IsSerial Name       BaseType                                       \r\n-------- -------- ----       --------                                       \r\nTrue     True     Object[]   System.Array                                   \r\ncat\r\ndog\r\nfish\r\n<\/pre>\n<p>* Empty array:<\/p>\n<pre lang=\"ps\">\r\n$animals = @()\r\n$animals.Count\r\n\r\n0\r\n<\/pre>\n<p>* Add an array item:<\/p>\n<pre lang=\"ps\">\r\n$animals = \"cat\", \"dog\", \"fish\"\r\n$animals += \"bird\"\r\n$animals\r\n\r\ncat\r\ndog\r\nfish\r\nbird\r\n<\/pre>\n<p>* Access an array item:<\/p>\n<pre lang=\"ps\">\r\n$animals = \"cat\", \"dog\", \"fish\"\r\n$animals[1]\r\n<\/pre>\n<p>* Array slicing:<\/p>\n<pre lang=\"ps\">\r\n$animals = \"cat\", \"dog\", \"fish\"\r\n$animals[0..1]\r\n\r\ncat\r\ndog\r\n<\/pre>\n<p>* Finding array elements:<\/p>\n<pre lang=\"ps\">\r\n$animals = \"cat\", \"dog\", \"fish\"\r\n$animals -ne 'cat'\r\n\r\ndog\r\nfish\r\n\r\n$animals = \"cat\", \"dog\", \"fish\"\r\n#$animals -ne 'cat'\r\n$animals -like \"*o*\"\r\n\r\ndog\r\n<\/pre>\n<p>* Reversing an array:<\/p>\n<pre lang=\"ps\">\r\n$animals = \"cat\", \"dog\", \"fish\"\r\n[array]::Reverse($animals)\r\n$animals\r\n\r\nfish\r\ndog\r\ncat\r\n<\/pre>\n<p>* Assign array values to multiple variables<\/p>\n<pre lang=\"ps\">\r\n$items = \"One\", \"Two\", \"Three\", 1, 2, 3\r\n$items\r\n\r\n$one, $two, $three, $numbers = $items\r\n\r\n$one\r\n$two\r\n$three\r\n$numbers\r\n\r\nOne\r\nTwo\r\nThree\r\n1\r\n2\r\n3\r\nOne\r\nTwo\r\nThree\r\n1\r\n2\r\n3\r\n<\/pre>\n<span id=\"Parentheses\"><h3>Parentheses<\/h3><\/span>\n<pre lang=\"ps\">\r\n* Don't use parentheses to pass parameters to a function:\r\nfunction Test($p1, $p2) {\r\n    '$p1' + \"is: \" + $p1\r\n    '$p2' + \"is: \" + $p2\r\n    \"\"\r\n}\r\n\r\nTest (1, 2) # Actually passing in an array\r\n\r\nTest 1 2\r\n\r\n$p1is: 1 2\r\n$p2is: \r\n\r\n$p1is: 1\r\n$p2is: 2\r\n<\/pre>\n<span id=\"Hash_Tables\"><h3>Hash Tables<\/h3><\/span>\n<pre lang=\"ps\">\r\n# Empty hash map\r\n$h = @{}\r\n$h.Count\r\n\r\n# Assign elements\r\n$h.Item1 = 1\r\n$h.Item2 = 2\r\n\r\n# Initialize hash map\r\n$h = @{Item1=1; Item2=2}\r\n\r\n# Access hash map\r\n$h\r\n$h.Item1\r\n$h.Item2\r\n\r\n# Concatenate hash tables\r\n$h1 = @{a=1;b=2}\r\n$h2 = @{c=3;d=4}\r\n$h = $h1 + $h2\r\n$h\r\n<\/pre>\n<span id=\"Get-Member\"><h3>Get-Member<\/h3><\/span>\n<p>* Returns the members (properties and methods) of objects at the command line.<\/p>\n<pre lang=\"ps\">\r\nNew-Object Net.Webclient | Get-Member -MemberType Property\r\n\r\n   TypeName: System.Net.WebClient\r\n\r\nName                  MemberType Definition                                                               \r\n----                  ---------- ----------                                                               \r\nBaseAddress           Property   System.String BaseAddress {get;set;}                                     \r\nCachePolicy           Property   System.Net.Cache.RequestCachePolicy CachePolicy {get;set;}               \r\nContainer             Property   System.ComponentModel.IContainer Container {get;}                        \r\nCredentials           Property   System.Net.ICredentials Credentials {get;set;}                           \r\nEncoding              Property   System.Text.Encoding Encoding {get;set;}                                 \r\nHeaders               Property   System.Net.WebHeaderCollection Headers {get;set;}                        \r\nIsBusy                Property   System.Boolean IsBusy {get;}                                             \r\nProxy                 Property   System.Net.IWebProxy Proxy {get;set;}                                    \r\nQueryString           Property   System.Collections.Specialized.NameValueCollection QueryString {get;set;}\r\nResponseHeaders       Property   System.Net.WebHeaderCollection ResponseHeaders {get;}                    \r\nSite                  Property   System.ComponentModel.ISite Site {get;set;}                              \r\nUseDefaultCredentials Property   System.Boolean UseDefaultCredentials {get;set;} \r\n<\/pre>\n<span id=\"Pipe_to_Grid_View\"><h3>Pipe to Grid View<\/h3><\/span>\n<pre lang=\"ps\">\r\nNew-Object Net.Webclient | Get-Member | Out-GridView\r\n<\/pre>\n<span id=\"\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2013\/04\/PowerShell_Out-GridView.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2013\/04\/PowerShell_Out-GridView-216x300.jpg\" alt=\"\" title=\"PowerShell_Out-GridView\" width=\"216\" height=\"300\" class=\"aligncenter size-medium wp-image-7642\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2013\/04\/PowerShell_Out-GridView-216x300.jpg 216w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2013\/04\/PowerShell_Out-GridView.jpg 568w\" sizes=\"auto, (max-width: 216px) 100vw, 216px\" \/><\/a><\/h6><\/span>\n<span id=\"New-Object\"><h3>New-Object<\/h3><\/span>\n<p>* Creates an instance of .NET object<br \/>\n* Examples:<br \/>\n- Open IE and go to google.com:<\/p>\n<pre lang=\"ps\">\r\n$ie = New-Object -ComObject InternetExplorer.Application\r\n$ie.Navigate2(\"http:\/\/www.google.com\")\r\n$ie.Visible = $true\r\n<\/pre>\n<p>- Create a new PowerShell object:<\/p>\n<pre lang=\"ps\">\r\n$obj = New-Object PSObject -Property @{Name=\"John\";Age=\"10\"}\r\n$obj.GetType()\r\n$obj\r\n\r\nIsPublic IsSerial Name             BaseType                                       \r\n-------- -------- ----             --------                                       \r\nTrue     False    PSCustomObject   System.Object                                  \r\n\r\nName : John\r\nAge  : 10\r\n<\/pre>\n<p>- Use .NET framework:<\/p>\n<pre lang=\"ps\">\r\n$wc = New-Object Net.WebClient\r\n[xml]$resp = $wc.DownloadString(\"http:\/\/feeds.feedburner.com\/DevelopmentInABlink\")\r\n$resp.rss.channel.item | ForEach {$_.Title}\r\n\r\nPowerShell Automation\r\nCreating a PowerShell Translate Function\r\nHow to run PowerShell from node.js\r\nUsing PowerShell to find all the years since you were born where the four digits were unique\r\nUse PowerShell to find the free space on the volume behind an SMB file share\r\nLearn one PowerShell command\r\nI\u00e2\u20ac\u2122ll be presenting on PowerShell at the Philly Code Camp\r\nBuilding GUI Applications in PowerShell\r\nHonorary PowerShell Scripting Guy\r\nPowerShell kv \u00e2\u20ac\u201c save snippets of text that you can later find and copy to your clipboard\r\n<\/pre>\n<span id=\"Add-Member\"><h3>Add-Member<\/h3><\/span>\n<pre lang=\"ps\">\r\n$s = \"Hello World\" | Add-Member -PassThru ScriptProperty Reverse {$this[$this.Length..0] -join \"\"}\r\n$s\r\n$s.Reverse\r\n\r\nHello World\r\ndlroW olleH\r\n<\/pre>\n<span id=\"Add-Type\"><h3>Add-Type<\/h3><\/span>\n<p>* Compile C# on the fly:<\/p>\n<pre lang=\"ps\">\r\nAdd-Type -TypeDefinition @\"\r\npublic class MyMathClass {\r\n    public int Add(int n1, int n2) {\r\n        return n1 + n2;\r\n    }\r\n}\r\n\"@\r\n\r\n$obj = New-Object MyMathClass\r\n$obj.GetType()\r\n$obj | Get-Member\r\n\r\n1..5 | ForEach {$obj.Add($_,$_)}\r\n<\/pre>\n<span id=\"Modules\"><h3>Modules<\/h3><\/span>\n<p>TODO.<\/p>\n<span id=\"Scan_for_Name_Value_Pairs\"><h3>Scan for Name Value Pairs<\/h3><\/span>\n<pre lang=\"ps\">\r\n$regex = \"(?<name>\\w+)\\s*=\\s*(?<value>.*)((?=\\W$)|\\z)\"\r\n\r\ndir c:\\temp *.txt | Select-String $regex |\r\n    ForEach {\r\n        $fn = $_.Path\r\n        ForEach($match in $_.Matches) {\r\n            $obj = New-Object PSObject -Property @{\r\n                FileName = $fn\r\n                Name = $match.Groups[\"name\"].Value\r\n                Value = $match.Groups[\"value\"].Value\r\n            }\r\n        }\r\n        $obj\r\n    }\r\n<\/pre>\n<span id=\"Template_Engines\"><h3>Template Engines<\/h3><\/span>\n<p>* Process templates and content to produce output<br \/>\nTODO<\/p>\n<span id=\"Generate_PowerShell_Functions_From_C_Methods\"><h3>Generate PowerShell Functions From C# Methods<\/h3><\/span>\n<p>* MyMath C# class:<\/p>\n<pre lang=\"ps\">\r\n$code = @\"\r\npublic class MyMath {\r\n    public int MyAdd(int n1, int n2) {\r\n        return n1 + n2;\r\n    }\r\n    \r\n    public int MySubstract (int n1, int n2) {\r\n        return n1 - n2;\r\n    }\r\n}\r\n\"@\r\n\r\nAdd-Type -TypeDefinition $code\r\n\r\n[MyMath].GetMethods() \r\n\r\n[MyMath].GetMethods() |\r\n    ForEach {\r\n        $_.GetParameters()\r\n    }\r\n<\/pre>\n<p>* PowerShell Wrapper:<\/p>\n<pre lang=\"ps\">\r\n$MyMath = New-Object MyMath\r\nfunction Invoke-MyAdd ($n1, $n2) {$MyMath.MyAdd($n1, $n2)}\r\nfunction Invoke-MySubstract ($n1, $n2) {$MyMath.MySubstract($n1, $n2)}\r\n\r\nInvoke-MyAdd 1 3\r\n\r\n1..10|\r\nForEach {Invoke-MyAdd $_ $_}\r\n<\/pre>\n<p>* Note that once type is added, it can <strong>not <\/strong>be removed until you restart PowerShell<\/p>\n<span id=\"Calling_PowerShell_Functions_from_C\"><h3>Calling PowerShell Functions from C#<\/h3><\/span>\n<p>TODO<\/p>\n<span id=\"_1\"><h2><\/h2><\/span>\n<span id=\"_2\"><h3><\/h3><\/span>\n<pre lang=\"ps\">\r\n<\/pre>\n<span id=\"_3\"><h6><\/h6><\/span>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* Windows PowerShell for Developers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview * PowerShell is a glue language for distributed automation in Windows environment * Leverage .NET framework &#8211; Can be embedded in .NET applications &#8211; Can embed .NET code directly in a PowerShell script &#8211; Access C# class methods: [System.Math]::Pow(2,3) &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=7635\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[376],"tags":[636],"class_list":["post-7635","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-1Z9","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/7635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7635"}],"version-history":[{"count":5,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/7635\/revisions"}],"predecessor-version":[{"id":12673,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/7635\/revisions\/12673"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}