Continuing from my comment, you need to use Scoping to reference variables declared outside the functions in order to manipulate them.
Without scoping, these variables are simply new, local variables that exist in the function only.
Why not try
function Init {
$script:compressedfiles = Get-ChildItem -Path $script:FilesPath -Recurse -Include "*.vip", "*.zip", "*.cab"
if (-Not $script:compressedfiles) {
$script:compressedfiles = fallback
}
}
function fallback {
$fallbackpath = '\\google.com\global\builds\PatchCreatorOutput'
# just output
Get-ChildItem -Path "$fallbackpath\$script:FolderNumber" -Recurse -Include "*.vip", "*.zip", "*.cab"
}
# Main script
# declare the variables you want the functions to see/manipulate here
# inside the functions you reference them with '$script:variablename'
$compressedfiles = $null # declared outside the functions
$FilesPath = '\\google.com\global\builds\SomeWhereToStartWith'
$FolderNumber = 1
# call Init
Init
# show what $compressedfiles contains
$compressedfiles | Format-Table -AutoSize
Still guessing what you really want to do here, but you can of course also achieve this by eliminating the fallback
function alltogether as it does nothing more than try and get a list of files from a different source path, with just a few extra lines in the init
function.
This would also relieve you from the burdon of having to use scoping everywhere because now we can use local variables in the function.
# put your init helper function on top so it is defined before you use it in the main script
function Init {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$FilesPath,
[Parameter(Mandatory = $true, Position = 1)]
[string]$FallBackPath
)
$files = @(Get-ChildItem -Path $FilesPath -File -Recurse -Include "*.vip", "*.zip", "*.cab" -ErrorAction SilentlyContinue)
if ($files.Count -eq 0) {
# no files found in $FilesPath, so try the $FallBackPath
Write-Host "No files found in $FilesPath.. Now trying $FallBackPath.."
$files = @(Get-ChildItem -Path $FallBackPath -File -Recurse -Include "*.vip", "*.zip", "*.cab" -ErrorAction SilentlyContinue)
if ($files.Count -eq 0) {
Write-Host "No files found in $FallBackPath.."
# no files in the fallbackpath either, return $null
return $null
}
}
# return the resulting files array.
# because of 'unboxing' prefix this with a unary comma
,$files
}
# Main script
$folderNumber = 1
$filesPath = Join-Path -Path 'X:\Somewhere\Builds\PatchCreatorOutput' -ChildPath $folderNumber
$fallBackPath = Join-Path -Path '\\google.com\global\builds\PatchCreatorOutput' -ChildPath $folderNumber
# call Init with parameters and capture the results in $compressedfiles
$compressedfiles = Init -FilesPath $filesPath -FallBackPath $fallBackPath
# show what $compressedfiles contains if anything
if (!$compressedfiles) {
Write-Warning "Could not find any files in both the files path and the fallback path.."
}
else {
$compressedfiles | Format-Table -AutoSize
}