{"id":33113,"date":"2010-11-22T18:44:21","date_gmt":"2010-11-22T17:44:21","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=33113"},"modified":"2024-11-06T11:22:38","modified_gmt":"2024-11-06T03:22:38","slug":"windows-powershell-2-0%e8%af%ad%e8%a8%80%e4%b9%8b%e5%87%bd%e6%95%b0%e5%92%8c%e8%bf%87%e6%bb%a4%e5%99%a8-2","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33113","title":{"rendered":"Windows PowerShell 2.0\u8bed\u8a00\u4e4b\u51fd\u6570\u548c\u8fc7\u6ee4\u5668"},"content":{"rendered":"<p>\u672c\u6587\u8ba8\u8bba\u4e86PowerShell\u63d0\u4f9b\u7684\u4e1a\u52a1\u63a7\u5236\u673a\u5236\u51fd\u6570\u548c\u8fc7\u6ee4\u5668\uff0c\u51fd\u6570\u662f\u7528\u6765\u6269\u5c55\u5185\u7f6eshell\u529f\u80fd\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5c3d\u5feb\u638c\u63e1\u51fd\u6570\u975e\u5e38\u91cd\u8981\uff0c\u4f1a\u4fbf\u4e8e\u5bf9\u4e1a\u52a1\u8fdb\u884c\u5c01\u88c5\u3002\u201c\u5206\u800c\u6cbb\u4e4b\u201d\u7684\u7b56\u7565\u5728PowerShell\u4e2d\u91cd\u8981\u800c\u6709\u6548\uff0c\u7528\u6237\u80fd\u591f\u5c06\u5212\u5206\u540e\u7684\u5b50\u95ee\u9898\u4f7f\u7528\u51fd\u6570\u6765\u89e3\u51b3\uff0c\u6700\u540e\u53ef\u4ee5\u901a\u8fc7\u5c06\u591a\u4e2a\u51fd\u6570\u8054\u5408\u8d77\u6765\u7684\u65b9\u6cd5\u89e3\u51b3\u9ad8\u96be\u5ea6\u7684\u95ee\u9898\u3002\u7528\u6237\u9700\u8981\u9010\u6e10\u719f\u6089\u548c\u4e60\u60ef\u5c06\u590d\u6742\u7684\u95ee\u9898\u5212\u5206\u6210\u4e3a\u591a\u4e2a\u8f83\u4e3a\u7b80\u5355\u7684\u5c0f\u95ee\u9898\uff0c\u5355\u4e2a\u5c0f\u95ee\u9898\u5212\u5206\u5230\u76f8\u5bf9\u72ec\u7acb\u800c\u6613\u4e8e\u5904\u7406\u7684\u7a0b\u5ea6\u540e\u5355\u72ec\u5904\u7406\uff0c\u8fd9\u6837\u89e3\u51b3\u95ee\u9898\u7684\u6210\u672c\u4f1a\u964d\u4f4e\u3002\u5212\u5206\u95ee\u9898\u7684\u6a21\u5f0f\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u5212\u5206\u7684\u6807\u51c6\u4e00\u822c\u662f\u5c3d\u91cf\u964d\u4f4e\u6a21\u5757\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u4e0d\u53ef\u907f\u514d\u7684\u8026\u5408\u5173\u7cfb\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u95f4\u53d8\u91cf\u7684\u4f20\u9012\u5b9e\u73b0\u6570\u636e\u7684\u4ea4\u6362\u3002\u8fd9\u6837\u5355\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u8f83\u4e3a\u72ec\u7acb\uff0c\u4fbf\u4e8e\u4ee3\u7801\u7684\u91cd\u590d\u4f7f\u7528\uff0c\u4e5f\u4fbf\u4e8e\u51fa\u73b0\u9519\u8bef\u65f6\u7684\u8c03\u8bd5\u548c\u6392\u9519\u3002 <\/p>\n<p>1 \u5b9a\u4e49\u51fd\u6570 <\/p>\n<p>\u51fd\u6570\u7684\u53ef\u6267\u884c\u4ee3\u7801\u5757\u7c7b\u4f3c\u811a\u672c\u5757\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u811a\u672c\u5757\u662f\u533f\u540d\u7684\uff0c\u5728\u6267\u884c\u4e4b\u524d\u4f1a\u88ab\u6307\u5b9a\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff1b\u51fd\u6570\u5728\u521b\u5efa\u65f6\u83b7\u53d6\u540d\u79f0\uff0c\u51fd\u6570\u540d\u4f1a\u7acb\u5373\u751f\u6548\u5e76\u53ef\u4ee5\u6267\u884c\u3002\u5b9a\u4e49\u51fd\u6570\u4f7f\u7528function\u5173\u952e\u5b57\uff0c\u5176\u683c\u5f0f\u5982\u4e0b\uff1a <\/p>\n<pre>function &lt;name&gt;(&lt;parameter list&gt;)\n{\n  &lt;function body&gt;\n}<\/pre>\n<p>\u51fd\u6570\u540d\u5e94\u8be5\u4ee5\u5b57\u6bcd\u5f00\u5934\uff0c\u5305\u542b\u5b57\u6bcd\u548c\u6570\u5b57\u7684\u7ec4\u5408\uff0c\u4ee5\u53ca\u8fde\u5b57\u7b26\uff08-\uff09\u548c\u4e0b\u753b\u7ebf\uff08_\uff09\u3002\u4e0b\u4f8b\u4f7f\u7528\u51fd\u6570\u8f93\u51fa\u5b57\u7b26\uff1a <\/p>\n<pre>PS C:\\&gt;function Say-Hello()\n&gt;&gt;  {\n&gt;&gt;    Write-Host \u201cHello World from a function in PowerShell.\u201d\n&gt;&gt;   }\n&gt;&gt;\nPS C:\\&gt; Say-Hello\nPS C:\\&gt; Hello World from a function in PowerShell.<\/pre>\n<p>\u53c2\u6570\u5217\u8868\u4e2d\u7684\u5706\u62ec\u53f7\u4e3a\u53ef\u9009\uff0c\u4e0d\u63a5\u6536\u53c2\u6570\u65f6\u53ef\u4ee5\u7701\u7565\uff0c\u5982\uff1a <\/p>\n<pre>PS C:\\&gt;function Say-Hello\n&gt;&gt;  {\n&gt;&gt;    Write-Host \u201cHello World from a function in PowerShell.\u201d\n&gt;&gt;   }\n&gt;&gt;\nPS C:\\&gt; Say-Hello\nPS C:\\&gt; Hello World from a function in PowerShell.<\/pre>\n<p>\u5b9a\u4e49\u51fd\u6570\u7684\u8fc7\u7a0b\u4e2d\u4f1a\u5728\u5f53\u524d\u4f5c\u7528\u57df\u4e2d\u81ea\u52a8\u521b\u5efa\u540c\u540d\u5bf9\u8c61\uff0c\u6240\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u65f6\u4f7f\u7528\u51fd\u6570\u540d\u5373\u53ef\uff0c\u5982\uff1a <\/p>\n<pre>PS C:\\&gt; Say-Hello\nPS C:\\&gt; Hello World from a function in PowerShell.<\/pre>\n<p>1.1 \u51fd\u6570\u4f53 <\/p>\n<p>\u5728\u4e0a\u4e00\u8282\u7684\u5b9e\u4f8b\u4e2d\u4f7f\u7528\u4e86Verb-Noun\u7684\u5f62\u5f0f\u547d\u540d\u51fd\u6570\uff0c\u8fd9\u79cd\u52a8\u8bcd-\u540d\u8bcd\u7684\u5f62\u5f0f\u662fPowerShell\u4e2d\u4e3a\u4e86\u6e05\u695a\u548c\u6613\u8bfb\u800c\u7ea6\u5b9a\u4fd7\u6210\u7684\u547d\u540d\u89c4\u5219\u3002\u51fd\u6570\u7684\u6267\u884c\u52a8\u4f5c\u7c7b\u4f3c\u4e8e\u5185\u5efa\u7684cmdlet\uff0c\u6240\u4ee5\u547d\u540d\u5f62\u5f0f\u4e5f\u662f\u7c7b\u4f3c\u7684\u3002 <\/p>\n<p>\u51fd\u6570\u6ce8\u518c\u5728\u5168\u5c40\u547d\u4ee4\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u5b83\u7684\u8c03\u7528\u548c\u5176\u4ed6cmdlet\u4e2d\u7684\u8c03\u7528\u76f8\u540c\u3002\u53ef\u4ee5\u4f7f\u7528Get-Command\u83b7\u53d6\u4e4b\u524d\u5b9a\u4e49\u51fd\u6570\u7684\u5f15\u7528\u3002\u4e0b\u4f8b\u83b7\u53d6\u4e4b\u524d\u5b9a\u4e49\u7684\u51fd\u6570\uff1a <\/p>\n<pre>PS C:\\&gt; $helloFunction = Get-Command Say-Hello -type Function\nPS C:\\&gt; $helloFunction.GetType().FullName\nSystem.Management.Automation.FunctionInfo<\/pre>\n<p>\u8fd9\u91cc\u83b7\u53d6\u7684\u662f\u8fd9\u4e2a\u51fd\u6570\u662f\u4e2aSystem.Management.Automation.FunctionInfo\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u7684\u6709\u7528\u5c5e\u6027\u5982\u4e0b; <\/p>\n<pre>PS C:\\&gt; $helloFunction | gm -type Property\n\n\n   TypeName: System.Management.Automation.FunctionInfo\n\nName                MemberType Definition\n----                ---------- ----------\nCmdletBinding       Property   System.Boolean CmdletBinding {get;}\nCommandType         Property   System.Management.Automation.\nCommandTypes CommandType {get;}\nDefaultParameterSet Property   System.String DefaultParameterSet {get;}\nDefinition          Property   System.String Definition {get;}\nDescription         Property   System.String Description {get;set;}\nModule              Property   System.Management.Automation.\nPSModuleInfo Module {get;}\nModuleName          Property   System.String ModuleName {get;}\nName                Property   System.String Name {get;}\nOptions             Property   System.Management.Automation.\nScopedItemOptions Options {get;set;}\nParameters          Property   System.Collections.Generic.\nDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Cu...\nParameterSets       Property   System.Collections.\nObjectModel.ReadOnlyCollection`1[[System.Management.\nAutomation.Com...\nScriptBlock         Property   System.Management.\nAutomation.ScriptBlock ScriptBlock {get;}\nVisibility          Property   System.Management.\nAutomation.SessionStateEntryVisibility Visibility \n{get;set;}<\/pre>\n<p>CommandType\u5c5e\u6027\u5df2\u7ecf\u662fFunction\uff0cName\u5c5e\u6027\u662fSay-Hello\u3002ScriptBlock\u5c5e\u6027\u53ef\u4ee5\u5728\u4e0d\u67e5\u770b\u6587\u6863\u7684\u60c5\u51b5\u4e0b\u663e\u793a\u5728\u5f53\u524d\u51fd\u6570\u4e2d\u5305\u542b\u7684\u529f\u80fd\u3002\u4e0b\u4f8b\u83b7\u53d6\u5f53\u524d\u51fd\u6570\u7684\u4fe1\u606f\uff1a <\/p>\n<pre>PS C:\\&gt; $helloFunction.ScriptBlock\n   Write-Host &quot; Hello World from a function in PowerShell.&quot;\nPS C:\\&gt; &amp;$helloFunction.ScriptBlock\nHello World from a function in PowerShell.<\/pre>\n<p>\u4e0a\u4f8b\u4e2d\u4f7f\u7528\u4e86\u8c03\u7528\u64cd\u4f5c\u7b26\uff08&amp;\uff09\uff0c\u53ef\u4ee5\u5728\u6267\u884c\u51fd\u6570\u4e4b\u524d\u68c0\u67e5\u51fd\u6570\u811a\u672c\u5757\u7684\u5185\u5bb9\u3002\u51fd\u6570\u5bf9\u8c61\u7684Definition\u5c5e\u6027\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u5305\u542b\u51fd\u6570\u4ee3\u7801\u3002\u4e0b\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u8fd9\u4e2a\u5c5e\u6027\uff1a <\/p>\n<pre>PS C:\\&gt; $helloFunction.Definition.GetType().FullName\nSystem.String\nPS C:\\&gt; $helloFunction.Definition\n   Write-Host &quot;Hello World from a function in PowerShell.&quot;<\/pre>\n<p>Definition\u5c5e\u6027\u5305\u542b\u6240\u6709\u6709\u6548\u7684\u53ef\u64cd\u4f5c\u4ee3\u7801\uff0c\u751a\u81f3\u53ef\u4ee5\u901a\u8fc7Invoke-Expression\u6267\u884c\u51fd\u6570\uff0c\u5982\uff1a <\/p>\n<pre>PS C:\\&gt; Invoke-Expression $helloFunction.Definition\nHello World from a function in PowerShell.<\/pre>\n<p>\u770b\u8d77\u6765\u4e0a\u4f8b\u7684\u6267\u884c\u65b9\u5f0f\u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\u5e76\u6ca1\u6709\u4f18\u52bf\uff0c\u4f46\u5728\u67d0\u4e9b\u573a\u5408\u4e2d\u786e\u5b9e\u5f88\u65b9\u4fbf\u3002 <\/p>\n<p>1.2 \u51fd\u6570\u53c2\u6570 <\/p>\n<p>\u4e3a\u4e86\u8ba9\u51fd\u6570\u63a5\u6536\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5b9a\u4e49\u4e2d\u6307\u5b9a\u53c2\u6570\u6e05\u5355\u3002\u4e0b\u4f8b\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u5e76\u5c06\u5176\u548c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff1a <\/p>\n<pre>PS C:\\&gt; function Write-Sum($first,$second)\n&gt;&gt; {\n&gt;&gt;   $sum = $first + $second\n&gt;&gt;   Write-Host &quot;Sum:$sum&quot;\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; Write-Sum 6 9\nSum:15<\/pre>\n<p>\u3010\u63d0\u793a\u3011 <\/p>\n<p>\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u9700\u8981\u7528\u6237\u5728\u62ec\u53f7\u4e2d\u63d0\u4f9b\u53c2\u6570\uff0c\u5e76\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u5982Write-Sum(6,9)\u3002\u5728PowerShell\u4e2d\u8c03\u7528\u51fd\u6570\u7684\u8bed\u6cd5\u4e0e\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u548ccmdlet\u76f8\u540c\uff0c\u5e76\u4ee5\u7a7a\u683c\u5206\u9694\u53c2\u6570\u5217\u8868\uff0c\u6216\u8005\u5bf9\u547d\u540d\u53c2\u6570\u4f7f\u7528\u57fa\u4e8e\u5f00\u5173\u7684\u8bed\u6cd5\uff0c\u5982Write-Sum \u2013first 6 \u2013second 9\u3002 <\/p>\n<p>\u5728PowerShell\u4e2d\u7528\u5706\u62ec\u53f7\u548c\u9017\u53f7\u7684\u5f62\u5f0f\u63d0\u4f9b\u53c2\u6570\u5bf9\u8c61\u65b9\u6cd5\uff0c\u5982$myObject.DoSometing(4,5)\u3002\u5c3d\u7ba1\u4e8c\u8005\u76f8\u4f3c\uff0c\u4f46\u662f\u5374\u6709\u5f88\u5927\u5dee\u522b\u3002 <\/p>\n<p>\u5728\u51fd\u6570\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u524d\u9762\u7ae0\u8282\u4e2d\u811a\u672c\u5757\u53c2\u6570\u4f7f\u7528\u7684\u7279\u6027\uff0c\u6240\u6709\u5728\u811a\u672c\u5757\u4e2d\u5bf9\u53c2\u6570\u6267\u884c\u7684\u64cd\u4f5c\u5728\u51fd\u6570\u53c2\u6570\u64cd\u4f5c\u4e2d\u5747\u53ef\u4f7f\u7528\u3002\u53ef\u4ee5\u6307\u5b9a\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u7a0b\u5e8f\u89e3\u91ca\u5668\u4f1a\u81ea\u52a8\u8f6c\u6362\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u3002\u4e0b\u4f8b\u5728\u51fd\u6570\u4e2d\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u5f62\u53c2\u6570\uff1a <\/p>\n<pre>PS C:\\&gt; function Write-Sum([int]$first, [int]$second)\n&gt;&gt; {\n&gt;&gt;  $sum = $first + $second\n&gt;&gt;  Write-Host &quot;Sum: $sum&quot;\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; Write-Sum 5 &quot;9&quot;\nSum: 14<\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u4f20\u9012\u67d0\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u7a0b\u5e8f\u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\u3002\u4e0b\u4f8b\u683c\u5f0f\u5316\u65e5\u671f\u5e76\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff1a <\/p>\n<pre>PS C:\\&gt; function Format-Date($date,  $format = &quot;{0:M\/d\/yyyy}&quot;)\n&gt;&gt; {\n&gt;&gt; Write-Host ($format -f $date)\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; Format-Date [datetime]::Today\n[datetime]::Today\nPS C:\\&gt; Format-Date ([datetime]::Today)\n2\/4\/2009<\/pre>\n<p>\u9ed8\u8ba4\u65e5\u671f\u683c\u5f0f\u662f\u201c\u6708\/\u65e5\/\u5e74\u201d\u3002\u5982\u679c\u8981\u4f7f\u7528\u5b8c\u6574\u683c\u5f0f\u7684\u65e5\u671f\u8f93\u51fa\uff0c\u53ef\u4ee5\u8986\u76d6\u4e0a\u4f8b\u4e2d\u7684\u683c\u5f0f\u53c2\u6570\uff1a <\/p>\n<pre>PS C:\\&gt; Format-Date ([datetime]::Today) &quot;{0:f}&quot;\n2009\u5e742\u67084\u65e5 0:00<\/pre>\n<p>\u53ef\u4ee5\u5f3a\u5236\u901a\u8fc7\u629b\u51fa\u5f02\u5e38\u7684\u5f62\u5f0f\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u7531\u51fd\u6570\u8c03\u7528\u8005\u4f20\u9012\u4e00\u4e2a\u53c2\u6570\u3002\u4e0b\u4f8b\u7684\u8f93\u5165\u65e5\u671f\u5f3a\u5236\u8981\u6c42\u53c2\u6570\uff1a <\/p>\n<pre>PS C:\\&gt; function Format-Date($date = $(throw &quot;Date required&quot;),`\n&gt;&gt;                       $format = &quot;{0:M\/d\/yyyy}&quot;)\n&gt;&gt; {\n&gt;&gt;         Write-Host ($format -f $date)\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; Format-Date\nDate required\nAt line:1 char:37\n+ function Format-Date($date = $(throw &lt;&lt;&lt;&lt;  &quot;Date required&quot;),`\n    + CategoryInfo          : OperationStopped: (Date required:String) [], RuntimeException\n    + FullyQualifiedErrorId : Date required<\/pre>\n<p>\u53ef\u4ee5\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\u8df3\u8fc7\u53c2\u6570\u58f0\u660e\uff0c\u800c\u5728\u51fd\u6570\u4f53\u4e2d\u58f0\u660e\u3002\u51fd\u6570\u4f53\u672c\u8eab\u4ee5\u811a\u672c\u5757\u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u53ef\u4ee5\u5305\u542bparam\u8bed\u53e5\u3002\u4e0b\u4f8b\u4e2d\u7684Format-Date\u51fd\u6570\u5728\u811a\u672c\u5757\u4e2d\u58f0\u660e\u53d8\u91cf\uff1a <\/p>\n<pre>PS C:\\PowerShell&gt; function Format-Date\n&gt;&gt; {\n&gt;&gt; param ($date = $(throw &quot;Date required&quot;), $format = &quot;{0:M\/d\/yyyy}&quot;)\n&gt;&gt; Write-Host ($format -f $date)\n&gt;&gt; }\n&gt;&gt;\nPS C:\\PowerShell&gt; Format-Date (Get-Item C:\\autoexec.bat).LastAccessTime\n3-26-2009\nPS C:\\PowerShell&gt;<\/pre>\n<p>1.3 \u901a\u8fc7\u5f15\u7528\u4f20\u9012\u53c2\u6570 <\/p>\n<p>\u7528\u53c2\u6570\u4f20\u9012\u6570\u636e\u7ed9\u51fd\u6570\u53ea\u662f\u5355\u65b9\u9762\u7684\uff0c\u4e0d\u80fd\u901a\u8fc7\u4e3a\u53c2\u6570\u8d4b\u503c\u800c\u6539\u53d8\u53c2\u6570\u7684\u539f\u6709\u503c\u3002\u4e3a\u53c2\u6570\u8d4b\u65b0\u503c\u65f6\uff0c\u53ea\u662f\u7b80\u5355\u5730\u5728\u672c\u5730\u521b\u5efa\u4e86\u540c\u540d\u53d8\u91cf\uff0c\u5982\uff1a <\/p>\n<pre>PS C:\\&gt; $name = &quot;LiMing&quot;\nPS C:\\&gt; function AssignValueToParam($name)\n&gt;&gt; {\n&gt;&gt; $name = &quot;WangLei&quot;\n&gt;&gt; Write-Host &quot;inside function: $name&quot;\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; AssignValueToParam $name\ninside function: WangLei\nPS C:\\&gt; Write-Host &quot;outside function: $name&quot;\noutside function: LiMing<\/pre>\n<p>\u65b0\u521b\u5efa\u7684\u53d8\u91cf\u4f1a\u5728\u5f53\u524d\u4f5c\u7528\u57df\u4e2d\u8986\u76d6\u4e4b\u524d\u4f20\u9012\u7684\u53c2\u6570\uff0c\u539f\u53c2\u6570\u503c\u4e0d\u53d8\uff0c\u4e3a\u6539\u53d8\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Get-Variable\u548cSet-Variable\u5728\u590d\u6742\u7684\u4f5c\u7528\u57df\u95f4\u66f4\u6539\u53d8\u91cf\u503c\u3002\u4e0b\u4f8b\u521b\u5efa\u7684\u51fd\u6570\u7528\u6765\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u503c\uff1a <\/p>\n<pre>PS C:\\&gt; function SwapValue($first, $second)\n&gt;&gt; {\n&gt;&gt;     $firstValue = Get-Variable $first -scope 1 -valueOnly\n&gt;&gt;     $secondValue = Get-Variable $second -scope 1 -valueOnly\n&gt;&gt;     Set-Variable $first $secondValue -scope 1\n&gt;&gt;     Set-Variable $second $firstValue -scope 1\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; $a = 5\nPS C:\\&gt; $b = 8\nPS C:\\&gt; SwapValue &quot;a&quot; &quot;b&quot;\nPS C:\\&gt; $a\n8\nPS C:\\&gt; $b\n5<\/pre>\n<p>\u4e0a\u4f8b\u4e2d\u867d\u7136\u8fbe\u5230\u4e86\u9884\u5b9a\u7684\u529f\u80fd\uff0c\u4f46\u662f\u4ee3\u7801\u5f53\u4e2d\u662f\u5b58\u5728\u5c40\u9650\u6027\u7684\u3002\u4ea4\u6362\u503c\u7684\u53d8\u91cf\u5b9a\u4e49\u5728\u7236\u4f5c\u7528\u57df\u4e2d\uff0c\u5982\u679c\u8981\u4ea4\u6362\u7236\u4f5c\u7528\u57df\u4e2d\u7684\u4e24\u4e2a\u53d8\u91cf\uff0c\u5219\u65e0\u6548\u3002 <\/p>\n<p>\u5728PowerShell\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528PSReference\u5bf9\u8c61\u7684\u53d8\u91cf\uff0c\u8fd9\u6837\u53ef\u4ee5\u4f7f\u7528[ref]\u7684\u7c7b\u578b\u6807\u8bb0\u6765\u5c06\u4efb\u4f55\u53d8\u91cf\u8f6c\u6362\u4e3aPSReference\u5bf9\u8c61\uff0c\u4ece\u800c\u4fee\u6539Value\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u5f15\u7528\u5bf9\u8c61\u7684value\u5c5e\u6027\u5c06\u4fee\u6539\u539f\u59cb\u7684\u53d8\u91cf\u3002\u4e0b\u4f8b\u4f7f\u7528\u5f15\u7528\u65b9\u5f0f\u91cd\u5199\u4e0a\u9762\u7684SwapValue\u51fd\u6570\uff1a <\/p>\n<pre>PS C:\\&gt; function SwapValue([ref] $first,[ref] $second)\n&gt;&gt; {\n&gt;&gt; $tmp = $first.Value\n&gt;&gt; $first.Value = $second.Value\n&gt;&gt; $second.Value = $tmp\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; $a = 6\nPS C:\\&gt; $b = 3\nPS C:\\&gt; SwapValue ([ref] $a) ([ref]$b)\nPS C:\\&gt; $a\n3\nPS C:\\&gt; $b\n6<\/pre>\n<p>\u5728\u5f15\u7528\u53d8\u91cf\u7684\u8fc7\u7a0b\u4e2d\u5e76\u6ca1\u6709\u9650\u5b9a\u641c\u7d22\u7684\u4f5c\u7528\u57df\uff0c\u8fd9\u6837\u4ee3\u7801\u7684\u4f7f\u7528\u8303\u56f4\u66f4\u5e7f\u3002\u4ee3\u7801\u4e2d\u8c03\u7528SwapValue\u65f6\u4f7f\u7528\u5706\u62ec\u53f7\u4f7f\u5f97\u53d8\u91cf\u7684[ref]\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u5728\u5c06\u5bf9\u8c61\u4f20\u9012\u7ed9\u51fd\u6570\u4e4b\u524d\u5b8c\u6210\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u5bf9\u8c61\u7c7b\u578b\u7684\u6709\u6548\u3002 <\/p>\n<p>1.4 \u8fd4\u56de\u503c <\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4ece\u51fd\u6570\u4e2d\u8f93\u51fa\u672a\u88ab\u4efb\u4f55\u64cd\u4f5c\u9500\u6bc1\u7684\u5bf9\u8c61\u6765\u8fd4\u56de\u503c\uff0c\u8f93\u51fa\u591a\u4e2a\u5bf9\u8c61\u5c06\u4f1a\u8fd4\u56de\u5305\u542b\u591a\u4e2a\u5bf9\u8c61\u7684\u96c6\u5408\u3002\u4e0b\u9762\u7684\u51fd\u6570\u4e2d\u5728\u5faa\u73af\u4e2d\u8f93\u51fa\u591a\u4e2a\u5bf9\u8c61\uff1a <\/p>\n<pre>PS C:\\&gt; function Generate-NumberTo($max)\n&gt;&gt; {\n&gt;&gt;     for($i=0; $i -lt $max; $i++)\n&gt;&gt;     {\n&gt;&gt;         $i\n&gt;&gt;     }\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; Generate-NumberTo 4\n0\n1\n2\n3<\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528return\u8bed\u53e5\u5728\u9000\u51fa\u51fd\u6570\u7684\u540c\u65f6\u8fd4\u56de\u503c\uff0c\u4e0b\u4f8b\u4e2d\u7684\u51fd\u6570\u5728\u96c6\u5408\u4e2d\u641c\u7d22\u5bf9\u8c61\uff1a <\/p>\n<pre>PS C:\\&gt; function Find-Object($target, $haystack)\n&gt;&gt; {\n&gt;&gt;     foreach ($item in $haystack)\n&gt;&gt;     {\n&gt;&gt;         if($item -eq $target)\n&gt;&gt;         {\n&gt;&gt;             return $item\n&gt;&gt;         }\n&gt;&gt;     }\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; Find-Object 5 (2..19)\n5\nPS C:\\&gt; Find-Object 5 (7..19)\nPS C:\\&gt;<\/pre>\n<p>\u9700\u8981\u5f3a\u8c03\u7684\u662f\u8f93\u51fa\u5bf9\u8c61\u4e0e\u5c06\u5bf9\u8c61\u5199\u5230\u63a7\u5236\u53f0\u4e4b\u95f4\u7684\u533a\u522b\uff0c\u524d\u8005\u662f\u5c06\u5bf9\u8c61\u4f20\u9012\u5230\u8f93\u51fa\u6d41\u4e2d\u7684\u52a8\u4f5c\u3002\u53ef\u4ee5\u7528\u53d8\u91cf\u4fdd\u5b58cmdlet\u8fd4\u56de\u7684\u5bf9\u8c61\uff0c\u6216\u8c03\u7528\u547d\u4ee4\u5c06\u5bf9\u8c61\u5199\u5230\u63a7\u5236\u53f0\u3002\u4e0b\u4f8b\u628a\u5bf9\u8c61\u5199\u5230\u63a7\u5236\u53f0\uff1a <\/p>\n<pre>PS C:\\&gt; function Get-TextFiles()\n&gt;&gt; {\n&gt;&gt;     dir *.txt\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; Get-TextFiles\n\n    Directory: C:\\\n\nMode                LastWriteTime     Length Name\n----                -------------     ------ ----\n-a---          2009\/1\/3     14:31         12 digit.txt\n-a---          2009\/1\/3      6:06      12100 largetext.txt\n-a---          2009\/1\/3      6:07       6949 smalltext.txt\n-ar--          2009\/1\/3      6:27         13 test.txt\n-a---          2009\/1\/3      6:28          9 test2.txt<\/pre>\n<p>\u8f93\u51fa\u5bf9\u8c61\u65f6\uff0cPowerShell\u4f1a\u4f20\u9012\u6240\u6709\u8f93\u51fa\u5bf9\u8c61\u5230\u7ba1\u9053\u4e2d\u7684\u4e0b\u4e00\u4e2a\u547d\u4ee4\u3002 <\/p>\n<p>\u5c06\u5bf9\u8c61\u5199\u5230\u63a7\u5236\u53f0\u662f\u628a\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6587\u672c\u540e\u5199\u5230\u63a7\u5236\u53f0\uff0c\u4e0d\u4f1a\u4f20\u9012\u503c\u5230\u7ba1\u9053\u4e2d\uff0c\u5b83\u901a\u8fc7Wrist-Host\u8fd9\u4e2acmdlet\u5b9e\u73b0\u3002\u8f93\u51fa\u5bf9\u8c61\u548c\u5c06\u5bf9\u8c61\u5199\u5230\u63a7\u5236\u53f0\u770b\u8d77\u6765\u76f8\u4f3c\u662f\u56e0\u4e3a\u6bcf\u4e2a\u7ba1\u9053\u4f1a\u5728\u6700\u540e\u9690\u5f0f\u8c03\u7528Out-Default\uff0c\u8fd9\u4e2acmdlet\u5904\u7406\u6240\u6709\u8f93\u5165\u5e76\u5c06\u5176\u5199\u5230\u63a7\u5236\u53f0\u3002 <\/p>\n<p>1.5 \u4f5c\u7528\u57df\u89c4\u5219 <\/p>\n<p>\u51fd\u6570\u4f1a\u521b\u5efa\u65b0\u7684\u672c\u5730\u4f5c\u7528\u57df\uff0c\u4f5c\u7528\u57df\u7ee7\u627f\u53d8\u91cf\u7684\u53ef\u89c1\u6027\uff0c\u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6\u6240\u6709\u5176\u4f5c\u7528\u57df\u4e2d\u53ca\u5176\u7236\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u3002 <\/p>\n<p>\u5bf9\u4e8e\u547d\u540d\u5bf9\u8c61\uff0c\u51fd\u6570\u9075\u5faa\u7c7b\u4f3c\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u89c4\u5219\u3002\u53ef\u4ee5\u5728\u4efb\u4f55\u4f5c\u7528\u57df\u4e2d\u58f0\u660e\u51fd\u6570\uff0c\u51fd\u6570\u4f1a\u5728\u5176\u4f5c\u7528\u57df\u53ca\u5176\u5b50\u4f5c\u7528\u57df\u6709\u6548\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u5d4c\u5957\u51fd\u6570\u3002\u4e0b\u4f8b\u662f\u5d4c\u5957\u51fd\u6570\uff1a <\/p>\n<pre>PS C:\\&gt; function OuterFunction()\n&gt;&gt; {\n&gt;&gt;         function InnerFunction()\n&gt;&gt;         {\n&gt;&gt;             Write-Host &quot;Printed by InnerFunction!&quot;\n&gt;&gt;         }\n&gt;&gt;         InnerFunction\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; OuterFunction\nPrinted by InnerFunction!<\/pre>\n<p>InnerFunction\u51fd\u6570\u5bf9\u4e8eOuterFunction\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801\u53ef\u89c1\uff0c\u8fd9\u6837\u80fd\u6709\u6548\u5730\u4fdd\u62a4\u51fd\u6570\u5185\u90e8\u7279\u5b9a\u7684\u903b\u8f91\u4e0d\u88ab\u5916\u90e8\u8bbf\u95ee\u3002\u5982\u679c\u5f3a\u884c\u5728OuterFunction\u51fd\u6570\u5916\u90e8\u4f5c\u7528\u57df\u8c03\u7528InnerFunction\u51fd\u6570\uff0c\u5c06\u4f1a\u62a5\u9519\uff0c\u5982\uff1a <\/p>\n<pre>PS C:\\&gt; InnerFunction\nThe term 'InnerFunction' is not recognized as a cmdlet, \nfunction, operable program, or script file. \nVerify the term and\n try again.\nAt line:1 char:14\n+ InnerFunction &lt;&lt;&lt;&lt;\n    + CategoryInfo          : ObjectNotFound: \n(InnerFunction:String) [], CommandNotFoundException\n    + FullyQualifiedErrorId : CommandNotFoundException<\/pre>\n<p>\u7c7b\u4f3c\u53d8\u91cf\u8d4b\u503c\uff0c\u5728\u5b50\u4f5c\u7528\u57df\u4e2d\u7684\u51fd\u6570\u4f1a\u8986\u76d6\u7236\u4f5c\u7528\u57df\u4e2d\u7684\u540c\u540d\u51fd\u6570\uff0c\u4e0b\u4f8b\u5728\u5f53\u524d\u4f5c\u7528\u57df\u4e2d\u8986\u76d6\u7236\u4f5c\u7528\u57df\u4e2d\u7684\u540c\u540d\u51fd\u6570\uff1a <\/p>\n<pre>PS C:\\&gt; function OuterFunction()\n&gt;&gt; {\n&gt;&gt;        function Do-Something()\n&gt;&gt;         {\n&gt;&gt;             Write-Host &quot;Original Do-Something function&quot;\n&gt;&gt;          }\n&gt;&gt;        function InnerFunction()\n&gt;&gt;         {\n&gt;&gt;             function Do-Something()\n&gt;&gt;             {\n&gt;&gt;                 Write-Host &quot;Overriden Do-Something function&quot;\n&gt;&gt;             }\n&gt;&gt;             Do-Something\n&gt;&gt;         }\n&gt;&gt;         InnerFunction\n&gt;&gt;         Do-Something\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; OuterFunction\nOverriden Do-Something\nOriginal Do-Something function<\/pre>\n<p>OuterFunction\u51fd\u6570\u5b9a\u4e49\u4e86Do-Something\u51fd\u6570\uff0c\u5176\u4e2d\u5d4c\u5957\u4e86InnerFunction\u51fd\u6570\u3002\u8be5\u51fd\u6570\u4e2d\u5b9a\u4e49\u4e86\u4e0e\u7236\u4f5c\u7528\u57df\u4e2d\u7684Do-Something\u51fd\u6570\uff0c\u8986\u76d6\u4e86\u7236\u4f5c\u7528\u57df\u4e2d\u7684\u540c\u540d\u51fd\u6570\u3002\u8fd9\u79cd\u8986\u76d6\u53ea\u5728InnerFunction\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u6709\u6548\uff0c\u4e00\u65e6\u9000\u51fa\u8fd9\u4e2a\u4f5c\u7528\u57df\uff0cDo-Something\u8fd4\u56de\u539f\u503c\u3002 <\/p>\n<p>\u4e0eGet-Variable\u4e0d\u540c\uff0cGet-Command\u4e0d\u652f\u6301\u4f5c\u7528\u57df\u53c2\u6570\u3002\u4e00\u65e6\u5728\u5f53\u524d\u4f5c\u7528\u57df\u4e2d\u8986\u76d6\uff0c\u5219\u65e0\u6cd5\u4ece\u7236\u4f5c\u7528\u57df\u4e2d\u5f97\u5230\u51fd\u6570\u3002 <\/p>\n<p>\u53e6\u5916\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u540d\u524d\u4f7f\u7528global\u3001script\u3001local\u6216private\u4f5c\u7528\u57df\u6807\u8bc6\u7b26\u3002\u4e0b\u4f8b\u5728global\u548clocal\u4f5c\u7528\u57df\u4e2d\u58f0\u660e\u540c\u540d\u51fd\u6570\uff0c\u7136\u540e\u7528\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u533a\u522b\u4e8c\u8005\uff1a <\/p>\n<pre>PS C:\\&gt; function global:Do-Something()\n&gt;&gt; {\n&gt;&gt;     Write-Host &quot;Global Do-Something&quot;\n&gt;&gt; }\n&gt;&gt; function InnerScope()\n&gt;&gt; {\n&gt;&gt;      function local:Do-Something()\n&gt;&gt;      {\n&gt;&gt;         Write-Host &quot;Local Do-Something&quot;\n&gt;&gt;      }\n&gt;&gt;     local:Do-Something\n&gt;&gt;     global:Do-Something\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; InnerScope\nLocal Do-Something\nGlobal Do-Something<\/pre>\n<p>\u3010\u63d0\u793a\u3011 <\/p>\n<p>\u7528\u4e8e\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u547d\u540d\u7a7a\u95f4\u524d\u7f00\u5bf9\u51fd\u6570\u6709\u6548\uff0c\u5982\u679c\u8981\u8c03\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u5219\u5fc5\u987b\u4f7f\u7528$global:myVariable\u8bed\u6cd5\u3002\u800c\u51fd\u6570\u7684\u4f5c\u7528\u57df\u547d\u540d\u7a7a\u95f4\u4e0d\u4f7f\u7528\u7f8e\u5143\u7b26\uff0c\u5f15\u7528\u5168\u5c40\u51fd\u6570\u7684\u8bed\u6cd5\u683c\u5f0f\u662fglobal:myFunction\u3002 <\/p>\n<p>\u5f53\u5728\u4e0d\u540c\u4f5c\u7528\u57df\u7ea7\u522b\u91cd\u8f7d\u672c\u5730\u51fd\u6570\u540e\uff0c\u53ea\u8981\u5728\u5f53\u524d\u4f5c\u7528\u57df\u7684\u51fd\u6570\u88ab\u91cd\u8f7d\u524d\u83b7\u5f97\u51fd\u6570\u7684\u5f15\u7528\uff0c\u5c31\u53ef\u4ee5\u4ece\u7236\u4f5c\u7528\u57df\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5982\u4e0b\u4f8b\uff1a <\/p>\n<pre>PS C:\\&gt; function OuterFunction()\n&gt;&gt; {\n&gt;&gt;     function Do-Something()\n&gt;&gt;     {\n&gt;&gt;         Write-Host &quot;Original Do-Something&quot;\n&gt;&gt;     }\n&gt;&gt;     function InnerFunction()\n&gt;&gt;     {\n&gt;&gt;         $original = Get-Command Do-Something -type Function\n&gt;&gt;         function Do-Something()\n&gt;&gt;         {\n&gt;&gt;             Write-Host &quot;Override Do-Something&quot;\n&gt;&gt;             Write-Host &quot;Calll original function......&quot;\n&gt;&gt;             &amp;$original\n&gt;&gt;         }\n&gt;&gt;         Do-Something\n&gt;&gt;     }\n&gt;&gt;     InnerFunction\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; OuterFunction\nOverride Do-Something\nCalll original function......\nOriginal Do-Something<\/pre>\n<p>2 \u8fc7\u6ee4\u5668 <\/p>\n<p>\u5728\u51fd\u6570\u4e2d\u63a5\u6536\u7ba1\u9053\u8f93\u5165\u9700\u8981\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49begin\u3001process\u548cend\u6bb5\uff0c\u5f53\u5bf9\u8c61\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u8fd9\u4e9b\u6bb5\u5c31\u4f1a\u6267\u884c\u30023\u4e2a\u6bb5\u4e2d\u53ea\u6709process\u662f\u5fc5\u987b\u7684\uff0c\u5b83\u4f5c\u7528\u4e8e\u4f20\u9012\u7684\u6bcf\u4e2a\u5bf9\u8c61\uff0c\u901a\u5e38\u5728\u5176\u4e2d\u53ef\u4ee5\u4f7f\u7528$_\u8fd9\u4e2a\u7279\u6b8a\u53d8\u91cf\u5f15\u7528\u5f53\u524d\u5bf9\u8c61\u3002begin\u548cend\u6bb5\u5206\u522b\u5728\u7ba1\u9053\u6267\u884c\u524d\u540e\u6267\u884c\u3002\u4e0b\u4f8b\u4e3a\u5b9a\u4e49\u63a5\u6536\u6587\u4ef6\u7684\u7ba1\u9053\u5e76\u8ba1\u7b97\u6587\u4ef6\u603b\u5927\u5c0f\u7684\u51fd\u6570\uff1a <\/p>\n<pre>PS C:\\&gt; function Get-FileSize\n&gt;&gt; {\n&gt;&gt;     begin\n&gt;&gt;     {\n&gt;&gt;         $total = 0\n&gt;&gt;     }\n&gt;&gt;     process\n&gt;&gt;      {\n&gt;&gt;         Write-Host &quot;processing: $($_.name)&quot;\n&gt;&gt;         $total += $_.Length\n&gt;&gt;     }\n&gt;&gt;     end\n&gt;&gt;     {\n&gt;&gt;         return $total\n&gt;&gt;     }\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; dir *.txt | Get-FileSize\nprocessing: digit.txt\nprocessing: largetext.txt\nprocessing: smalltext.txt\nprocessing: test.txt\nprocessing: test2.txt\n19083<\/pre>\n<p>\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u5728\u5904\u7406\u7ba1\u9053\u8f93\u5165\u65f6\u4ec5\u9700\u8981\u5b9a\u4e49process\u6bb5\uff0c\u5982\u8fc7\u6ee4\u8fdb\u7a0b\u7684\u96c6\u5408\u5e76\u53ea\u663e\u793a\u542f\u52a8\u4e0d\u8d85\u8fc75\u5206\u949f\u7684\u8fdb\u7a0b\u7b49\u3002\u4e3a\u6b64\u9700\u8981\u5b9a\u4e49Get-RecentlyStarted\u51fd\u6570\uff0c\u5728process\u6bb5\u4e2d\u8fd4\u56deStartTime\u5c5e\u6027\u503c\u5c0f\u4e8e5\u5206\u949f\u7684\u7ba1\u9053\u5bf9\u8c61\uff1a <\/p>\n<pre>PS C:\\&gt; function Get-RecentlyStarted\n&gt;&gt; {\n&gt;&gt;     process\n&gt;&gt;     {\n&gt;&gt;         $start = $_.StartTime\n&gt;&gt;         if ($start -ne $null)\n&gt;&gt;         {\n&gt;&gt;             $now = [datetime]::Now\n&gt;&gt;             $diff = $now - $start\n&gt;&gt;             if ($diff.TotalMinutes -lt 5)\n&gt;&gt;             {\n&gt;&gt;                 return $_\n&gt;&gt;             }\n&gt;&gt;         }\n&gt;&gt;     }\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; Get-Process | Get-RecentlyStarted\n\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName\n-------  ------    -----      ----- -----   ------     -- -----------\n    291      10    10240      17836   101    19.38   3324 iexplore\n     80       4     2740       7252    67     2.73   3400 mspaint\n     45       2     1192       3952    54     1.09   1892 notepad\n    234       8     3336       5072    51     2.28   2440 svchost<\/pre>\n<p>PowerShell\u4e0d\u5141\u8bb8\u51fd\u6570\u540c\u65f6\u5305\u542b\u5728\u8c03\u7528\u65f6\u7acb\u5373\u6267\u884c\u7684\u4efb\u610f\u8bed\u53e5\uff0c\u4ee5\u53ca\u4e0e\u7ba1\u9053\u76f8\u5173\u7684begin\u3001process\u548cend\u6bb5\u3002PowerShell\u5e38\u89c4\u51fd\u6570\u548c\u7ba1\u9053\u5904\u7406\u51fd\u6570\u5b8c\u5168\u4e0d\u540c\uff0c\u7ba1\u9053\u51fd\u6570\u6a21\u5f0f\u7ecf\u5e38\u7528\u4e8e\u8fc7\u6ee4\u96c6\u5408\u3002\u4e3a\u4e86\u4fbf\u4e8e\u5b9a\u4e49\u8fc7\u6ee4\u51fd\u6570\uff0c\u53ef\u4ee5\u5f15\u7528fliter\u5173\u952e\u5b57\u3002\u4e0b\u4f8b\u4f7f\u7528\u8fc7\u6ee4\u5668\u91cd\u65b0\u5b9a\u4e49\u4e0a\u4e00\u4e2a\u5b9e\u4f8b\uff1a <\/p>\n<pre>PS C:\\&gt; filter Get-RecentlyStarted\n&gt;&gt; {\n&gt;&gt;     $start = $_.StartTime\n&gt;&gt;     if ($start -ne $null)\n&gt;&gt;     {\n&gt;&gt;         $now = [datetime]::Now\n&gt;&gt;         $diff = $now - $Start\n&gt;&gt;         if ($diff.TotalMinutes -lt 5)\n&gt;&gt;         {\n&gt;&gt;             return $_\n&gt;&gt;         }\n&gt;&gt;     }\n&gt;&gt; }\n&gt;&gt;\nPS C:\\&gt; Get-Process |  Get-RecentlyStarted\n\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName\n-------  ------    -----      ----- -----   ------     -- -----------\n    189       8     5452      11076    86     2.63   3368 iexplore\n     80       4     2740       7228    67     1.58   2408 mspaint\n    229       8     3308       5076    51     2.28   2440 svchost\n     82       4     2920       8192    71     2.84   2892 wordpad<\/pre>\n<p>\u4f7f\u7528\u8fc7\u6ee4\u5668\u66ff\u4ee3\u51fd\u6570\u4f1a\u6e05\u9664\u5d4c\u5957\u7684\u590d\u6742\u5ea6\u800c\u4f7f\u4ee3\u7801\u66f4\u4e3a\u7b80\u6d01\u4e14\u6613\u8bfb\uff0c\u53ef\u4ee5\u4f7f\u7528Get-Command\u67e5\u770bGet-RecentlyStarted\u7684\u8be6\u7ec6\u4fe1\u606f\u3002PowerShell\u89c4\u5b9a\u8fc7\u6ee4\u5668\u662f\u51fd\u6570\u7684\u7279\u4f8b\uff0c\u56e0\u4e3a\u4f7f\u7528Function\u548cFilter\u7c7b\u578b\u67e5\u627e\u547d\u4ee4\u90fd\u4f1a\u8fd4\u56de\u524d\u9762\u7684\u8fc7\u6ee4\u5668\uff0c\u5982\uff1a <\/p>\n<pre>PS C:\\&gt; Get-Command Get-RecentlyStarted -type Function\n\nCommandType     Name                                                Definition\n-----------     ----                                                ----------\nFunction        Get-RecentlyStarted                                 ...\n\nPS C:\\&gt; Get-Command Get-RecentlyStarted -type Filter\n\nCommandType     Name                                                Definition\n-----------     ----                                                ----------\nFunction        Get-RecentlyStarted                                 ...<\/pre>\n<p>\u4e0a\u4f8b\u4e2d\u7684Definition\u5c5e\u6027\u88ab\u7701\u7565\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u83b7\u53d6\uff1a <\/p>\n<pre>PS C:\\&gt; (Get-Command Get-RecentlyStarted -type Filter).ScriptBlock\n\n    $start = $_.StartTime\n    if ($start -ne $null)\n    {\n        $now = [datetime]::Now\n        $diff = $now - $Start\n        if ($diff.TotalMinutes -lt 5)\n        {\n            return $_\n        }\n    }<\/pre>\n<p>\u4e0e\u7ba1\u9053\u914d\u5408\u5de5\u4f5c\u7684\u51fd\u6570\u4e0e\u8fc7\u6ee4\u5668\u770b\u8d77\u6765\u76f8\u4f3c\uff0c\u5c3d\u7ba1\u51fd\u6570\u7684process\u5757\u8bed\u4e49\u7b49\u540c\u4e8e\u8fc7\u6ee4\u5668\uff0c\u4f46\u662f\u51fd\u6570\u5728\u5185\u90e8\u4ee5FunctionInfo\u5bf9\u8c61\u5b58\u5728\uff1b\u800c\u8fc7\u6ee4\u5668\u4ee5FilterInfo\u5bf9\u8c61\u5b58\u5728\u3002 <\/p>\n<p>\u4e0b\u4f8b\u4e2d\u7684\u51fd\u6570\u4f1a\u8fc7\u6ee4\u6389\u7ba1\u9053\u4e2d\u6240\u6709\u7684\u5947\u6570\uff0c\u53ea\u4fdd\u7559\u5076\u6570\uff1a <\/p>\n<pre>PS C:\\&gt; function Even-Function\n&gt;&gt; {\n&gt;&gt;     process\n&gt;&gt;     {\n&gt;&gt;         if ($_ % 2 -eq 0)\n&gt;&gt;         {\n&gt;&gt;             $_\n&gt;&gt;         }\n&gt;&gt;     }\n&gt;&gt; }\n&gt;&gt;<\/pre>\n<p>\u4e0b\u4f8b\u4f7f\u7528filter\u5b9e\u73b0\u540c\u6837\u7684\u903b\u8f91\uff1a <\/p>\n<pre>PS C:\\&gt; filter Even-Filter\n&gt;&gt; {\n&gt;&gt;     if ($_ %2 -eq 0)\n&gt;&gt;     {\n&gt;&gt;         $_\n&gt;&gt;     }\n&gt;&gt; }\n&gt;&gt;<\/pre>\n<p>\u5c3d\u7ba1Even-Function\u548cEven-Filter\u5728\u67e5\u8be2\u51fd\u6570\u7c7b\u578b\u7684\u547d\u4ee4\u65f6\u5747\u8fd4\u56de\uff0c\u4f46\u662f\u5bf9\u4e8eCommandType\u5c5e\u6027\uff0c\u4e8c\u8005\u5206\u522b\u8fd4\u56deFunction\u548cFilter\uff0c\u5982\uff1a <\/p>\n<pre>PS C:\\&gt; Get-Command Even-Function -CommandType Function\nCommandType    Name                        Definition\n-----------         ----                          ----------\nFunction         Event-Function                process {...\nPS C:\\&gt; Get-Command Even-Filter -CommandType Function\n\nCommandType    Name                        Definition\n-----------         ----                          ----------\nFilter              Even-Filter                   ...\n\u53e6\u5916\u4e00\u79cd\u5728\u8fd0\u884c\u65f6\u533a\u522b\u51fd\u6570\u548c\u8fc7\u6ee4\u5668\u7684\u65b9\u6cd5\u662f\u68c0\u67e5\u5185\u90e8ScriptBlock\u5bf9\u8c61\u7684IsFilter\u5c5e\u6027\uff0c\u5982\uff1a\nPS C:\\&gt; $function = (Get-Command Event-Function -CommandType Function)\nPS C:\\&gt; $function.ScriptBlock.IsFilter\nFalse\nPS C:\\&gt; $function = (Get-Command Even-Filter -CommandType Function)\nPS C:\\&gt; $function.ScriptBlock.IsFilter\nTrue<\/pre>\n<p>\u9700\u8981\u518d\u6b21\u91cd\u7533\uff0c\u8fd9\u4e24\u79cd\u51fd\u6570\u5bf9\u8c61\u4ee5\u5b8c\u5168\u76f8\u540c\u7684\u65b9\u5f0f\u5de5\u4f5c\u3002 <\/p>\n<p>\u8d5b\u8fea\u7f51\u5730\u5740\uff1a<a title=\"http:\/\/news.ccidnet.com\/art\/32857\/20100617\/2089211_1.html\" href=\"http:\/\/news.ccidnet.com\/art\/32857\/20100617\/2089211_1.html\">http:\/\/news.ccidnet.com\/art\/32857\/20100617\/2089211_1.html<\/a><\/p>\n<p>\u4f5c\u8005: \u4ed8\u6d77\u519b<br \/>\n  <br \/>\u7248\u6743\uff1a\u672c\u6587\u7248\u6743\u5f52\u4f5c\u8005\u6240\u6709<\/p>\n<p>\u8f6c\u8f7d\uff1a\u6b22\u8fce\u8f6c\u8f7d\uff0c\u4e3a\u4e86\u4fdd\u5b58\u4f5c\u8005\u7684\u521b\u4f5c\u70ed\u60c5\uff0c\u8bf7\u6309\u8981\u6c42\u3010\u8f6c\u8f7d\u3011\uff0c\u8c22\u8c22<\/p>\n<p>\u8981\u6c42\uff1a\u672a\u7ecf\u4f5c\u8005\u540c\u610f\uff0c\u5fc5\u987b\u4fdd\u7559\u6b64\u6bb5\u58f0\u660e\uff1b\u5fc5\u987b\u5728\u6587\u7ae0\u4e2d\u7ed9\u51fa\u539f\u6587\u8fde\u63a5\uff1b\u5426\u5219\u5fc5\u7a76\u6cd5\u5f8b\u8d23\u4efb <\/p>\n<p>\u4e2a\u4eba\u7f51\u7ad9: <a href=\"http:\/\/txj.shell.tor.hu\/\">http:\/\/txj.shell.tor.hu\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8ba8\u8bba\u4e86PowerShell\u63d0\u4f9b\u7684\u4e1a\u52a1\u63a7\u5236\u673a\u5236\u51fd\u6570\u548c\u8fc7\u6ee4\u5668\uff0c\u51fd\u6570\u662f\u7528\u6765\u6269\u5c55\u5185\u7f6eshell\u529f\u80fd\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5c3d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-33113","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33113"}],"collection":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33113"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33113\/revisions"}],"predecessor-version":[{"id":33803,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33113\/revisions\/33803"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}