{"id":33520,"date":"2013-10-06T17:32:03","date_gmt":"2013-10-06T16:32:03","guid":{"rendered":"http:\/\/www.fuhaijun.com\/?p=33520"},"modified":"2024-11-06T11:18:46","modified_gmt":"2024-11-06T03:18:46","slug":"powershell%e5%ae%9a%e6%97%b6%e6%8a%93%e5%8f%96%e5%b1%8f%e5%b9%95%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33520","title":{"rendered":"PowerShell\u5b9a\u65f6\u6293\u53d6\u5c4f\u5e55\u56fe\u50cf"},"content":{"rendered":"<p>&#160;&#160;&#160;&#160; \u6628\u5929\u7684\u535a\u6587\u5199\u4e86\u5b9a\u65f6\u8bb0\u5f55\u64cd\u4f5c\u7cfb\u7edf\u884c\u4e3a\uff0c\u5176\u5b9e\u8bf4\u767d\u4e86\u5c31\u662f\u6293\u53d6\u4e86\u51fb\u952e\u7684\u8bb0\u5f55\u548c\u5bf9\u5e94\u7a97\u53e3\u7684\u6807\u9898\u680f\uff0c\u800c\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\u6807\u9898\u680f\u53c8\u5305\u542b\u5f53\u65f6\u8bb0\u5f55\u7684\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\uff0c\u7528\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5927\u81f4\u8bb0\u5f55\u64cd\u4f5c\u4e86\u54ea\u4e9b\u7a0b\u5e8f\uff0c\u6253\u5f00\u4e86\u54ea\u4e9b\u6587\u4ef6\uff0c\u4ee5\u53ca\u6572\u51fb\u4e86\u54ea\u4e9b\u6309\u952e\u3002\u4e8b\u5b9e\u4e0a\u8fd9\u6837\u8bb0\u5f55\u64cd\u4f5c\u7cfb\u7edf\u7684\u884c\u4e3a\u663e\u5f97\u76f8\u5bf9\u5355\u8584\u4e00\u70b9\uff0c\u56e0\u4e3a\u8bb0\u5f55\u7684\u5185\u5bb9\u4e0d\u592a\u5f62\u8c61\uff0c\u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\u592a\u8fc7\u4e8e\u9690\u6666\u4e86\uff0c\u5bf9\u4e8e\u4eba\u7c7b\u6765\u8bf4\uff0c\u56fe\u50cf\u4f1a\u6bd4\u6587\u5b57\u66f4\u52a0\u6709\u5229\u4e8e\u7528\u6237\u7406\u89e3\u3002\u5f53\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u65b9\u4fbf\u88c5\u5c4f\u5e55\u8bb0\u5f55\u8f6f\u4ef6\uff0c\u4f46\u53c8\u9700\u8981\u770b\u5df2\u7ecf\u767b\u5f55\u7528\u6237\u5728\u5e72\u4ec0\u4e48\u7684\u65f6\u5019\uff0c\u7528PowerShell\u7684\u811a\u672c\u6765\u5b9e\u73b0\u5b9a\u65f6\u6293\u53d6\u56fe\u50cf\u7684\u65b9\u5f0f\u8bb0\u5f55\u64cd\u4f5c\uff0c\u67e5\u770b\u56fe\u50cf\u5c31\u77e5\u9053\u767b\u9646\u7528\u6237\u505a\u4e86\u4ec0\u4e48\uff0c\u5f53\u7136\u4f60\u5b58\u653e\u56fe\u7247\u7684\u76ee\u5f55\u8981\u9690\u853d\uff0c\u4e0d\u8981\u8ba9\u7528\u6237\u53d1\u73b0\u4e86\u4e3a\u597d\u3002<\/p>\n<p>&#160;&#160;&#160;&#160; \u5f53\u7136\u5bf9\u4e8e\u5728\u5b66\u6821\u8ba1\u7b97\u673a\u7cfb\u7684\u5c4c\u4e1d\u4eec\uff0c\u8fd9\u4e2a\u529f\u80fd\u4e5f\u53ef\u4ee5\u7528\u6765\u4e86\u89e3\u81ea\u5df1\u7684\u5973\u795e\u6709\u4ec0\u4e48\u559c\u597d\u4e86\u3002\u4ec0\u4e48\uff1f\u548b\u628a\u811a\u672c\u5b89\u88c5\u5230\u5973\u795e\u7684\u7535\u8111\u91cc\uff1f\u548b\u628a\u6293\u7684\u56fe\u7247\u8fd4\u56de\u7ed9\u4f60\uff1f\u62dc\u6258\uff0c\u8fd9\u4e48\u7b80\u5355\u7684\u95ee\u9898\uff0c\u8fd8\u9700\u8981\u6211\u5e2e\u4f60\u627e\u7b54\u6848\u4e48\uff1f\u5973\u795e\u7684\u7535\u8111\u574f\u4e86\uff0c\u901a\u5e38\u90fd\u4f1a\u627e\u4e00\u4e2a\u719f\u6089\u7535\u8111\u8f6f\u786c\u4ef6\u7684\u8ba1\u7b97\u673a\u7cfb\u7684\u7ae5\u978b\u6765\u4fee\u7684\uff0c\u4fee\u7684\u65f6\u5019\u6084\u6084\u52a8\u70b9\u624b\u811a\u3002\u56fe\u7247\u8fd4\u56de\u7684\u95ee\u9898\u5462\uff0c\u5b8c\u5168\u53ef\u4ee5\u5b9a\u65f6\u6293\u53d6\uff0c\u7136\u540e\u6293\u53d6\u4e00\u5b9a\u6570\u91cf\u4e4b\u540e\u6253\u5305\u53d1\u5230\u6307\u5b9a\u90ae\u7bb1\uff0c\u7136\u540e\u5220\u9664\u672c\u5730\u56fe\u7247\u561b\u3002\u4ec0\u4e48\uff1f\u4e0d\u77e5\u9053PowerShell\u548b\u53d1\u90ae\u4ef6\u2026\u4f60\u59b9\u7684\uff0c\u7528System.Net.Mail.MailMessage\u7ec4\u4ef6\u8c03\u7528SMTP\u53d1\u9001\u90ae\u4ef6\u4f60\u4e0d\u4f1a\uff1f\u6211\u4ee5\u524d\u5199\u8fc7\u7c7b\u4f3c\u6587\u7ae0\u7684\u2026\u597d\u5427\uff0c\u9001\u4f5b\u9001\u5230\u897f\uff0c\u81ea\u5df1\u53bb\u770b\u5427\u300a<a href=\"http:\/\/www.cnblogs.com\/fuhj02\/archive\/2010\/06\/09\/1754961.html\" target=\"_blank\" rel=\"noopener\">\u4f7f\u7528PowerShell\u901a\u8fc7Smtp\u53d1\u9001\u90ae\u4ef6<\/a>\u300b\u3002\u8fd8\u6709\u5c31\u662f\u5199\u7684\u811a\u672c\u52a1\u5fc5\u8981\u52a0\u5bc6\uff0c\u81f3\u4e8e\u52a0\u5bc6\u65b9\u5f0f\u65b9\u6cd5\u561b\uff0c\u6539\u5929\u5427\uff0c\u6539\u5929\u4e13\u95e8\u5199\u4e00\u7bc7\u6587\u7ae0\u5199\u5173\u4e8ePowerShell\u811a\u672c\u52a0\u5bc6\uff0c\u8fd9\u79cd\u4e8b\u60c5\u561b\uff0c\u5982\u679c\u88ab\u5973\u795e\u53d1\u73b0\u9e1f\uff0c\u90a3\u53ef\u662f\u5403\u4e0d\u4e86\u515c\u7740\u8d70\u7684\u4e8b\u554a\u3002\u8fd8\u6709\u51fa\u53bb\u522b\u544a\u8bc9\u522b\u4eba\uff0c\u6211\u7ed9\u4f60\u51fa\u7684\u8fd9\u4e3b\u610f\uff0c\u8fd8\u6709\u9690\u79c1\u7684\u4e1c\u897f\uff0c\u81ea\u5df1\u628a\u63e1\u597d\u5ea6\uff0c\u5982\u679c\u8b66\u5bdf\u8700\u9ecd\u8bf7\u4f60\u53bb\u559d\u8336\u4e86\u53ef\u5c31\u4e0d\u597d\u73a9\u4e86\u3002<font color=\"#ff0000\">\u672c\u6545\u4e8b\u7eaf\u5c5e\u865a\u6784\uff0c\u5982\u6709\u96f7\u540c\u7eaf\u5c5e\u5de7\u5408\uff0c\u672c\u4eba\u53ea\u63d0\u4f9b\u521b\u610f\uff0c\u5982\u679c\u5177\u4f53\u5b9e\u65bd\u88ab\u5973\u795e\u6253\u7834\u5934\uff0c\u6216\u8005\u88ab\u8b66\u5bdf\u8700\u9ecd\u8bf7\u53bb\u559d\u8336\u4e86\uff0c\u672c\u4eba\u6982\u4e0d\u8d1f\u4efb\u4f55\u6cd5\u5f8b\u8d23\u4efb\u54c8\u3002<\/font><font color=\"#000000\">\u563f\u563f\uff0c\u4e0d\u591a\u626f\u4e86\uff0c\u5148\u4e0a\u4eca\u5929\u7684\u5b9a\u65f6\u6293\u53d6\u5c4f\u5e55\u56fe\u50cf\u7684\u65b9\u6cd5\u3002<\/font><\/p>\n<p>&#160;&#160;&#160;&#160; \u5176\u5b9e\u65b9\u6cd5\u4e0d\u590d\u6742,\u5199\u4e86\u4e00\u4e2a\u6293\u53d6\u5c4f\u5e55\u7684\u51fd\u6570,\u5b9a\u65f6\u6267\u884c,\u5c06\u6293\u53d6\u7684\u56fe\u7247\u5b58\u5165\u6307\u5b9a\u4f4d\u7f6e,\u5982\u679c\u8fbe\u5230\u7ec8\u6b62\u7684\u65f6\u95f4,\u7ed3\u675f\u6267\u884c.\u4ee3\u7801\u4e0d\u590d\u6742,\u6709\u770b\u4e0d\u61c2\u7684\u5144\u5f1f\u53ef\u4ee5\u7559\u8a00,\u6211\u5e2e\u4f60\u89e3\u7b54\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u4e0a\u4ee3\u7801:<\/p>\n<pre style=\"width: 142.05%; font: 9pt verdana, fixedsys, verdana, tahoma; height: 1164px\"><span class=\"line_num\">        =====\u6587\u4ef6\u540d\uff1aGet-TimedScreenshot.ps1=====<\/span>\n<font color=\"#0000ff\">function<\/font> Get-TimedScreenshot {\n&lt;<font color=\"#008000\">#<\/font>\n    Author:fuhj(powershell<font color=\"#008000\">#live.cn ,http:\/\/fuhaijun.com) <\/font>\n    Takes screenshots at a regular interval and saves them to disk.\n.PARAMETER Path\n    Specifies the folder path.\n.PARAMETER Interval\n    Specifies the interval <font color=\"#0000ff\">in<\/font> seconds between taking screenshots.\n.Parameter EndTime\n    Specifies when the script should stop running <font color=\"#0000ff\">in<\/font> the format HH-MM \n.Example\n    Get-TimedScreenshot -Path c:\\temp\\ -Interval 30 -EndTime 14:00\n<font color=\"#008000\">#&gt;<\/font>\n\n    [CmdletBinding()] <font color=\"#0000ff\">Param<\/font>(\n        [Parameter(Mandatory=$True)]             \n        [ValidateScript({<font color=\"#008080\">Test-Path<\/font> -Path $_ })]\n        [<font color=\"#0000ff\">String<\/font>] $Path, \n\n        [Parameter(Mandatory=$True)]             \n        [Int32] $Interval,\n\n        [Parameter(Mandatory=$True)]             \n        [<font color=\"#0000ff\">String<\/font>] $EndTime    \n    )\n\n<font color=\"#008000\">    #Define helper function that generates and saves screenshot<\/font>\n    <font color=\"#0000ff\">Function<\/font> Get-Screenshot {\n       $ScreenBounds = [Windows.Forms.SystemInformation]::VirtualScreen\n       $ScreenshotObject = <font color=\"#008080\">New-Object<\/font> Drawing.Bitmap $ScreenBounds.Width, $ScreenBounds.Height\n       $DrawingGraphics = [Drawing.Graphics]::FromImage($ScreenshotObject)\n       $DrawingGraphics.CopyFromScreen( $ScreenBounds.Location, [Drawing.Point]::Empty, $ScreenBounds.Size)\n       $DrawingGraphics.Dispose()\n       $ScreenshotObject.Save($FilePath)\n       $ScreenshotObject.Dispose()\n    }\n    <font color=\"#0000ff\">Try<\/font> {\n\n<font color=\"#008000\">        #load required assembly<\/font>\n        Add-Type -Assembly System.Windows.Forms            \n\n        <font color=\"#0000ff\">Do<\/font> {\n<font color=\"#008000\">            #get the current time and build the filename from it<\/font>\n            $Time = (<font color=\"#008080\">Get-Date<\/font>)\n\n            [<font color=\"#0000ff\">String<\/font>] $FileName = <font color=\"#ff00ff\">&quot;$($Time.Month)&quot;<\/font>\n            $FileName += '-'\n            $FileName += <font color=\"#ff00ff\">&quot;$($Time.Day)&quot;<\/font> \n            $FileName += '-'\n            $FileName += <font color=\"#ff00ff\">&quot;$($Time.Year)&quot;<\/font>\n            $FileName += '-'\n            $FileName += <font color=\"#ff00ff\">&quot;$($Time.Hour)&quot;<\/font>\n            $FileName += '-'\n            $FileName += <font color=\"#ff00ff\">&quot;$($Time.Minute)&quot;<\/font>\n            $FileName += '-'\n            $FileName += <font color=\"#ff00ff\">&quot;$($Time.Second)&quot;<\/font>\n            $FileName += '.png'\n\n<font color=\"#008000\">            #use join-path to add path to filename<\/font>\n            [<font color=\"#0000ff\">String<\/font>] $FilePath = (<font color=\"#008080\">Join-Path<\/font> $Path $FileName)\n\n<font color=\"#008000\">            #run screenshot function<\/font>\n            Get-Screenshot\n\n            <font color=\"#008080\">Write-Verbose<\/font> <font color=\"#ff00ff\">&quot;Saved screenshot to $FilePath. Sleeping for $Interval seconds&quot;<\/font>\n\n            <font color=\"#008080\">Start-Sleep<\/font> -Seconds $Interval\n        }\n\n<font color=\"#008000\">        #note that this will run once regardless if the specified time as passed<\/font>\n        <font color=\"#0000ff\">While<\/font> ((<font color=\"#008080\">Get-Date<\/font> -Format HH:mm) <font color=\"#0000ff\">-lt<\/font> $EndTime)\n    }\n\n    <font color=\"#0000ff\">Catch<\/font> {<font color=\"#008080\">Write-Error<\/font> $Error[0].ToString() + $Error[0].InvocationInfo.PositionMessage}\n}<\/pre>\n<p>&#160;<\/p>\n<p>\u6267\u884c\u65b9\u5f0f,\u5982\u4e0b\u56fe\u6240\u793a:<\/p>\n<p>&#160;<img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.fuhaijun.com\/wp-content\/uploads\/2013\/10\/image5.png\" width=\"669\" height=\"102\" \/> <\/p>\n<p>\u6267\u884c\u6548\u679c,\u4f1a\u5728\u6307\u5b9a\u7684\u76ee\u5f55,\u6309\u7167\u65f6\u95f4\u95f4\u9694\u751f\u6210\u684c\u9762\u6293\u56fe,\u7c7b\u4f3c\u5982\u4e0b\u56fe\u6240\u793a.<\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.fuhaijun.com\/wp-content\/uploads\/2013\/10\/image6.png\" width=\"639\" height=\"286\" \/> <\/p>\n<p><font color=\"#000000\"><strong>\u54e5\uff0c\u53ea\u80fd\u5e2e\u4f60\u5230\u8fd9\u4e86!\u5144\u5f1f\u4eec\u597d\u81ea\u4e3a\u4e4b\u5427!<\/strong><\/font><\/p>\n<p>&#160;<\/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\u4e14\u4fdd\u8bc1\u5185\u5bb9\u5b8c\u6574!\u5426\u5219\u5fc5\u7a76\u6cd5\u5f8b\u8d23\u4efb! <\/p>\n<p>\u4e2a\u4eba\u7f51\u7ad9: <a href=\"http:\/\/www.fuhaijun.com\/\">http:\/\/www.fuhaijun.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#160;&#160;&#160;&#160; \u6628\u5929\u7684\u535a\u6587\u5199\u4e86\u5b9a\u65f6\u8bb0\u5f55\u64cd\u4f5c\u7cfb\u7edf\u884c\u4e3a\uff0c\u5176\u5b9e\u8bf4\u767d\u4e86\u5c31\u662f\u6293\u53d6\u4e86\u51fb\u952e [&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,1],"tags":[],"class_list":["post-33520","post","type-post","status-publish","format-standard","hentry","category-powershell","category-default"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33520"}],"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=33520"}],"version-history":[{"count":2,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33520\/revisions"}],"predecessor-version":[{"id":33736,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33520\/revisions\/33736"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}