簡體   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