Kevin
Kevin

Reputation: 193

Powershell foreach in function never completes task

I have a CSV file with about 1200 computer names, and used [system.net.dns]::resolve() to see which are online and which are offline.

This worked fine, but as the script got larger I had to wrap it in a function, but now it reads only the first 8 lines of the file and then it stops.

This is an example of what $y would be:

HB4440
SVSIG09
HB2645
HB5567
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Set-PSDebug -Strict

#Function Assembling

Function Saveat ()
{
    #work in progress
    $Saveat = New-Object -Typename System.Windows.Forms.SaveFileDialog
    $Saveat.filter = "CSV (*.csv)| *.csv"
    [void]$Saveat.ShowDialog()
    return $Saveat.FileName
}

function Compare ($location)
{
    ...
}


function whichcsv()
{
    #working so far
    $location = New-Object System.Windows.Forms.OpenFileDialog
    $location.filter = "CSV (*.csv)| *.csv"
    [void]$location.ShowDialog()
    return $location.FileName
}

function Checktrough ($y , $Filter,$Saveworking,$SaveFailed)
{
    Write-Host "Function Start"
    foreach($n in $y)
    {
        try {
            $Computer = [system.net.dns]::resolve($n.$Filter) | Select HostName,AddressList 
            $IP = ($Computer.AddressList).IPAddressToString
            Write-Host $n.$Filter $IP
            New-Object PSObject -Property @{IPAddress=$IP; Name=$n.$Filter} | Export-Csv $Saveworking -NoTypeInformation -Append
        } catch {
            Write-Host "$($n.$Filter) is unreachable."
            New-Object PSObject -Property @{Name=$n.$Filter} | Export-Csv $SaveFailed -NoTypeInformation -Append
        }
    }
    Write-Host "Function end"
}

    #Select which option Form
    #region Initiate Form
    ...
    #endregion
    #Formclosed
    ...
        #Select path of the CSV
        $csvpath = whichcsv

        #Choosed Options
        $x = $listBox.SelectedItem
        switch ($x)
        {
            #Option 1 
            "AS400 Computer"
            {
                ...
            }

            #Option 2
            "AS400 Personalstamm" 
            {
               ...
            }

            #Option 3
            "ADComputer" 
            {
                #work in progress
                $CSV = Import-Csv -Path $csvpath -Delimiter ',' 
                $y = $CSV | Select Name
                $Filter = "Name"
                $Saveworking = Saveat
                $SaveFailed = Saveat
                Checktrough ($y , $Filter,$Saveworking,$SaveFailed)
            }

            #Option 4
            "ADBenutzer" 
            {
                ...
            }  
        }
    } 

Upvotes: 0

Views: 52

Answers (1)

TToni
TToni

Reputation: 9391

Your call to `Checkthrough' only fills the first parameter because with the commas between the arguments you are telling PowerShell to create an array. Rule of thumb: self defined functions behave like cmdlets, so call them like a cmdlet.

Let me demonstrate with a similar function:

function Foo($a,$b,$c)
{
    Write-Host "a = '$a'"
    Write-Host "b = '$b'"
    Write-Host "c = '$c'"
}

$myArray = @("X","Y","Z")

Write-Host "*** First call (OK)"
Foo $myArray "Hello" "World"

Write-Host "*** Second call (OK)"
Foo -a $myArray -c "World" -b "Hello"

Write-Host "*** Third call (Oops)"
Foo ($myArray,"Hello","World")

Write-Host "*** Same withouf braces"
Foo $myArray,"Hello","World"

Upvotes: 2

Related Questions