Powershell将文件夹和内容复制到目标

时间:2020-02-28 03:53:53

标签: powershell copy-item

我正在尝试编写Powershell脚本,该脚本将我的SID-1文件夹传输到C:\ temp,然后为每个程序安装.exe文件。目前,我的工作只是进行C:\ temp \ SID-1 \,而没有子文件夹或内容传输。对Powershell来说是很新的东西,所以感觉到我缺少明显的东西。我将这些作为Atera MSP中的预加载脚本运行。

我认为问题出在#Transporter部分,因为它没有复制整个文件夹及其内容。我最近尝试添加IF语句来创建C:\ temp

    #   Transporter
$Source = '\\server\Deployment\Standard Installs\SID-1\'
$Destination = 'C:\temp\'
Get-ChildItem $Destination | ForEach-Object {Copy-Item -Path $Source -Destination $_ -Recurse -Force}
If(!(test-path $Destination))
{
New-Item -Path $Destination -ItemType directory
}

这点以下的所有内容都只是我认为是标准安装的程序的安装程序。

下面是完整的脚本

    #   Transporter
$Source = '\\server\Deployment\Standard Installs\SID-1\'
$Destination = 'C:\temp\'
Get-ChildItem $Destination | ForEach-Object {Copy-Item -Path $Source -Destination $_ -Recurse -Force}
If(!(test-path $Destination))
{
New-Item -Path $Destination -ItemType directory
}
$CurrentLocation = 'C:\temp\'
#  Installer
#EXE Preloader
$exe = @(
#   Internet Browsers
'C:\temp\SID-1\Internet Browser\ChromeSetup.exe',
'C:\temp\SID-1\Internet Browser\Firefox Installer.exe',
'C:\temp\SID-1\Internet Browser\OperaSetup.exe',
#   Office 365
'c:\temp\SID-1\Office 365\Setup.X64.en-us_O365BusinessRetail.exe',
'c:\temp\SID-1\Office 365\setuponenotefreeretail.x64.en-us_.exe',
'c:\temp\SID-1\Office 365\Teams_windows.exe',
'c:\temp\SID-1\Office 365\Yammer-ia32-3.4.5.exe',
#   Printers
'c:\temp\SID-1\Papercut\client-local-install.exe',
#   Utilities
'c:\temp\SID-1\Utilities\7z1805-x64.exe',
'c:\temp\SID-1\Utilities\GrammarlySetup.exe',
'c:\temp\SID-1\Utilities\pwsafe64-3.51.0.exe',
'c:\temp\SID-1\Utilities\readerdc_uk_xa_cra_install.exe',
'c:\temp\SID-1\Utilities\TeamViewer_Host_Setup.exe',
'c:\temp\SID-1\Utilities\AnyDesk.exe'
)
#MSI Preloader
$msi = @(
#   AntiVirus
'c:\temp\SID-1\Eset\eset_endpoint_av64.msi'
)
# foreach ($exefile in $exe)

#   Internet Browsers
#Google Chrome
Start-Process -FilePath "c:\temp\SID-1\Internet Browser\ChromeSetup.exe" -ArgumentList "/qn" -Wait
#Opera
Start-Process -FilePath "c:\temp\SID-1\Internet Browser\Firefox Installer.exe" -ArgumentList "/qn" -Wait
#Mozilla Firefox
Start-Process -FilePath "c:\temp\SID-1\Internet Browser\OperaSetup.exe" -ArgumentList "/qn" -Wait

#   Office 365
#Office 2016 Business Premium 64
Start-Process -FilePath "c:\temp\SID-1\Office 365\Setup.X64.en-us_O365BusinessRetail.exe" -ArgumentList "/qn" -Wait
#Onenote 2016
Start-Process -FilePath "c:\temp\SID-1\Office 365\setuponenotefreeretail.x64.en-us_.exe" -ArgumentList "/qn" -Wait
#Teams 64
Start-Process -FilePath "c:\temp\SID-1\Office 365\Teams_windows.exe" -ArgumentList "/qn" -Wait
#Yammer 64
Start-Process -FilePath "c:\temp\SID-1\Office 365\Yammer-ia32-3.4.5.exe" -ArgumentList "/qn" -Wait

#   AntiVirus
#ESET
Start-Process -FilePath "c:\temp\SID-1\Eset\eset_endpoint_av64.msi" -ArgumentList "/qn" -Wait

#   Printers
#Papercut
Start-Process -FilePath "c:\temp\SID-1\Papercut\client-local-install.exe" -ArgumentList "/qn" -Wait


#   Utilities
#PassVault
Start-Process -FilePath "c:\temp\SID-1\Utilities\pwsafe64-3.51.0.exe" -ArgumentList "/qn" -Wait
#Grammarly
Start-Process -FilePath "c:\temp\SID-1\Utilities\GrammarlySetup.exe" -ArgumentList "/qn" -Wait
#7zip
Start-Process -FilePath "c:\temp\SID-1\Utilities\7z1805-x64.exe" -ArgumentList "/qn" -Wait
#Adobe DC Reader
Start-Process -FilePath "c:\temp\SID-1\Utilities\readerdc_uk_xa_cra_install.exe" -ArgumentList "/qn" -Wait
#TeamViewer
Start-Process -FilePath "c:\temp\SID-1\Utilities\TeamViewer_Host_Setup.exe" -ArgumentList "/qn" -Wait
#Anydesk
Start-Process -FilePath "c:\temp\SID-1\Utilities\AnyDesk.exe" -ArgumentList "/qn" -Wait

2 个答案:

答案 0 :(得分:0)

您正在从目的地复制!但是可能不会有任何文件。 您想反过来做。创建目录后,还需要复制:

#   Transporter
$Source = '\\server\Deployment\Standard Installs\SID-1\'
$Destination = 'C:\temp\'
If(!(test-path $Destination))
{
New-Item -Path $Destination -ItemType directory
}
Get-ChildItem $source| ForEach-Object {Copy-Item -Path $_.FullName -Destination $destination -Recurse -Force}

编辑: 更好的选择是复制整个目录。您甚至不必检查该文件夹是否已存在,因为如果该文件夹不存在,该脚本会自动创建C:\ temp文件夹:

$Source = '\\server\Deployment\Standard Installs\SID-1\'
$Destination = 'C:\temp\'    
Copy-Item -Path $source -Destination $destination -Recurse -Force

请对其进行测试,让我知道它是否有效,如果有效,请将我的帖子标记为答案:)

答案 1 :(得分:0)

检查文件路径是否存在,建议在开始时这样做,对我来说,这项工作很好。

$Source = '\\server\Deployment\Standard Installs\SID-1\'
$Destination = 'C:\temp\'
If(!(test-path $Destination))
{
New-Item -Path $Destination -ItemType directory
}
Copy-Item -Path $Source -Destination $Destination -Recurse -Force