PowerShell script exchange
Post Reply
mriesenbeck
Enthusiast
Posts: 45
Liked: 6 times
Joined: Apr 07, 2021 10:07 am
Full Name: Michael Riesenbeck
Contact:

V12 Retrieve Repo usage data

Post by mriesenbeck »

Hello,

I am trying to build a script to retrieve the totalspace, freespace and freespace in percentage of a specific repo.

Basically I want to be able to put totalspace, freespace and freespace_percentage into three variables in order to pick those values up in nCentral.

I've searched the forum, found some examples, looked into the manuals for syntax help, but I'm not quite there.

Code: Select all

$repos = Get-VBRBackupRepository -Name "IMMUTABLE Backup Repository 1"
$repoReport = @()
foreach ($repo in $repos) {
               $repo.GetContainer()
               $totalSpace = $container.CachedTotalSpace
               $totalFreeSpace = $container.CachedFreeSpace
               $totalFreeSpaceProcent = $totalFreeSpace / $totalSpace * 100
               $totalFreeSpaceProcentRounded = [Math]::Round($totalFreeSpaceProcent,1)
               
  $repoReport += $repo | select Name, @{n='TotalSpace';e={$totalSpace}},  `
  @{n='FreeSpace';e={$totalFreeSpace}}
}
$repoReport
$totalFreeSpaceProcentRounded
If I run this, I get:

Code: Select all

PS C:\Windows\system32> $repos = Get-VBRBackupRepository -Name "IMMUTABLE Backup Repository 1"
PS C:\Windows\system32> $repoReport = @()
PS C:\Windows\system32> foreach ($repo in $repos) {
>>                $repo.GetContainer()
>>                $totalSpace = $container.CachedTotalSpace
>>                $totalFreeSpace = $container.CachedFreeSpace
>>                $totalFreeSpaceProcent = $totalFreeSpace / $totalSpace * 100
>>                $totalFreeSpaceProcentRounded = [Math]::Round($totalFreeSpaceProcent,1)
>>
>>   $repoReport += $repo | select Name, @{n='TotalSpace';e={$totalSpace}},  `
>>   @{n='FreeSpace';e={$totalFreeSpace}}
>> }


Id               : 1101d825-84ec-4f70-a4bf-71b38704eea8
Type             : Default
CachedTotalSpace : 18,1 TB (19998980440064)
CachedFreeSpace  : 2,87 TB (3158485389312)
UniqueKey        : 1101D825-84EC-4F70-A4BF-71B38704EEA8
Info             : Veeam.Backup.Model.CRepositoryContainerInfo



PS C:\Windows\system32> $repoReport

Name                              TotalSpace               FreeSpace
----                              ----------               ---------
IMMUTABLE Backup Repository 1 18,1 TB (19998980440064) 2,87 TB (3159579332608)


PS C:\Windows\system32> $totalFreeSpaceProcentRounded
15,8
PS C:\Windows\system32>
Now, the $totalFreeSpaceProcentRounded is perfect, but I need the actual sizes of totalspace and totalfreespace in TB in seperate variables as well, similar to my own created $totalFreeSpaceProcentRounded one.

I'm using the foreach method, since using a separate line gave me the wrong repo, probably the default one, but the foreach does retrieve the correct info for the correct repo. If I just use the $totalSpace at the end, I get a whole list of all values, so that didn't help:

Code: Select all

PS C:\Windows\system32> $totalSpace

InBytes            : 19998980440064
InKilobytes        : 19530254336
InMegabytes        : 19072514
InGigabytes        : 18625
InTerabytes        : 18
InPetabytes        : 0
InBytesAsInt32     :
InKilobytesAsInt32 :
InMegabytesAsInt32 : 19072514
InGigabytesAsInt32 : 18625
InTerabytesAsInt32 : 18
InPetabytesAsInt32 : 0
InBytesAsUInt64    : 19998980440064
IsInvalid          : False
So all I really want is a script to give me this output:

Code: Select all

$reponame
IMMUTABLE Backup Repository 1

$TotalSpace 
18     
         
$FreeSpace
2

$totalFreeSpaceProcentRounded
15,8
So if anyone can offer my some assistance, that would be greatly appreciated. I'm new to powershell.

Thanks in advance for any assistance you can give me.
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: V12 Retrieve Repo usage data

Post by david.domask »

Hi @mriesenbeck

It looks like the report saves PSCustomObjects to the array $repoReport. You're calling the variable $TotalSpace, etc, which was declared and set during the last iteration of the loop. You need to instead call from the index of the array. I've adjusted the code a bit to be more friendly and fix a bug in the code you presented:

Code: Select all

