Search code examples
powershellglobal-variables

Build variables from properties in Powershell script


I am trying to build a script to gather the DFSR backlog details of a list of servers. So far the script will query a text file for a list of servers. From that list it will use various Powershell commands to determine what replication groups it is a member of and what connections it has. Once I have that data stored in variables I can then use it in dfsrdiag backlog to check the status.

The problem I have is how can I capture and set select properties to variables that I can use to create the dfsrdiag command.

Anyone out there know the best way to select the particular properties and store then to variables in Powershell?

Cheers

Woodsy


Solution

  • Here is a simple example using get-service. You can create an object called $report that contains only the properties you want. You can export $Reportto a CSV.

    All you need to do is apply this to your script.

    $Services = Get-Service | Where-Object {$_.name -like "A*"}
    
    [Array]$Report = foreach ($service in $Services)
    {
        [PScustomobject]@{
            Name = $service.DisplayName
            Shortname = $service.name
            Status = $service.Status
            StartType = $service.StartType
        }
    }
    
    $Report | select Name, Shortname, Status, StartType