1

I'm trying to create a custom object based on server names from a text file. The script I have goes and imports the txt file into a Variable. Then runs a foreach server in the servers variable to create the custom object. I would like to be able to output the object's properties as a table that doesn't include the header info each time.

See script and output below:

 $SERVERS = gc c:\servers.txt


foreach ($srv in $SERVERS)
{
    $Obj = New-Object PsObject -Property`
   @{
         Computername = $srv
         SecurityGroup = (Get-QADComputer $srv).memberof
         RebootDay = ((Get-QADComputer $srv).memberof).split(',').split(' ')[2]
         Combined = ((Get-QADComputer $srv).memberof).split(',').split(' ').split('=')[1]
         RebootTime = $obj.combined.substring(0,4)
     }

 echo $obj | ft Computername,RebootDay -autosize

}

This is the output currently:

Computername RebootDay


SERVER007 Sunday

Computername RebootDay


SERVER009 Sunday

Computername RebootDay


SERVER003 Sunday


I'd like it to look more like:

Computername RebootDay


SERVER007 Sunday
SERVER001 Sunday
SERVER009 Sunday

2 Answers 2

3

TessellatingHeckler was on the right track really. The issue with his code is that you can't pipe a ForEach($x in $y){} loop to anything (not to be confused with a ForEach-Object loop that you usually see shortened to just ForEach like $Servers | ForEach{<code here>}) You don't want to pipe objects to Format-Table one at a time, you want to pipe a collection of objects to it so that it looks nice. So here's the modified code:

$SERVERS = gc c:\servers.txt

$Results = foreach ($srv in $SERVERS)
{
    New-Object PsObject -Property @{
         Computername = $srv
         SecurityGroup = (Get-QADComputer $srv).memberof
         RebootDay = ((Get-QADComputer $srv).memberof).split(',').split(' ')[2]
         Combined = ((Get-QADComputer $srv).memberof).split(',').split(' ').split('=')[1]
         RebootTime = $obj.combined.substring(0,4)
     }
}
$Results | FT ComputerName,RebootDay -auto

That collects the objects in an array, then you pass the whole array to Format-Table

Sign up to request clarification or add additional context in comments.

Comments

1

Don't put the "ft" (Format-Table) command inside the loop, put it outside, once, at the end. e.g.

$SERVERS = gc c:\servers.txt

$results = foreach ($srv in $SERVERS)
{
    $Obj = New-Object PsObject -Property`
   @{
         Computername = $srv
         SecurityGroup = (Get-QADComputer $srv).memberof
         RebootDay = ((Get-QADComputer $srv).memberof).split(',').split(' ')[2]
         Combined = ((Get-QADComputer $srv).memberof).split(',').split(' ').split('=')[1]
         RebootTime = $obj.combined.substring(0,4)
     }
    $Obj
} 
$results | ft Computername,RebootDay -autosize

Edit: Fixed for foreach pipeline bug.

You could possibly neaten it a bit because you don't need to make a new PSObject for a hashtable, and then put the object into the pipeline; you don't need to repeat the Get-QADComputer commands three times. I'm suspicious that the $obj.combined line isn't doing anything - how can you refer to an object inside the properties of the new-object call, before it gets assigned that name? And the repeated splits could probably be combined because it operates on individual characters, not strings.

gc c:\servers.txt | foreach {
    $memberof = (Get-QADComputer $_).memberof

    @{
        Computername = $_;
        SecurityGroup = $memberof;
        RebootDay = $memberof.split(', ')[2];
        Combined = $memberof.split(', =')[1];
        # ?? RebootTime = $obj.combined.substring(0,4)
    }
} | ft Computername,RebootDay -autosize

1 Comment

You can not pipe output of a ForEach loop to anything (not to be confused with the inline ForEach-Object loop that can be shortened to ForEach). Instead do something like $Results = ForEach($srv in $SERVERS){<same code here>};$Results|FT ComputerName,RebootDay -auto

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.