1

I have a Powershell command that for automation purposes in our monitoring system, I'm trying to write in one line.

Basically, we're checking multiple Hyper-V virtual machines to make sure they are replicating properly. The way we're doing this is checking the last replication date/time and comparing it to the current date/time. Here's the one-liner I have for checking one machine.

(new-timespan -start(get-vm termserv002| Measure-VMReplication | select -expand lastreplicationtime)).totalminutes

This works perfectly. However, we'd like to check ALL of the VMs on this Hyper-V box. I can do this without running the new-timespan by running this command -

get-vm | where {$_.replicationstate -ne "Disabled"} | Measure-VMReplication | select vmname, lastreplicationtime

This gives me every VM on the box and its last replication time.

Ultimately, what I'd like for output is the VMName and LastReplicationTime in minutes from current date/time. If I can do this on one line, homerun!

Any suggestions? No matter how I try to meld the two together, I just can't seem to get the syntax right (Or it's not possible.)

1 Answer 1

2

I can't test it but give this a try:

get-vm | where {$_.replicationstate -ne "Disabled"} | Measure-VMReplication |
    select vmname, @{n="LastReplicationTime";e={ (new-timespan -start ($_.lastreplicationtime) -end (get-date)).TotalMinutes }}
Sign up to request clarification or add additional context in comments.

4 Comments

That seems to have done the trick! I may have some more tweaking to go, but I'll open a new thread if I do! THANKS!!
OK - follow-up - not sure of protocol. If I need to open another question, let me know. Any easy way to just show the vms where lastreplication time > say 1?
add this | ? { $_.lastreplicationtime -gt 1 } after the code above.
OUTSTANDING! Thanks so much, exactly what I needed! I couldn't figure that part out for the life of me - so simple, I didn't think I could use $_.lastreplicationtime at that point since we were into expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.