{"id":57,"date":"2025-11-24T13:21:03","date_gmt":"2025-11-24T10:21:03","guid":{"rendered":"https:\/\/notesjob.ru\/?p=57"},"modified":"2025-11-24T15:17:35","modified_gmt":"2025-11-24T12:17:35","slug":"%d1%81%d0%ba%d1%80%d0%b8%d0%bf%d1%82-powershell-%d0%b4%d0%bb%d1%8f-%d0%ba%d0%be%d0%bf%d0%b8%d1%80%d0%be%d0%b2%d0%b0%d0%bd%d0%b8%d1%8f-%d0%bf%d0%b0%d0%bf%d0%be%d0%ba-%d1%87%d0%b0%d1%81%d1%82%d1%8f","status":"publish","type":"post","link":"https:\/\/notesjob.ru\/?p=57","title":{"rendered":"\u0421\u043a\u0440\u0438\u043f\u0442 PowerShell \u0434\u043b\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u043f\u043e\u043a \u0447\u0430\u0441\u0442\u044f\u043c\u0438"},"content":{"rendered":"\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u0421\u043a\u0440\u0438\u043f\u0442 PS \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c\/\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043f\u043e\u043a \u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c\u0438.<br><br>\u041f\u043e\u0434 \u043a\u043e\u043f\u043e\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f Robocopy \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \/E \/B \/R:2 \/W:1 \/MT:8 \/COPYALL \/NFL \/NDL \/NP &#8212; \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043f\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u043d\u0443\u0436\u043d\u044b\u0435 \u0432 \u0441\u0430\u043c\u043e\u043c \u0442\u0435\u043b\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u0430.<br><br>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043a\u0440\u0438\u043f\u0442\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0434\u0438\u0442 \u0431\u0430\u0442\u0447\u0430\u043c\u0438 \u043f\u043e 10 \u043f\u0430\u043f\u043e\u043a \u0432 8 \u043f\u043e\u0442\u043e\u043a. \u042d\u0442\u0438 \u0432\u0441\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u0430\u043c\u0438.<br><br>\u0412\u043a\u043b\u044e\u0447\u0435\u043d\u043e \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430. <br><br>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/td><td><img decoding=\"async\" src=\"https:\/\/notesjob.ru\/wp-content\/uploads\/2025\/11\/batch.jpg\" alt=\"\"><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b\n$sourcePath = \"\\\\server\\d$\\Shares\\\"#\u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043f\u0443\u0442\u044c \u043e\u0442\u043a\u0443\u0434\u0430 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435\n$destinationPath = \"D:\\Shares\\\"#\u041f\u0443\u0442\u044c \u043a\u0443\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\n$batchSize = 10 #\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u043f\u043e\u043a \u0432 \u0431\u0430\u0442\u0447\u0435\n$threadCount = 6\n\n# \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0443\u0442\u0438\nif (-not (Test-Path $sourcePath)) {\n    Write-Host \"\u041e\u0448\u0438\u0431\u043a\u0430: \u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d: $sourcePath\" -ForegroundColor Red\n    exit 1\n}\n\n# \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0441\u0435 \u043f\u0430\u043f\u043a\u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f\nWrite-Host \"\u0421\u043a\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043f\u043e\u043a \u0432 $sourcePath ...\"\n$allFolders = Get-ChildItem -Path $sourcePath -Directory | Select-Object -ExpandProperty FullName\n\nWrite-Host \"\u0412\u0441\u0435\u0433\u043e \u043f\u0430\u043f\u043e\u043a \u0434\u043b\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: $($allFolders.Count)\"\n$totalBatches = [Math]::Ceiling($allFolders.Count \/ $batchSize)\nWrite-Host \"\u0411\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043e \u0431\u0430\u0442\u0447\u0435\u0439: $totalBatches (\u043f\u043e $batchSize \u043f\u0430\u043f\u043e\u043a \u0432 \u043a\u0430\u0436\u0434\u043e\u043c)\" -ForegroundColor Yellow\n\n# \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f\nfunction Copy-FoldersBatch {\n    param(\n        [string[]]$Folders,\n        [string]$SourceRoot,\n        [string]$DestinationRoot,\n        [int]$ThreadCount = 6\n    )\n\n    $jobs = @()\n    $foldersPerThread = [Math]::Ceiling($Folders.Count \/ $ThreadCount)\n\n    for ($i = 0; $i -lt $ThreadCount; $i++) {\n        $startIndex = $i * $foldersPerThread\n        $endIndex = [Math]::Min(($startIndex + $foldersPerThread - 1), ($Folders.Count - 1))\n        if ($startIndex -gt $endIndex) { continue }\n\n        $threadFolders = $Folders[$startIndex..$endIndex]\n\n        Write-Host \"\u0417\u0430\u043f\u0443\u0449\u0435\u043d \u043f\u043e\u0442\u043e\u043a $i \u0434\u043b\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f $($threadFolders.Count) \u043f\u0430\u043f\u043e\u043a\"\n\n        # \u0417\u0430\u043f\u0443\u0441\u043a \u0444\u043e\u043d\u043e\u0432\u043e\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f\n        $job = Start-Job -ScriptBlock {\n            param($folders, $sourceRoot, $destinationRoot, $threadId)\n\n            foreach ($folder in $folders) {\n                try {\n                    $folderName = Split-Path $folder -Leaf\n                    $relativePath = $folder.Substring($sourceRoot.Length).TrimStart('\\')\n                    $destPath = Join-Path $destinationRoot $relativePath\n\n                    # \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0446\u0435\u043b\u0435\u0432\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\n                    if (-not (Test-Path $destPath)) {\n                        New-Item -ItemType Directory -Path $destPath -Force | Out-Null\n                    }\n\n                    # \u0412\u0441\u0435\u0433\u0434\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c Robocopy \u2014 \u043e\u043d \u0441\u0430\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442, \u0447\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c\n                    Write-Host \"[Thread $threadId] \u041a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f): $folderName\"\n\n                    # \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0434\u043b\u044f cmd\n                    $cmd = \"robocopy `\"$folder`\" `\"$destPath`\" \/E \/B \/R:2 \/W:1 \/MT:8 \/COPYALL \/NFL \/NDL \/NP\"\n                    cmd.exe \/c $cmd | Out-Null\n                    $exitCode = $LASTEXITCODE\n\n                    if ($exitCode -le 7) {\n                        Write-Host \"[Thread $threadId] \u0423\u0441\u043f\u0435\u0448\u043d\u043e: $folderName\" -ForegroundColor Green\n                    } else {\n                        Write-Host \"[Thread $threadId] \u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: $folderName (\u041a\u043e\u0434: $exitCode)\" -ForegroundColor Red\n                    }\n                }\n                catch {\n                    Write-Host \"[Thread $threadId] EXCEPTION: $folder -> $($_.Exception.Message)\" -ForegroundColor Red\n                }\n            }\n        } -ArgumentList $threadFolders, $SourceRoot, $DestinationRoot, $i\n\n        $jobs += $job\n    }\n\n    Write-Host \"\u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432...\"\n    $jobs | Wait-Job | Out-Null\n    $jobs | Receive-Job | Out-Null\n    $jobs | Remove-Job | Out-Null\n}\n\n# \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0446\u0438\u043a\u043b \u043f\u043e \u0431\u0430\u0442\u0447\u0430\u043c\nfor ($batchNumber = 0; $batchNumber -lt $totalBatches; $batchNumber++) {\n    $startIndex = $batchNumber * $batchSize\n    $endIndex = [Math]::Min(($startIndex + $batchSize - 1), ($allFolders.Count - 1))\n    $currentBatch = $allFolders[$startIndex..$endIndex]\n\n    Write-Host \"`n=== \u0411\u0430\u0442\u0447 $($batchNumber + 1) \u0438\u0437 $totalBatches ===\" -ForegroundColor Yellow\n    Write-Host \"\u041f\u0430\u043f\u043a\u0438 \u0432 \u0431\u0430\u0442\u0447\u0435: $($currentBatch.Count)\" -ForegroundColor Cyan\n    $currentBatch | ForEach-Object { Write-Host \"  - $(Split-Path $_ -Leaf)\" }\n\n    Copy-FoldersBatch -Folders $currentBatch -SourceRoot $sourcePath -DestinationRoot $destinationPath -ThreadCount $threadCount\n\n    Write-Host \"\u0411\u0430\u0442\u0447 $($batchNumber + 1) \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\" -ForegroundColor Green\n\n    if ($batchNumber -lt $totalBatches - 1) {\n        Write-Host \"\u041f\u0430\u0443\u0437\u0430 3 \u0441\u0435\u043a\u0443\u043d\u0434 \u043f\u0435\u0440\u0435\u0434 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0431\u0430\u0442\u0447\u0435\u043c...\" -ForegroundColor Gray\n        Start-Sleep -Seconds 3\n    }\n}\n\nWrite-Host \"`n=== \u0412\u0421\u0415 \u041f\u0410\u041f\u041a\u0418 \u0421\u0418\u041d\u0425\u0420\u041e\u041d\u0418\u0417\u0418\u0420\u041e\u0412\u0410\u041d\u042b ===\" -ForegroundColor Green\nWrite-Host \"\u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a: $sourcePath\"\nWrite-Host \"\u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435: $destinationPath\"\nWrite-Host \"\u0412\u0441\u0435\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043e \u043f\u0430\u043f\u043e\u043a: $($allFolders.Count)\"<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0421\u043a\u0440\u0438\u043f\u0442 PS \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c\/\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043f\u043e\u043a \u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c\u0438. \u041f\u043e\u0434 \u043a\u043e\u043f\u043e\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f Robocopy \u0441&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/notesjob.ru\/index.php?rest_route=\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notesjob.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notesjob.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notesjob.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notesjob.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=57"}],"version-history":[{"count":7,"href":"https:\/\/notesjob.ru\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/notesjob.ru\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions\/83"}],"wp:attachment":[{"href":"https:\/\/notesjob.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notesjob.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notesjob.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}