by rcjay272 at 2013-02-22 12:20:01
All,by mjolinor at 2013-02-22 21:37:30
I want to update this script to output the servername and session count on the same line. I am having some trouble and would like a point in the right direction to get the out correct or like I want it.
Here is the code I am working on…
$servers = Get-Content -Path C:\Load_Stuff\MonitoringScripts\Env2_TS_Servers.txt
ForEach ($server in $servers) {Get-TSSession -ComputerName $server | Measure-Object -Line; '’ * 4;Write-Host $server }
PS C:\Load_Stuff\MonitoringScripts\PowerShell> .\GetActiveSessionTOTAL_Count.ps1
Lines Words Characters Property
----- ----- ---------- --------
36
ts06.LOAD.LOCAL
43
ts07.LOAD.LOCAL
44
ts08.LOAD.LOCAL
44
ts09.LOAD.LOCAL
37
*****************************************************
This is the output format I am wanting to get to.
PS C:\Load_Stuff\MonitoringScripts\PowerShell> .\GetActiveSessionTOTAL_Count.ps1
Lines Words Characters Property
----- ----- ---------- --------
ts06.LOAD.LOCAL 36
ts07.LOAD.LOCAL 43
ts08.LOAD.LOCAL 44
ts09.LOAD.LOCAL 45
There’s probably a dozen ways to do that.by rcjay272 at 2013-02-22 23:41:50
Here’s one (most of your original code is unchanged)$servers =
Get-Content -Path C:\Load_Stuff\MonitoringScripts\Env2_TS_Servers.txt
ForEach ($server in $servers)
{
[string]{
Get-TSSession -ComputerName $server |
Measure-Object -Line
$Server
}.invoke()
'' * 4
}
mjolinor,by rcjay272 at 2013-02-23 00:03:01
Can you tell me what that is called? Part of my problem is not knowing what the right term is to ask my question.
Testing code now…
Thanks
Rob
mjolinor,by mjolinor at 2013-02-23 07:39:36
I am running into an issue with the output now…What am I doing wrong?
Here is the output…
PS C:\Load_Stuff\MonitoringScripts\PowerShell> . ‘C:\Load_Stuff\MonitoringScripts\PowerShell\GetActiveSessionListENV2.ps1’
Microsoft.PowerShell.Commands.TextMeasureInfo ts06.LOAD.LOCAL
Microsoft.PowerShell.Commands.TextMeasureInfo ts07.LOAD.LOCAL
Microsoft.PowerShell.Commands.TextMeasureInfo ts08.LOAD.LOCAL
Microsoft.PowerShell.Commands.TextMeasureInfo ts09.LOAD.LOCAL
Here is the code…$servers =
Get-Content -Path C:\Load_Stuff\MonitoringScripts\Env2_TS_Servers.txt
ForEach ($server in $servers)
{
[string]{
Get-TSSession -ComputerName $server |
Measure-Object -Line
$Server
}.invoke()
'’ * 4
}
Try this:$servers =
Get-Content -Path C:\Load_Stuff\MonitoringScripts\Env2_TS_Servers.txt
ForEach ($server in $servers)
{
[string]{
$Server
(Get-TSSession -ComputerName $server |
Measure-Object -Line).lines
}.invoke()
'' * 4
}
Basically, it’s wrapping what you want to output in a script block (enclosing it in {} makes it a script block). The .invoke() on the end executes the script block, which returns the output of the script block as a collection of lines. Casting it to [string] turns the individual lines into a single string, with the Output Field Separator between each one. The Output Field separator is controlled by an automatic variable ($ofs). The default is a space. If you change $ofs to something else, that will be inserted between the computer name and the line count. It can be multiple characters (e.g. $ofs = ’ = ')