I am trying to make a script with neatly laid out functions that compare two folders items. The program:
- Prompts the user for the file path
- Check to see if the file names differ
- Check to see if the file sizes differ
As a test I've been comparing the same folder to itself (output should be false, false). When making step 1 ($referencepath) a function (FolderPrompt) my program doesn't work right and by that I mean I seem to get a different answer almost every time I run it.
This works:
$referencePath = Read-Host -Prompt "Enter new DTNA folder path to check" 
NameDisc
SizeDisc
function NameDisc {
    write-host "Name Discrepancy: " -NoNewline 
    if (Compare-Object -Property name (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
        {return $true} 
    else
        {return $false}
}
function SizeDisc {
    write-host "Size Discrepancy: " -NoNewline 
    if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
        {return $true} 
    else
        {return $false}
}     
But this does not:
FolderPrompt
NameDisc
SizeDisc
function FolderPrompt {
    $referencePath = Read-Host -Prompt "Enter new DTNA folder path to check" 
}
function NameDisc {
    write-host "Name Discrepancy: " -NoNewline 
    if (Compare-Object -Property name (Get-ChildItem $referencePath) -DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
        {return $true} 
    else
        {return $false}
}
function SizeDisc {
    write-host "Size Discrepancy: " -NoNewline
    if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
        {return $true} 
    else
        {return $false}
}     
I've tried:
- Declaring the functions before calling them 
- Putting - $referencePath = 0to reset the value each time thinking that was the problem
- Putting - Return $referencePathat the end of different functions
My best guess is that I need to do something like function <name> ($referencePath) to pass the variable(?).