$repos = Get-VBRBackupRepository | Where-Object {$_.GetContainer().Type -ne "ObjectStorage"}
$repoReport = @()
foreach ($repo in $repos) {
	$container = $repo.GetContainer()
	$totalSpace = $container.CachedTotalSpace
	$totalFreeSpace = $container.CachedFreeSpace
	$totalFreeSpaceProcent = $totalFreeSpace / $totalSpace * 100
	$totalFreeSpaceProcentRounded = [Math]::Round($totalFreeSpaceProcent,1)
	$RepoDataHash = [ordered]@{
		RepoName = $repo.name
		TotalSpace = $TotalSpace
		TotalFreeSpace = $totalFreeSpace
		TotalFreeSpacePercent = $totalFreeSpaceProcent
		TotalFreeSpacePercentRounded = $totalFreeSpaceProcentRounded
		}
	$RepoData = [PSCustomObject]$RepoDataHash
	$repoReport += $RepoData
}
1. Get All repositories
2. initialize the $repoReport array
3. Save all the properties and then create an ordered hash list that we will save to a PSCustomObject. We use Ordered because items from Lists are returned unordered in Powershell, so we specify order for consistency
4. We save the PSCustomObject to $ReportData, then just add that to the $repoReport

You can call items from an array by index number; remember Powershell starts its indices at 0, so to call the 1st object, you'd do $repoReport[0]. Read more here on how to call items from an array, but also see my example below:

Link: https://learn.microsoft.com/en-us/power ... y-elements

Code: Select all

PS C:\Users\Administrator> $repoReport | Select -first 3


RepoName                     : Default Backup Repository
TotalSpace                   : 99.3 GB (106727206912)
TotalFreeSpace               : 23.0 GB (24714321920)
TotalFreeSpacePercent        : 23.1565339664307
TotalFreeSpacePercentRounded : 23.2

RepoName                     : refs4k-dedup
TotalSpace                   : 39.9 GB (42882564096)
TotalFreeSpace               : 27.0 GB (29037490176)
TotalFreeSpacePercent        : 67.7139783689114
TotalFreeSpacePercentRounded : 67.7

RepoName                     : restore-test
TotalSpace                   : 99.3 GB (106727206912)
TotalFreeSpace               : 23.0 GB (24714137600)
TotalFreeSpacePercent        : 23.1563612644502
TotalFreeSpacePercentRounded : 23.2

PS C:\Users\Administrator> $repoReport[0]


RepoName                     : Default Backup Repository
TotalSpace                   : 99.3 GB (106727206912)
TotalFreeSpace               : 23.0 GB (24714321920)
TotalFreeSpacePercent        : 23.1565339664307
TotalFreeSpacePercentRounded : 23.2



PS C:\Users\Administrator> $repoReport[3]


RepoName                     : perjob
TotalSpace                   : 39.9 GB (42882564096)
TotalFreeSpace               : 27.0 GB (29038243840)
TotalFreeSpacePercent        : 67.715735875758
TotalFreeSpacePercentRounded : 67.7



PS C:\Users\Administrator> $repoReport[1..3]


RepoName                     : refs4k-dedup
TotalSpace                   : 39.9 GB (42882564096)
TotalFreeSpace               : 27.0 GB (29037490176)
TotalFreeSpacePercent        : 67.7139783689114
TotalFreeSpacePercentRounded : 67.7

RepoName                     : restore-test
TotalSpace                   : 99.3 GB (106727206912)
TotalFreeSpace               : 23.0 GB (24714137600)
TotalFreeSpacePercent        : 23.1563612644502
TotalFreeSpacePercentRounded : 23.2

RepoName                     : perjob
TotalSpace                   : 39.9 GB (42882564096)
TotalFreeSpace               : 27.0 GB (29038243840)
TotalFreeSpacePercent        : 67.715735875758
TotalFreeSpacePercentRounded : 67.7
David Domask | Product Management: Principal Analyst
mriesenbeck
Enthusiast
Posts: 45
Liked: 6 times
Joined: Apr 07, 2021 10:07 am
Full Name: Michael Riesenbeck
Contact:

Re: V12 Retrieve Repo usage data

Post by mriesenbeck »

Hi David,

Thanks so much, this looks great! But unfortunately if I execute this on the Veeam server, I get stuck in this >> thing:

Code: Select all

