Author Posts

August 4, 2018 at 12:05 am

Hi all

thank you for your help in advance. I am clearly new to powershell. i am barely/poorly able to cobble together batch files and stuck in the 1980's way of doing things.

I need to find a value out of data from an internal website rest called "something" within a whole series of text lines that will be a number, although i need that number to be 0 before a server shuts down

i can currently achieve this with the script below, however i do it in a number of messy hops, outputing values in temp*.txt files, performing remove all text lines but the text line containing "something" and then stripping all the letters, only keeping numbers – looking for a value of 0

is there any way to instead of outputting all this data into temp text files, i could use variables? or is there a neater way to perform this...?

$command = Invoke-RestMethod http://localhost:80/data -Method Get > temp1.txt
$output = get-content temp1.txt
foreach ($line in $output) {
if ($line -like "*something*") {
$line | out-file -FilePath "temp2.txt"
$pattern = '[^0-9]'
$outcome = $line -replace($pattern,")
If ($outcome -gt 0) {"It is not 0, *do not* shutdown."} else {"It is 0, you can shutdown."} # replace with {"shutdown /m \\localhost /f /s"}
}
}
del temp1.txt, temp2.txt

regards Barnaby

August 4, 2018 at 2:36 pm

What does the output of the invoke-restmethod look like? What is temp2.txt for?

August 4, 2018 at 3:53 pm

First, don't send the output to file.

What Invoke-RestMethod will get you back is typically a custom object with rich properties and values available. Instead, just get the output from the Invoke-RestMethod command:

$command = Invoke-RestMethod http://localhost:80/data -Method Get

Then, check what you have to work with:

$Command | Get-Member

I'd be willing to be you could trim this down to something pretty easy, along the lines of:

$Command.PropertyName -match 'something'

And that would tell you exactly what you need to know.

August 5, 2018 at 1:02 am

thank you Joel

I have gotten further cleaning things by way of your direction, much appreciated. currently it looks like this, without the need for temp files etc – so i am much happier:

$command = Invoke-RestMethod http://localhost:80/data -Method Get
$something = $command | get-member -type NoteProperty -name something | select -exp Definition
$something = $something -replace"[^0-9]"

kind regards!

August 5, 2018 at 1:06 am

thanks js, output is pretty simple:

"something" : 0
"otherthings" : 1

you are correct temp2.txt was a depreciated step in my long winded approach..

cheers

August 5, 2018 at 3:21 pm

Not bad! Worth noting that everything you can see from Get-Member is available in a much simpler fashion as well!

Once you have the property name, all you need do is ask the object for it:

$Something = $Command.Something -replace '[^\d]'

Basically, every property and method you see with Get-Member can be accessed using the dot-property accessor and specifying the property or method name. 🙂

August 5, 2018 at 3:44 pm

Ah, that replace deletes all the non-numbers. And if the 2nd arg is $null, it can be left off.

$something = Invoke-RestMethod http://localhost/data -Method Get | select -expand something
$something = $something -replace '[^0-9]'

# or if you like one-liners

$something = (Invoke-RestMethod http://localhost/data -Method Get | select -expand something) -replace '[^0-9]'