PowerShell script exchange
Post Reply
MikeLeone
Enthusiast
Posts: 25
Liked: 2 times
Joined: Sep 21, 2022 3:03 pm
Full Name: Michael Leone
Contact:

Query library for tapes in slots that are FULL?

Post by MikeLeone »

I am trying to write a script that queries my library for all tapes in slots that are marked FULL. But I cn't seem to select correctly.

First I tried getting all tapes ever marked FULL, then select out only the ons in the slots. It's that 2nd filtering that isn't working.
(ordinarily I'd have made a compound WHERE, but I am breaking it up into pieces to troubleshoot).

[String] $LibraryName = Get-VBRTapeLibrary | Select -Expandproperty Name
$TapesToBeEjected = Get-VBRTapeMedium -Library $LibraryName | Where {{$_.IsFull -eq $TRUE}} | Select Name, LastWriteTime, MediaPoolID, IsFull -ExpandProperty Location | Sort -Property MediaPoolID, Name
Write-Host "Now limiting to in slots"
$TapeeInSlots = $TapesToBeEjected | WHERE {{$_.Type -eq "Slot"}}
$TapesInSlots | Format-Table

And I get nothing this way.
If I don't "-ExpandProperty Location", and instead just list it without expanding, and do a 2nd where on $_.Location -eq "Slot", I get ... nothing ...

So what am I missing here?
david.domask
Veeam Software
Posts: 3004
Liked: 697 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Query library for tapes in slots that are FULL?

Post by david.domask »

Hi MikeLeone,

In your Select-Object expression, you can pass script blocks to create custom properties by using the syntax

Code: Select all

Select @{Name="name for custom property";Expression={script block}}
You can shorten Name and Expression to n and e respectively.

For example, here's quick code I did just in my lab:

Code: Select all

$libraries = Get-VBRTapeLibrary
$tapes = $tapes = Get-VBRTapeMedium | Where-Object {$_.LibraryId -eq $libraries[0].id}
$tapes | Where-Object {$_.Location.SlotAddress -ne $null -and $_.IsFull -eq $true} | Select Id, Barcode, @{n="SlotAddress";e={$_.Location.SlotAddress}}, IsFull

Id                                   Barcode  SlotAddress IsFull
--                                   -------  ----------- ------
0dd6869d-fa90-4c33-9e83-46177536ba09 H00018L5          17   True
ebed103d-e9ad-4168-9a7b-4fe6eb84fcf6 H00010L5           9   True
be0ad70f-26ea-461f-9491-36b4aa041d6b H00002L5           1   True
332d463f-958f-4bb5-8e16-4ab8626c0f1e H00009L5           8   True
72c033ac-d6f2-47b9-b5c4-5c29c5bfa005 H00022L5          21   True

You can see how in last line we call a script block $_.Location.SlotAddress and set it as the property Slot Address, and this happens for each item that is passed through the pipeline, resulting in the output above.
David Domask | Product Management: Principal Analyst
MikeLeone
Enthusiast
Posts: 25
Liked: 2 times
Joined: Sep 21, 2022 3:03 pm
Full Name: Michael Leone
Contact:

Re: Query library for tapes in slots that are FULL?

Post by MikeLeone » 1 person likes this post

Hmmm ... I didn't notice this as a name

$_.Location.SlotAddress

when I expanded .Location", I saw .Location.Type .. but no matter.

But yes, that resolves my selection criteria. I ran a script every morning that lists any taes that are now FULL in the slots, so my Tape Operators know what tapes to pull out and send offsite. So my scrit still has a bit of mail message formatting. But it's selecting a whole lot faster now!

Thanks!
david.domask
Veeam Software
Posts: 3004
Liked: 697 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Query library for tapes in slots that are FULL?

Post by david.domask »

Happy to help, Mike, and glad script is running faster.

I'm not quite sure why your original code wasn't filtering correctly as it worked in my lab, but perhaps something got copied incorrectly between your shell and the forums.

There's nothing wrong with using -ExpandProperty, but I'm not a fan of it like this because -ExpandProperty has hard to spot side effects.

Give a read through the examples on that page for -ExpandProperty as I find these considerations undesirable usually and non-intuitive. Personally would prefer executing explicit script blocks to get what I want, but probably there are reasons -ExpandProperty is desirable, just can't think of a time I needed it as opposed to just making a custom property.
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: Baidu [Spider] and 6 guests