2016-11-15 4 views
0

Ich schreibe eine Funktion und beobachtet etwas ungewöhnliches Verhalten mit dem Ausgang.Problem mit der Ausgabe der Powershell-Funktion

Dies ist der Code:

Function Get-CompInfo 
{ 
    [Cmdletbinding()] 
    PARAM 
    (
     [Parameter(ValueFromPipeline=$true)] 
     [Alias('Comp', 'Host')] 
     [string[]]$computername 
    ) 


    Begin 
    { 

    } 
    Process 
    { 
     if ($computername -eq $Null) { 
       $computername=$env:computername 
       $VerboseOut="No computer specified. Running against local computer $computername :" 
     } 
      Else { 
       $VerboseOut="Getting information for computer $computername :" 
     } 
     Write-Verbose $VerboseOut 
     $CompInfo=Get-WmiObject Win32_Computersystem -computername $computername 
     $OSInfo=Get-WmiObject Win32_OperatingSystem -computername $computername 
       $Properties = [ordered]@{ 
       'Input'=$computername 
       'SystemName'=$CompInfo.Name 
       'Manufacturer'=$CompInfo.Manufacturer 
       'Model'=$CompInfo.Model 
       'PhysicalMemory'=$CompInfo.TotalPhysicalMemory 
       'LogicalProcessors'=$CompInfo.NumberOfLogicalProcessors 
       'OSCaption'=$OSInfo.Caption 
       'OSArchitecture'=$OSInfo.OSArchitecture 
       'ServicePackMajorVersion'=$OSInfo.ServicePackMajorVersion} 

     # Output Information 
     $obj=New-Object -TypeName PSobject -Property $Properties 
     write-output $obj 

    } 
    End 
    { 

    } 
} 

Wenn Parameter aus dem pipline vorbei:

"Karuma", "localhost" | Get-CompInfo

Input     : {karuma} 
SystemName    : KARUMA 
Manufacturer   : Hewlett-Packard 
Model     : h9-1400a 
PhysicalMemory   : 17115000832 
LogicalProcessors  : 8 
OSCaption    : Microsoft Windows 10 Pro 
OSArchitecture   : 64-bit 
ServicePackMajorVersion : 0 

Input     : {localhost} 
SystemName    : KARUMA 
Manufacturer   : Hewlett-Packard 
Model     : h9-1400a 
PhysicalMemory   : 17115000832 
LogicalProcessors  : 8 
OSCaption    : Microsoft Windows 10 Pro 
OSArchitecture   : 64-bit 
ServicePackMajorVersion : 0 

ich die gleiche Art von Ausgabe zu erhalten, wenn ich eine Textdatei mit einer Liste von Computernamen übergeben.

Bei der Angabe mehrerer Hostnamen etwas anderes:

Get-CompInfo -computername localhost, Karuma

Input     : {localhost, karuma} 
SystemName    : {KARUMA, KARUMA} 
Manufacturer   : {Hewlett-Packard, Hewlett-Packard} 
Model     : {h9-1400a, h9-1400a} 
PhysicalMemory   : {17115000832, 17115000832} 
LogicalProcessors  : {8, 8} 
OSCaption    : {Microsoft Windows 10 Pro, Microsoft Windows 10 Pro} 
OSArchitecture   : {64-bit, 64-bit} 
ServicePackMajorVersion : {0, 0} 

Ich erwarte Tabellenausgabe zu sehen, wenn mehrere Werte vorbei, wie durch gesehen würde Rohrleitung zur Format-Tabelle.

Jede Hilfe auf was ich ändern muss, um die Ausgabe wie gewünscht zu erhalten, würde geschätzt werden.

Antwort

0

Es ist die Art und Weise, wie Sie Ihre Funktion einrichten ... Wenn Sie die Pipeline verwenden, wird der PROCESS-Block einmal für jedes Objekt in der Pipeline ausgeführt - der Parameter $ computername enthält in dieser Situation immer nur ein einzelnes Objekt.

Wenn Sie zwei Computer in $ Computername-Parameter angeben, ändern Sie die Art, wie die gesamte Funktion ausgeführt wird, da sie zwei Objekte enthält.

Es ist einfach durch Umwickeln Sie Ihre Funktion in einem Foreach zu beheben, wie so:

Function Get-CompInfo 
{ 
    [Cmdletbinding()] 
    PARAM 
    (
     [Parameter(ValueFromPipeline=$true)] 
     [Alias('Comp', 'Host')] 
     [string[]]$computername 
    ) 

    Begin {} 
    Process 
    { 
     Foreach ($computer in $computername) { 
      if ($computer -eq $Null) { 
        $computer=$env:computername 
        $VerboseOut="No computer specified. Running against local computer $computer :" 
      } 
       Else { 
        $VerboseOut="Getting information for computer $computer :" 
      } 
      Write-Verbose $VerboseOut 
      $CompInfo=Get-WmiObject Win32_Computersystem -computername $computer 
      $OSInfo=Get-WmiObject Win32_OperatingSystem -computername $computer 
        $Properties = [ordered]@{ 
        'Input'=$computer 
        'SystemName'=$CompInfo.Name 
        'Manufacturer'=$CompInfo.Manufacturer 
        'Model'=$CompInfo.Model 
        'PhysicalMemory'=$CompInfo.TotalPhysicalMemory 
        'LogicalProcessors'=$CompInfo.NumberOfLogicalProcessors 
        'OSCaption'=$OSInfo.Caption 
        'OSArchitecture'=$OSInfo.OSArchitecture 
        'ServicePackMajorVersion'=$OSInfo.ServicePackMajorVersion} 

      # Output Information 
      $obj=New-Object -TypeName PSobject -Property $Properties 
      write-output $obj 
     } 
    } 
    End {} 
} 
+0

Dom Jones hat einen großen Artikel, der diese behavious mit schönen Beispielen erläutert: https://technet.microsoft.com/en- us/library/hh413265.aspx –

+0

Hey James, vielen Dank! Ich kann nicht glauben, dass ich das übersehen habe, aber es funktionierte und war in der Lage, Eigenschaften mit Auswahl etc. ohnmächtig. Dieser Artikel sieht auch interessant aus, wird es auschecken! – J1raya