2017-09-19 66 views
0

我有一個包含1200個計算機名稱的CSV文件,並使用[system.net.dns]::resolve()來查看哪些在線,哪些處於離線狀態。Powershell foreach功能永遠不會完成任務

這工作得很好,但隨着腳本變大,我不得不將它包裝在一個函數中,但現在它只讀取文件的前8行,然後停止。

這是一個什麼樣$y將是一個例子:

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" 
      { 
       ... 
      } 
     } 
    } 
+2

您可能要下降到只有一個[最小,完整,可驗證的示例]修剪你的代碼(http://stackoverflow.com/help/mcve ),以便更容易發現故障。 –

+0

您對Checktrhough的調用包括多餘的括號和逗號:Checktrough($ y,$ Filter,$ Saveworking,$ SaveFailed)=> Checktrhough $ y $ Filter $ SaveWork $ SaveFailed –

+0

我縮小了問題所需的代碼,有幾個「選項」,因爲擁有300個腳本並不是一個解決方案。這一個將總結大約10個腳本到1什麼是相當不錯的,而對於工作的部分,實際上我不知道爲什麼。因爲函數checktrough在capseld腳本中被測試了幾次,只有函數在這個腳本中,而且這個功能已經達到100%,但只要它在命令中命中,它就不再工作了,我想知道爲什麼以及如何進行協商。 – Kevin

回答

2

您對`Checkthrough」僅填充第一個參數,因爲與參數之間的逗號,你告訴PowerShell來創建一個數組通話。經驗法則:自定義函數的行爲與cmdlet相似,因此將它們稱爲cmdlet。

讓我有一個類似的功能演示:

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"