繁体   English   中英

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

[英]Powershell copy folder and content to destination

我正在尝试编写一个 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

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

#   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

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

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

$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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM