I am new to PowerShell.
Goal
Combine text from nested YAML files to a master YAML file.
Current setup
File structure
- folder1/
- (empty)
- folder2/
- restapi/
- toc.yml
- restapi/
- folder3/
- Docs/
- restapi/
- toc.yml
- restapi/
- Docs/
- restapi/
- 1.json
- 2.json
- 3.json
- 4.json
Contents of folder2/toc.yml
- name: JSON 1
href: 1.json
- name: JSON 2
href: 2.json
Contents of folder3/toc.yml
- name: JSON 3
href: 3.json
- name: JSON 4
href: 4.json
Desired result
File structure
- folder1/
- (empty)
- folder2/
- restapi/
- toc.yml
- restapi/
- folder3/
- Docs/
- restapi/
- toc.yml
- restapi/
- Docs/
- restapi/
- 1.json
- 2.json
- 3.json
- 4.json
- toc.yml (generated by combining the toc.yml files in folder2/restapi and folder3/Docs/restapi)
Contents of the toc.yml file in the restapi folder at root
- name: JSON 1
href: 1.json
- name: JSON 2
href: 2.json
- name: JSON 3
href: 3.json
- name: JSON 4
href: 4.json
folder2/restapi/toc.yml and folder3/Docs/restapi/toc.yml do not need to be deleted.
Attempted code
$subfolderslist = (Get-ChildItem $PSScriptRoot -recurse | Where-Object { $_.PSIsContainer -eq $True -and $_.Name -like "restapi"} | Sort-Object)
foreach ($restapifolder in $subfolderslist) {
$fullPath = $restapifolder.FullName
$item = (Get-ChildItem $fullPath)
Get-Content $fullPath/$item | Out-File -append $PSScriptRoot/restapi/toc.yml
}
This successfully produces the desired appended content of the restapi folder at root.
But it errors out with: Get-Content : Cannot find path 'C:\my-project\restapi\1.json 2.json 3.json toc.yml' because it does not exist.
I have tried adding -Exclude *.json to the $item variable but it returns an error also for both folder2 and folder3. Example: Get-Content : Cannot find path 'C:\myproject\folder3\Docs\restapi\C:\myproject\docs-multi-3\Docs\restapi\toc.yml' because it does not exist.
I have also tried excluding the restapi folder at root inside Where-Object but that fails as well.
I have tried all manner of things with changing around variables.
This is probably irrelevant but for what it's worth, this script will be executed in an Azure DevOps YAML pipeline after repos containing those folder structures are cloned (e.g., folder2 is a repo, folder3 is a repo, etc.).
Thank you for any help, including recommendations on better ways to accomplish this.