Powershell and expanding out multiple properties

Soldato
Joined
8 Mar 2005
Posts
3,618
Location
London, UK
I think I know why PS throws a wobbly as there could be an instance where the property contains more values than another but in this case every property will contain the same number of values.

cmdlet returns the following:
Code:
CatalogName         ClientName     DesktopGroupName            BrokeringUserName
-----------         ----------     ----------------            -----------------
BANANA             SIRRUS           BANANA_GROUP        DOMAIN\USERNAME
APPLE              EARTH            APPLE_GROUP            DOMAIN\USERNAME
I need to pull out the values, otherwise when I pass the output to another environment, it returns the cmdlet name. So,
Code:
Select-Object -ExpandProperty CatalogName
returns
Code:
BANANA
APPLE
but,
Code:
Select-Object -ExpandProperty CatalogName, ClientName,DesktopGroupName, BrokeringUserName
fails as I suspect for the assumption above. It would seem a really simply thing to do but brainboy here cannot fathom it :(

The preference would be to keep the headers as well but I'll take just getting the value data.

TIA!


E: oops
so piping to Out-String seems an obvious winner but thinking about it, I probably do want to drop the headers. Ultimately I'm running a query to return all open sessions for a specific user with a view to then perform actions on that session e.g. disconnect, logoff etc.

eh Format-Table -HideTableHeaders works but the formatting isn't great.

Morphed into something else now, somewhat c# related. I'm calling a script with an argument/parameter ..
Code:
// Add the script to the PowerShell object
            // shell.Commands.AddScript(Input.Text);
            shell.Commands.AddScript("D:\\Local_Scripts\\sessioncall.ps1");
            // Add Params
            // shell.Commands.AddParameter(null,User.Identity.Name);
            shell.Commands.AddParameter(null,"TestuserName");

            // Execute the script
            var results = shell.Invoke();
In this instance I'm just trying to verify the argument is passed to the script;
Code:
$username= "$($args[0])"
#param($username)
Write-Output "username - $username" | Out-String
I'm guessing the syntax is incorrect as it just returns "username -" as opposed to the expected "username - TestuserName"

Replacing AddScript with AddCommand worked!
Code:
// Add the script to the PowerShell object
            // shell.Commands.AddScript(Input.Text);
            // shell.Commands.AddScript("D:\\Local_Scripts\\sessioncall.ps1");
            shell.Commands.AddCommand("D:\\Local_Scripts\\sessioncall.ps1");
            // Add Params
            // shell.Commands.AddParameter(null,User.Identity.Name);
            shell.Commands.AddParameter("Username", User.Identity.Name);
            // shell.Commands.AddArgument(User.Identity.Name);

            // Execute the script
            var results = shell.Invoke();
 
Last edited:
Associate
Joined
2 Jul 2003
Posts
2,436
I might be missing something here? Are the values returned by CatalogName, ClientName, DesktopGroupName, BrokeringUserName collections themselves?

If not then you don't need to -expandproperty?

If they are then may just be best to create a customobject with your own field names and populate from the cmdlet?
 
Soldato
OP
Joined
8 Mar 2005
Posts
3,618
Location
London, UK
I might be missing something here? Are the values returned by CatalogName, ClientName, DesktopGroupName, BrokeringUserName collections themselves?

If not then you don't need to -expandproperty?

If they are then may just be best to create a customobject with your own field names and populate from the cmdlet?
Yes to the former and yes to the latter. I'm going to create a PSObject as I need to a) pump the output to a gridview which b) will provide a mechanism to then further interact with the dataset,e.g. forcibly logoff sessions.
 
Back
Top Bottom