>> $repos = Get-VBRBackupRepository | Where-Object {$_.GetContainer().Type -ne "ObjectStorage"}
>> $repoReport = @()
>> foreach ($repo in $repos) {
>> $container = $repo.GetContainer()
>> $totalSpace = $container.CachedTotalSpace
>> $totalFreeSpace = $container.CachedFreeSpace
>> $totalFreeSpaceProcent = $totalFreeSpace / $totalSpace * 100
>> $totalFreeSpaceProcentRounded = [Math]::Round($totalFreeSpaceProcent,1)
>> $RepoDataHash = [ordered]@{
>> RepoName = $repo.name
>> TotalSpace = $TotalSpace
>> TotalFreeSpace = $totalFreeSpace
>> TotalFreeSpacePercent = $totalFreeSpaceProcent
>> TotalFreeSpacePercentRounded = $totalFreeSpaceProcentRounded
>> }
>> $RepoData = [PSCustomObject]$RepoDataHash
>> $repoReport += $RepoData
>>
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: V12 Retrieve Repo usage data

Post by david.domask » 1 person likes this post

Hi @mriesenbeck! If that's what was in your shell, you forgot the closing curly brace } Add it as the last entry.

(hint: Use Notepad++ or something like VScode with a Powershell linter; it will help you catch unclosed braces, unused/undeclared variables, etc)
David Domask | Product Management: Principal Analyst
mriesenbeck
Enthusiast
Posts: 45
Liked: 6 times
Joined: Apr 07, 2021 10:07 am
Full Name: Michael Riesenbeck
Contact:

Re: V12 Retrieve Repo usage data

Post by mriesenbeck »

Hi,

The curly brace was in, I copied your code and just pasted it into the Powershell window.
mriesenbeck
Enthusiast
Posts: 45
Liked: 6 times
Joined: Apr 07, 2021 10:07 am
Full Name: Michael Riesenbeck
Contact:

Re: V12 Retrieve Repo usage data

Post by mriesenbeck »

This I copied and pasted:

Code: Select all

$repos = Get-VBRBackupRepository | Where-Object {$_.GetContainer().Type -ne "ObjectStorage"}
$repoReport = @()
foreach ($repo in $repos) {
	$container = $repo.GetContainer()
	$totalSpace = $container.CachedTotalSpace
	$totalFreeSpace = $container.CachedFreeSpace
	$totalFreeSpaceProcent = $totalFreeSpace / $totalSpace * 100
	$totalFreeSpaceProcentRounded = [Math]::Round($totalFreeSpaceProcent,1)
	$RepoDataHash = [ordered]@{
		RepoName = $repo.name
		TotalSpace = $TotalSpace
		TotalFreeSpace = $totalFreeSpace
		TotalFreeSpacePercent = $totalFreeSpaceProcent
		TotalFreeSpacePercentRounded = $totalFreeSpaceProcentRounded
		}
	$RepoData = [PSCustomObject]$RepoDataHash
	$repoReport += $RepoData
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: V12 Retrieve Repo usage data

Post by david.domask » 1 person likes this post

Aha, look a little closer at my post :) Still missing the closing } for the ForEach loop

Image

This might help you see the difference:

Image

Left is what you posted, right is my original code.
David Domask | Product Management: Principal Analyst
mriesenbeck
Enthusiast
Posts: 45
Liked: 6 times
Joined: Apr 07, 2021 10:07 am
Full Name: Michael Riesenbeck
Contact:

Re: V12 Retrieve Repo usage data

Post by mriesenbeck »

I feel really stupid, but I copied the original script again and executed it, the result being:

Code: Select all

PS C:\Windows\system32> $repos = Get-VBRBackupRepository | Where-Object {$_.GetContainer().Type -ne "ObjectStorage"}
PS C:\Windows\system32> $repoReport = @()
PS C:\Windows\system32> foreach ($repo in $repos) {
>> $container = $repo.GetContainer()
>> $totalSpace = $container.CachedTotalSpace
>> $totalFreeSpace = $container.CachedFreeSpace
>> $totalFreeSpaceProcent = $totalFreeSpace / $totalSpace * 100
>> $totalFreeSpaceProcentRounded = [Math]::Round($totalFreeSpaceProcent,1)
>> $RepoDataHash = [ordered]@{
>> RepoName = $repo.name
>> TotalSpace = $TotalSpace
>> TotalFreeSpace = $totalFreeSpace
>> TotalFreeSpacePercent = $totalFreeSpaceProcent
>> TotalFreeSpacePercentRounded = $totalFreeSpaceProcentRounded
>> }
>> $RepoData = [PSCustomObject]$RepoDataHash
>> $repoReport += $RepoData
>> }
PS C:\Windows\system32>
I think all brackets are correct, since it's the original code you posted, but maybe something is going wrong while copy and paste? Sometimes strange things can happen with " or ' I know from the past.
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: V12 Retrieve Repo usage data

Post by david.domask »

