-1

I am new to PS and I am trying to write a function which takes in parameters from a global variable. I want to pass a path name read from a .txt file into a function in the same script.

function GetCorrectChildren ([string] $homepath,$min,$max,$row)
{
    #Testpoint 2
    write-host "homepath = $homepath"
    $ColItem = (Get-ChildItem $homepath |? {$_.PSIsContainer} | sort-object)
}

foreach ($homepath in (Get-Content $PSScriptRoot\homepath_short.txt))
{
    $freeSpace = [win32api]::GetDiskFreeSpace("$homepath").FreeBytesAvailable / 1073741824
    $totalSpace = [win32api]::GetDiskFreeSpace("$homepath").TotalNumberOfBytes / 1073741824 
    $percentageFreeSpace = $freeSpace / $totalSpace * 100

    if($freeSpace -lt $threshold)
    {
    #Testpoint 1
    write-host "homepath = $homepath"
    GetCorrectChildren ("$homepath",$min,$max,$OriRow)
}

For #Testpoint 1, it returns the path name correctly which is \\C:\test1\test_a. However in #Testpoint 2 it returns \\C:\test1\test_a 20 30 System.Object.
I don't understand what does the 20 30 System.Object mean and where does it come from? Can some one shine some light on this? Thanks

DAXaholic
  • 33,312
  • 6
  • 76
  • 74

1 Answers1

4

Change the last line

GetCorrectChildren ("$homepath",$min,$max,$OriRow)

to

GetCorrectChildren $homepath $min $max $OriRow

as ("$homepath",$min,$max,$OriRow) creates a single array with the four values and passes it to the function GetCorrectChildren as its first parameter so that write-host "homepath = $homepath" in it will print all 4 values

DAXaholic
  • 33,312
  • 6
  • 76
  • 74