PowerShell cmd 复制和粘贴所有子目录及其内容?

PowerShell cmd to copy and paste all subdirectories and their contents?

如何编辑此代码以复制所有子目录及其内容,然后将其粘贴到远程工作站?

此代码的目的是远程卸载 MS Office 2007。我使用相同的代码来安装更新版本。

$Computers = (Get-ADComputer -Filter * -SearchBase "OU=X,DC=Y,DC=Z").Name
ForEach ($Computer in $Computers)
{
Write-Host "Working on $Computer" -ForegroundColor White
Write-Host "Testing access to $Computer" -ForegroundColor White
$HostUp = Test-Connection -ComputerName $Computer -BufferSize 12 -Count 1
If (!($HostUp))
{
Write-Warning -Message "Remote Host is not accessible!" }
Else
{
Write-Host "Success!" -ForegroundColor Green
$items = Get-Item -Path C:\Transfer2007\*
Write-Host "Creating Transfer folder on $Computer" -ForegroundColor Yellow
New-Item -Path \$computer\c$\Transfer2007 -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
foreach ($item in $items)
{
Write-Host "Copying $Item over to $Computer\c$\Transfer2007\" -ForegroundColor Yellow
Copy-Item -Path $item -Destination \$Computer\C$\Transfer2007\ -Force
}
Write-Host "Starting setup on $Computer" -ForegroundColor White
Invoke-Command -ScriptBlock { set-location "C:\Transfer2007\"; .\SETUP.exe /uninstall ProPlus /config \UninstallConfig.xml } -ComputerName $Computer -AsJob
}
}
Get-Job | Format-Table
pause

使用此代码,仅将 'Transfer2007' 目录的内容粘贴到远程工作站。我需要粘贴所有子目录及其内容。

Copy-Item 需要添加 -Recurse 参数,因此您的 Copy-Item 行应该是:

Copy-Item -Path $item -Destination \$Computer\C$\Transfer2007\ -Recurse -Force

试试看 Get-Item -Recursive

并查看 powershell workflow 它允许您一次安装 32 个会话