2017-09-19 34 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"