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

时间:2019-09-27 06:54:11

标签: powershell active-directory ms-office

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

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

$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”目录的内容粘贴到远程工作站中。我需要粘贴所有子目录及其内容。

2 个答案:

答案 0 :(得分:1)

尝试 Get-Item -Recursive

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

答案 1 :(得分:0)

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

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