Glad that it looks to have worked @mriesenbeck! And I would just chop such issues up to "cosmic rays" :) (i.e., I wouldn't worry on it, this just kind of happens when copying code sometimes :/)

Everything should be good now for you, all your data is in $repoReport array, and you can call from the index as I showed earlier. I do repeat my recommendation of write/check your code in Notepad++ or VSCode (np++ has built-in syntax highlighting for Powershell, VSCode has a Powershell plugin. VScode is a little sluggish/over-built for simple scripting in my opinion, but it's just a preference)
David Domask | Product Management: Principal Analyst
mriesenbeck
Enthusiast
Posts: 45
Liked: 6 times
Joined: Apr 07, 2021 10:07 am
Full Name: Michael Riesenbeck
Contact:

Re: V12 Retrieve Repo usage data

Post by mriesenbeck »

Ah! Now I understand the secondary code. I was waiting for data to appear :) Very nice, thank you lots!
mriesenbeck
Enthusiast
Posts: 45
Liked: 6 times
Joined: Apr 07, 2021 10:07 am
Full Name: Michael Riesenbeck
Contact:

Re: V12 Retrieve Repo usage data

Post by mriesenbeck »

One last question.

Since I need to integrate this into ncentral, ncentral will supply the repo name as input value to do the lookup. I would like to get as a result four separate filled variables, eg:

Code: Select all

$RepoName                     
refs4k-dedup

$RepoTotalSpace
39.9 GB (42882564096)

$RepoTotalSpaceFree
27.0 GB (29037490176)

$RepoTotalSpaceFreePercentRounded
67.7
This, so nCentral can use it's thresholds against eg. the $RepoTotalSpaceFreePercentRounded.

Is there a way to achieve this as well?

Cheers,

Michael
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: V12 Retrieve Repo usage data

Post by david.domask »

Hi @mriesenbeck,

I'm afraid I can't comment much on passing the data to nAble as I've never used it directly, but you can call individual items from the $reportData array as I mentioned here:

You can call items from an array by index number; remember Powershell starts its indices at 0, so to call the 1st object, you'd do $repoReport[0]. Read more here on how to call items from an array, but also see my example below:

Link: https://learn.microsoft.com/en-us/power ... y-elements

Check my examples from original post. You will need to write some code yourself here for passing it to nAble as I just am not sure how it "wants" the data or what it's expecting as input.

But you can call for example all the details for a given repository by just calling it from the index:

Code: Select all

PS C:\Users\Administrator> $repoReport[0]


RepoName                     : Default Backup Repository
TotalSpace                   : 99.3 GB (106727206912)
TotalFreeSpace               : 23.0 GB (24714321920)
TotalFreeSpacePercent        : 23.1565339664307
TotalFreeSpacePercentRounded : 23.2



PS C:\Users\Administrator> $repoReport[0].RepoName
Default Backup Repository
PS C:\Users\Administrator> $repoReport[0].TotalFreeSpace


InBytes            : 24714321920
InKilobytes        : 24135080
InMegabytes        : 23569
InGigabytes        : 23
InTerabytes        : 0
InPetabytes        : 0
InBytesAsInt32     :
InKilobytesAsInt32 : 24135080
InMegabytesAsInt32 : 23569
InGigabytesAsInt32 : 23
InTerabytesAsInt32 : 0
InPetabytesAsInt32 : 0
InBytesAsUInt64    : 24714321920
IsInvalid          : False



PS C:\Users\Administrator> $repoReport[0].TotalFreeSpacePercent
23.1565339664307
PS C:\Users\Administrator> $repoReport[0].TotalFreeSpacePercentRounded
23.2
PS C:\Users\Administrator>
David Domask | Product Management: Principal Analyst
mriesenbeck
Enthusiast
Posts: 45
Liked: 6 times
Joined: Apr 07, 2021 10:07 am
Full Name: Michael Riesenbeck
Contact:

Re: V12 Retrieve Repo usage data

Post by mriesenbeck »

Hi,

This helps! The difficult thing was/is, like you see in your example of $repoReport[0].TotalFreeSpace, is that I would only like to retrieve the "InTerabytes" part of the output, not the others, but I see that this works:

Code: Select all

$repoReport[0].TotalFreeSpace.InTerabytes
So I'm a very happy camper!

Thank you again!
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: V12 Retrieve Repo usage data

Post by david.domask » 1 person likes this post

Glad I could help! Yes, you can just continue down the properties like that, and you can even call them in Select-Object calls (select), etc.

Glad this is enough to get your integration going and always happy to hear about happy campers ;)
David Domask | Product Management: Principal Analyst
JRRW
Enthusiast
Posts: 76
Liked: 45 times
Joined: Dec 10, 2019 3:59 pm
Full Name: Ryan Walker
Contact:

Re: V12 Retrieve Repo usage data

Post by JRRW » 2 people like this post

david.domask wrote: Jan 03, 2024 11:04 am
Image

Left is what you posted, right is my original code.
Excuse me sir - may I ask how you only have $new 2 ????

I feel horribly unwell:
Image

I thought I was normal. I'm not. I'm broken.
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: V12 Retrieve Repo usage data

Post by david.domask »

ahahaa :D it's just more congruent with my mental model for cases :) I use workspaces feature of np++ heavily and prefer just everything "nice and clean"; easier for me to do context-switching when dealing with multiple cases/projects.
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests