简体   繁体   English

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

[英]Powershell copy folder and content to destination

I am trying to write a Powershell script that transfers my SID-1 folder to the C:\\temp and then install the .exe files for each program.我正在尝试编写一个 Powershell 脚本,将我的 SID-1 文件夹传输到 C:\\temp,然后为每个程序安装 .exe 文件。 At the moment my efforts merely make C:\\temp\\SID-1\\ but no subfolder or content transfers.目前我的努力只是使 C:\\temp\\SID-1\\ 但没有子文件夹或内容传输。 Very new to Powershell so feel I am missing something obvious. Powershell 非常新,所以觉得我错过了一些明显的东西。 I run these as a preloaded script in the Atera MSP.我将这些作为 Atera MSP 中的预加载脚本运行。

I believe the issue is with the #Transporter section, as it is not copying the whole folder and its content.我相信问题出在 #Transporter 部分,因为它没有复制整个文件夹及其内容。 I have recently attempted to add the IF statement to create C:\\temp我最近尝试添加 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
}

Everything else below this point is merely an installer for the programs I consider standard installs.低于这一点的其他所有内容都只是我认为标准安装的程序的安装程序。

Below is the full script下面是完整的脚本

    #   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

You are copying from the destination!您正在从目的地复制! But there probably won't be any files.但可能不会有任何文件。 You want to do it the other way around.你想反过来做。 Also you need to copy after the directory was created:您还需要在创建目录后复制:

#   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}

Edit: The even better option is to copy the whole directory.编辑:更好的选择是复制整个目录。 You don't even have to check if the folder already exists, because the script automaticly creates the C:\\temp folder if it isn't there:您甚至不必检查文件夹是否已经存在,因为如果 C:\\temp 文件夹不存在,脚本会自动创建:

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

Please test it, let me know if it worked and if it did, mark my post as the answer :)请测试它,让我知道它是否有效,如果有效,请将我的帖子标记为答案:)

Check if filepath is exist recomend do at start, like this, for me this work good.检查文件路径是否存在推荐在开始时做,像这样,对我来说这项工作很好。

$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.

相关问题 Powershell:将父文件夹中的所有子目录和内容复制到另一个文件夹目标 - Powershell: Copy all Subdirectory and Contents in Parent Folder to Another Folder Destination Powershell - 将目录和文件从源文件夹复制到目标文件夹 - Powershell - Copy directory and files from source folder to destination folder 用于将文件夹和内容复制到远程位置的 Powershell 脚本 - Powershell Script to Copy Folder and Content to remote location 如何使用powershell将文件夹的内容复制到另一个特定文件夹? - How to copy content of a folder to another specific folder using powershell? 使用Powershell,我想强制复制文件夹/文件而又不删除现有目标文件夹中的多余文件: - Using Powershell I want to Forcefully Copy Folder/files Without Erasing Extra Files in Existing Destination Folder: 基于文件夹和.txt内容比较的Powershell复制和日志项目 - Powershell copy and log items based on folder and .txt content comparison 如何使用robocopy在Powershell脚本中仅复制目录的文件夹内容 - How to copy **only** the folder content of a directory in powershell script with robocopy Powershell复制项目目标不明确 - Powershell Copy-Item destination is ambiguous Powershell Copy-Item 不去目的地 - Powershell Copy-Item Not going to destination Powershell 将文件复制到新目标的脚本 - Powershell Script to Copy Files to new Destination
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM