Link to home
Start Free TrialLog in
Avatar of Hankinater
HankinaterFlag for United States of America

asked on

I need to include the ServerName in the output when running a "Get-Queue" PowerShell script.

I have a script that runs every 2 minutes against our hub servers.  It gives me the queues and their message counts.  I use that data in a dashboard.  The "identity" values are <ServerName>/<QueueName>, I want to be able to create another column in my CSV file with server name only.  I've tried several things and I'm getting kind of frustrated.  Below is the script that I use now.  I have it create a CSV file for each server...which is working for me.

Having the ServerName value in a column by itself gives me more flexibility on how I display the data in the dashboard.

Any assistance you can provide would be appreciated!

Thanks,
Hank
$serverlist = @("BLAH0", "BLAH1", "BLAH2", "BLAH3")
     foreach ($server in $serverlist) {
          Get-Queue -Server $server | Select $server, * |Export-Csv -NoTypeInformation "E:\$server.csv" -Verbose 
start-sleep -second 2
 
}

Open in new window

Avatar of Mestha
Mestha
Flag of United Kingdom of Great Britain and Northern Ireland image

Do a split on the identity. You may end up stuck with both parts of the queue identity though.

Something like this in your select line should do it..
expression={$_.Identity.ToString().Split("\")

Do post back the completed script, as it may help others.

Simon.
Avatar of Hankinater

ASKER

Simon,
Thanks for the quick response. I rewrote the script to include the expression. However the result is encapsulated in brackets:
$_.Identity.ToString    Identity                          MessageCount
().Split("\")
--------------------            --------                            ------------
{BLAH0, 452962}        BLAH0\452962            0
{BLAH0, 457544}        BLAH0\457544            6
{BLAH0, 458092}        BLAH0\458092            1
{BLAH0, Submi...        BLAH0\Submission    9
Any idea how to get rid of the curly brackets?  Also can I make the column header read Server, Queue?
Thanks again!

$serverlist = @("BLAH0", "BLAH1", "BLAH2", "BLAH3")
$expression = {$_.Identity.ToString().Split("\")}
     foreach ($server in $serverlist) {
          Get-Queue -Server $server | Select $expression, identity, messagecount |Export-Csv -NoTypeInformation "E:\$server.csv" -Verbose 
start-sleep -second 2
 
}

Open in new window

Sorry, I was looking at output to screen.  Actually the output has double quotes around the split name.  Like "BLAH0 452962"
My PS skills aren't too hot, but I know someone who is and is a member here. Let me see if he can assist.

Simon.
Thanks Simon for getting a step closer.

FYI, I was able to  change the expression to add a comma where th "\" was, so if I can get the double quotes to disapear I'd be ready to go.

Thanks again
Another thought I had was to call the server list in a different way, rather than your static list, and then possibly use that information. However I have sought assistance, lets see if he is available and can post.

Simon.
ASKER CERTIFIED SOLUTION
Avatar of Michael B. Smith
Michael B. Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Michael, thanks a bunch for the help...it works perfectly now.  
Thanks Michael,

The edited code is below

Hank
$serverlist = @("BLAH0", "BLAH1", "BLAH2", "BLAH3")
     foreach ($server in $serverlist) {
          Get-Queue -Server $server | select @{name="Server"; expression={$s = $_.identity.ToString();$s.SubString(0, $s.IndexOf("\"))}}, *
 |Export-Csv -NoTypeInformation "E:\$server.csv" -Verbose 
start-sleep -second 2
 
}

Open in new window