How can I filter .content from a invoke-webrequest command

2.6k views Asked by At

I have an internal web app that provides connection statistics. I am trying to use PS to collect those statistics, which are in .conent. The .content property has a lot of other values I am trying to filter out and I haven't been very successful.

My Command:

invoke-webrequest -URI http://<my internal web site> | Select-Object -ExpandProperty Content

Returned Results:

Greetings from live node:  server-name [ FQDN ] , serving at port: 443 since [4 days, 0 hour, 3
3 minutes, 33 seconds] with following settings:

IN_MEMORY_MESSAGE_LIFE_IN_HOURS : 1
DELIVERY_GRACE_WINDOW_IN_SECONDS : 10
BUILD_NUMBER : 4
STATISTICS_LOG_FREQUENCY_IN_MINUTES : 0
HEARTBEAT_INTERVAL_IN_MINUTES : 2
CLUSTERING_MODE : IMPLICIT
CLEAN_ORPHANED_SESSIONS : Y
OFFLOAD_SSL : Y
SERVICE_HOST : 192.168.1.1
REQUEST_TIMEOUT_MILLIS : 3000
SERVICE_PORT : 443
VALIDATE_REQUEST_CONTENT_TYPE : N
APP_BROADCAST_LIFE_IN_MINUTES : 1
DEFAULT_MESSAGE_LIFE_IN_DAYS : 1
VERSION : release/2008
ENABLE_IDLER_STATISTICS : Y
ORIGIN_STATS_EXPIRY_IN_MINUTES : 60
INCLUDE_CACHE_CONTROL_HEADER_IN_HEALTH_CHECK : N
DATABASE_PROVIDER : INMEMORY
QUEUEMONITOR_THREADPOOLSIZE : 100
BUILD_DATE : Mon 27 Jul 2020 10:02:44 AM EDT -0400
BUILD_PLAN : AWCM-AWCM302-JOB1-4

Persistence Store Type:INMEMORY , status: live

Current Active Sessions: 6

They only line I care about is "Current Active Sessions: 6" and really all I care about is the numerical value which I will use in a test later in the script.

How can I just grab the one value I need?

2

There are 2 answers

0
JosefZ On BEST ANSWER
$iwr=invoke-webrequest -URI http://<my internal web site> |
    Select-Object -ExpandProperty Content
$cas=$iwr -split [System.Environment]::NewLine |
    Select-String -Pattern "Current Active Sessions:\s*(.*)"
$cas.Matches[0].Groups[1].Value

Above code snippet should return 6. Tested using the following (constant) value of $iwr instead of invoke-webrequest:

$iwr=@'
Greetings from live node:  server-name [ FQDN ] , serving at port: 443 since [4 days, 0 hour, 3
3 minutes, 33 seconds] with following settings:

IN_MEMORY_MESSAGE_LIFE_IN_HOURS : 1
DELIVERY_GRACE_WINDOW_IN_SECONDS : 10
BUILD_NUMBER : 4
STATISTICS_LOG_FREQUENCY_IN_MINUTES : 0
HEARTBEAT_INTERVAL_IN_MINUTES : 2
CLUSTERING_MODE : IMPLICIT
CLEAN_ORPHANED_SESSIONS : Y
OFFLOAD_SSL : Y
SERVICE_HOST : 192.168.1.1
REQUEST_TIMEOUT_MILLIS : 3000
SERVICE_PORT : 443
VALIDATE_REQUEST_CONTENT_TYPE : N
APP_BROADCAST_LIFE_IN_MINUTES : 1
DEFAULT_MESSAGE_LIFE_IN_DAYS : 1
VERSION : release/2008
ENABLE_IDLER_STATISTICS : Y
ORIGIN_STATS_EXPIRY_IN_MINUTES : 60
INCLUDE_CACHE_CONTROL_HEADER_IN_HEALTH_CHECK : N
DATABASE_PROVIDER : INMEMORY
QUEUEMONITOR_THREADPOOLSIZE : 100
BUILD_DATE : Mon 27 Jul 2020 10:02:44 AM EDT -0400
BUILD_PLAN : AWCM-AWCM302-JOB1-4

Persistence Store Type:INMEMORY , status: live

Current Active Sessions: 6
'@
2
Wasif On

Try this:

$iwr=invoke-webrequest -URI http://<my internal web site> | Select-Object -ExpandProperty Content
$iwr|Select-String -Pattern "Current Active Sessions: (.*)"