{"id":3184,"date":"2023-08-16T13:22:42","date_gmt":"2023-08-16T05:22:42","guid":{"rendered":"https:\/\/benson80.eu.org\/?p=3184"},"modified":"2023-08-16T13:53:42","modified_gmt":"2023-08-16T05:53:42","slug":"%e4%bd%bf%e7%94%a8powershell-%e8%84%9a%e6%9c%ac%e7%94%a8%e4%ba%8e%e5%9c%a8%e4%b8%8d%e5%90%8c%e7%bd%91%e7%bb%9c%e4%bd%8d%e7%bd%ae%e7%9a%84%e4%b8%a4%e4%b8%aa%e6%96%87%e4%bb%b6%e5%a4%b9%e4%b9%8b%e9%97%b4","status":"publish","type":"post","link":"https:\/\/benson80.eu.org\/?p=3184","title":{"rendered":"\u4f7f\u7528PowerShell \u811a\u672c\u7528\u4e8e\u5728\u4e0d\u540c\u7f51\u7edc\u4f4d\u7f6e\u7684\u4e24\u4e2a\u6587\u4ef6\u5939\u4e4b\u95f4\u8fdb\u884c\u6587\u4ef6\u540c\u6b65"},"content":{"rendered":"\n<p>\u8fd9\u4e2a PowerShell \u811a\u672c\u53ef\u4ee5\u7528\u4e8e\u5728\u4e0d\u540c\u7f51\u7edc\u4f4d\u7f6e\u7684\u4e24\u4e2a\u6587\u4ef6\u5939\u4e4b\u95f4\u8fdb\u884c\u6587\u4ef6\u540c\u6b65\u3002\u5b83\u4f1a\u76d1\u89c6\u6e90\u6587\u4ef6\u5939\u7684\u53d8\u5316\uff0c\u5e76\u5c06\u53d8\u5316\u540c\u6b65\u5230\u76ee\u6807\u6587\u4ef6\u5939\u4e2d\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u811a\u672c\uff1a<\/p>\n\n\n\n<p>$sourceFolder = &#8220;\\\\192.168.0.146\\c$\\ViewSystem\\ROLLS\\FN2&#8221;<br>$targetFolder = &#8220;\\\\192.168.0.182\\fn22\\FN2\\demo&#8221;<\/p>\n\n\n\n<p>$syncWatcher = New-Object System.IO.FileSystemWatcher<br>$syncWatcher.Path = $sourceFolder<br>$syncWatcher.IncludeSubdirectories = $true<br>$syncWatcher.EnableRaisingEvents = $true<\/p>\n\n\n\n<p>$syncAction = {<br>$changedItem = $eventArgs.FullPath<br>$relativePath = $changedItem -replace [regex]::Escape($sourceFolder), &#8221;<br>$targetItem = Join-Path -Path $targetFolder -ChildPath $relativePath<\/p>\n\n\n\n<p>if (Test-Path -Path $changedItem -PathType Leaf) {<br># \u6587\u4ef6\u88ab\u521b\u5efa\u6216\u4fee\u6539<br>if (Test-Path -Path $targetItem -PathType Leaf) {<br>$sourceSize = (Get-Item $changedItem).Length<br>$targetSize = (Get-Item $targetItem).Length<\/p>\n\n\n\n<p>if ($sourceSize -ne $targetSize) {<br>Copy-Item -Path $changedItem -Destination $targetItem -Force<br>Write-Host &#8220;\u5df2\u540c\u6b65\u53d8\u52a8\u6587\u4ef6: $relativePath&#8221;<br>}<br>} else {<br>Copy-Item -Path $changedItem -Destination $targetItem -Force<br>Write-Host &#8220;\u5df2\u540c\u6b65\u65b0\u6587\u4ef6: $relativePath&#8221;<br>}<br>} elseif (Test-Path -Path $changedItem -PathType Container) {<br># \u6587\u4ef6\u5939\u88ab\u521b\u5efa<br>if (-not (Test-Path -Path $targetItem -PathType Container)) {<br>New-Item -Path $targetItem -ItemType Directory -Force<br>Write-Host &#8220;\u6587\u4ef6\u5939\u5df2\u521b\u5efa: $relativePath&#8221;<br>}<br>} elseif (-not (Test-Path -Path $changedItem)) {<br># \u6587\u4ef6\u6216\u6587\u4ef6\u5939\u88ab\u5220\u9664<br>if (Test-Path -Path $targetItem) {<br>Remove-Item -Path $targetItem -Force -Recurse<br>Write-Host &#8220;\u5df2\u5220\u9664: $relativePath&#8221;<br>}<br>}<br>}<\/p>\n\n\n\n<p>Register-ObjectEvent -InputObject $syncWatcher -EventName &#8220;Created&#8221; -Action $syncAction<br>Register-ObjectEvent -InputObject $syncWatcher -EventName &#8220;Changed&#8221; -Action $syncAction<br>Register-ObjectEvent -InputObject $syncWatcher -EventName &#8220;Deleted&#8221; -Action $syncAction<\/p>\n\n\n\n<p># \u5faa\u73af\u904d\u5386\u6574\u4e2a\u6587\u4ef6\u5939\u5e76\u590d\u5236\u6587\u4ef6<\/p>\n\n\n\n<p>Get-ChildItem -Path $sourceFolder -File -Recurse | ForEach-Object {<br>$relativePath = $_.FullName -replace [regex]::Escape($sourceFolder), &#8221;<br>$targetItem = Join-Path -Path $targetFolder -ChildPath $relativePath<br>Copy-Item -Path $_.FullName -Destination $targetItem -Force<br>Write-Host &#8220;\u5df2\u590d\u5236\u6587\u4ef6: $relativePath&#8221;<br>}<\/p>\n\n\n\n<p>Write-Host &#8220;\u6b63\u5728\u76d1\u89c6\u548c\u540c\u6b65\u2026&#8221;<br>try {<br>do {<br>Wait-Event -Timeout 60<br>} while ($true)<br>} finally {<br>Unregister-Event -SourceIdentifier $syncWatcher.Created<br>Unregister-Event -SourceIdentifier $syncWatcher.Changed<br>Unregister-Event -SourceIdentifier $syncWatcher.Deleted<br>$syncWatcher.Dispose()<br>}<\/p>\n\n\n\n<p>\u8981\u4f7f\u7528\u8fd9\u4e2a\u811a\u672c\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5c06 <code>$sourceFolder<\/code> \u548c <code>$targetFolder<\/code> \u5206\u522b\u66ff\u6362\u4e3a\u6e90\u6587\u4ef6\u5939\u548c\u76ee\u6807\u6587\u4ef6\u5939\u7684\u5b9e\u9645\u8def\u5f84\u3002<\/li>\n\n\n\n<li>\u5c06\u811a\u672c\u4fdd\u5b58\u4e3a <code>.ps1<\/code> \u6587\u4ef6\uff08\u4f8b\u5982 <code>FileSync.ps1<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u5728 PowerShell \u7a97\u53e3\u4e2d\u8fd0\u884c\u811a\u672c\uff1a<code>.\\FileSync.ps1<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u8981\u5728\u6bcf\u6b21\u5f00\u673a\u65f6\u81ea\u52a8\u8fd0\u884c\u8fd9\u4e2a PowerShell \u811a\u672c\uff0c\u4f60\u53ef\u4ee5\u5c06\u811a\u672c\u7684\u6267\u884c\u6dfb\u52a0\u5230\u8ba1\u7b97\u673a\u7684\u542f\u52a8\u9879\u4e2d\u3002\u8fd9\u6837\uff0c\u6bcf\u6b21\u8ba1\u7b97\u673a\u542f\u52a8\u65f6\uff0c\u811a\u672c\u90fd\u4f1a\u81ea\u52a8\u8fd0\u884c\u3002<\/p>\n\n\n\n<p>1. \u6253\u5f00 PowerShell \u7a97\u53e3\uff08\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\uff09\u3002<\/p>\n\n\n\n<p>2. \u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u6253\u5f00\u8ba1\u5212\u4efb\u52a1\u7ba1\u7406\u5668\uff1a<\/p>\n\n\n\n<p>schtasks \/create \/tn &#8220;FileSync&#8221; \/tr &#8220;powershell.exe -File &#8216;C:\\Users\\xiaosun.GLOTECH-GF\\Desktop\\FileSync.ps1&#8242;&#8221; \/sc onlogon \/ru USERNAME \/rl HIGHEST<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"FileSync\"<\/code>\uff1a\u4efb\u52a1\u7684\u540d\u79f0\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u66f4\u6539\u3002<\/li>\n\n\n\n<li><code>\"C:\\Users\\xiaosun.GLOTECH-GF\\Desktop\\FileSync.ps1\"<\/code>\uff1a\u811a\u672c\u7684\u5b9e\u9645\u8def\u5f84\u3002\u8bf7\u5c06\u5176\u66ff\u6362\u4e3a\u4f60\u7684\u811a\u672c\u7684\u8def\u5f84\u3002<\/li>\n\n\n\n<li><code>USERNAME<\/code>\uff1a\u4f60\u7684\u8ba1\u7b97\u673a\u7528\u6237\u540d\u3002\u8bf7\u5c06\u5176\u66ff\u6362\u4e3a\u4f60\u7684\u5b9e\u9645\u7528\u6237\u540d\u3002<\/li>\n<\/ul>\n\n\n\n<p>3. \u8fd0\u884c\u4ee5\u4e0a\u547d\u4ee4\u540e\uff0c\u8ba1\u5212\u4efb\u52a1\u5c06\u4f1a\u88ab\u521b\u5efa\uff0c\u5b83\u5c06\u5728\u6bcf\u6b21\u7528\u6237\u767b\u5f55\u65f6\u8fd0\u884c\u6307\u5b9a\u7684 PowerShell \u811a\u672c\u3002<\/p>\n\n\n\n<p>\u8bf7\u786e\u4fdd\u5c06\u811a\u672c\u7684\u8def\u5f84\u548c\u7528\u6237\u540d\u66ff\u6362\u4e3a\u4f60\u7684\u5b9e\u9645\u503c\u3002\u5b8c\u6210\u540e\uff0c\u6bcf\u6b21\u8ba1\u7b97\u673a\u542f\u52a8\u65f6\uff0c\u8be5\u811a\u672c\u90fd\u4f1a\u81ea\u52a8\u8fd0\u884c\u5e76\u5f00\u59cb\u76d1\u89c6\u548c\u540c\u6b65\u6587\u4ef6\u5939\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a PowerShell \u811a\u672c\u53ef\u4ee5\u7528\u4e8e\u5728\u4e0d\u540c\u7f51\u7edc &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-3184","post","type-post","status-publish","format-standard","hentry","category-windows"],"_links":{"self":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/3184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3184"}],"version-history":[{"count":8,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/3184\/revisions"}],"predecessor-version":[{"id":3193,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/3184\/revisions\/3193"}],"wp:attachment":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}