4

I'm pretty new to scripting, as well as to PowerShell. What I'm trying to do is deploy files from one folder (C:\Deployments\QA\QADEP-1504\Processor Handlers\Release) to another folder on a remote server (\HQDEVAPP004\C$\Lonestar\ProcessorHandlers\Test). And within that 'Test' folder, there are 3 subfolders (1, 2, and RejectedTransactionHandler).

So I want all of the files to deploy to folders 1 and 2, but not the RejectedTransactionHandler folder. I've tried using the '-exclude' function but that seems to be only good for files, not folders, but I could be wrong. The files deploy to all three folders every time. Here is what I have so far:

Location of starting directory

$_SourcePath = "C:\Deployments\QA\QADEP-1504\Processor Handlers\Release"

List all folders in local directory

$PH = Get-ChildItem '\\HQDEVAPP004\c$\LoneStar\ProcessorHandlers\Test\' -Directory
Clear-Host

foreach ($folder in $PH.Name) { Get-ChildItem -recurse ($_SourcePath) | Copy-Item -Destination "\HQDEVAPP004\C`$\Lonestar\ProcessorHandlers\Test$folder" -PassThru }

</code></pre>
Zero596
  • 83

2 Answers2

2

Add if ($folder -ne 'RejectedTransactionHandler'), so when looping through the folders, it won't copy to the RejectedTransactionHandler folder.
The PowerShell -ne comparison operator returns $true if the two values are not equal, and $false if they are. Here are the relevant docs.

Tuor
  • 291
1

To knock this task out [K.O. style] you could...

  1. Create an array variable to contain the folder name(s) to omit.
  2. Create another variable containing the directory names of the subfolders beneath the source directory excluding the omitted subfolders specified.
  3. Then loop over the source variable to...
    • Create destination subfolder(s) if it does not exist
    • Copy the source subfolder(s) content to the correlate destination folder(s)

PowerShell

#$ExcludeFolders = "RejectedTransactionHandler","FolderXYZ"
$ExcludeFolders = "RejectedTransactionHandler"
$_Source = Get-ChildItem 'C:\Folder\Source' -Directory | ? { $_.Name -notin $ExcludeFolders };
$_Destination = "C:\Folder\Destination\Test";

$Source | % { If (!(Test-Path "$_Destination$($.Name)")){ New-Item -Path "$Destination$($.Name)" -ItemType Directory -Force; } Copy-Item "$($.FullName)*" -Destination "$_Destination$($.Name)" -PassThru }

Supporting Resources