VMware Cloud Community
lorried82
Enthusiast
Enthusiast
Jump to solution

exclude list

Hi - Looking to do a query against all my datastores to find out what is at 90% used space but exclude a list of datastores from the output. I can't seem to get the content read in to exclude  my list and not sure what I am doing wrong. It seems like it should be a fairly easy script - any help is appreciated.

------------------------------------------------------------------------------

#powershell script to output the datastores that over over 90%used

#add datastores to be excluded

$exceptiondatastores = Get-Content C:\exceptiondatastores.txt <<this file contains a list of datastores as well as * at the end for wildcard

Get-Datastore |

Select Name, @{N='UsedPerc';E={($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB}} |

where{$_.'UsedPerc' -ge 0.90  -and $_.Name -notmatch $exceptiondatastores} |

Select Name -expandproperty Name |Sort-Object -Property Name |

out-file  C:\diskusage90.txt

Disconnect-VIServer -Server xxxx -Confirm:$false

------------------------------------------------------------------------------

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The problem when using the 'like' syntax is that you can not combine multiple masks together.

With the RegEx syntax (used with -match) you can do that.

For example $test -match "abc|klm|xyz", will return true if $test has 'abc' or 'klm' or 'xyz' anywhere in the string.

If you want to be more specific, for example to specify that 'snap' should appear at the beginning, you should use the RegEx syntax "^snap" (where ^ indicates at the beginning of the string).

If you use the RegEx notation, the condition can be made a lot easier.

Something like this for example (but you have to get rid of the like meta-characters in your .txt file)

$exceptiondatastores = (Get-Content C:\exceptiondatastores.txt) -join '|'

Get-Datastore |

   ForEach-Object -Process {

   $usedPerc = ($_.CapacityGB - $_.FreeSpaceGB) / $_.CapacityGB

   $_ | where {$usedPerc -ge 0.9 -and $_.Name -notmatch $exceptiondatastores} |

   Select-Object Name, @{N = 'UsedPerc'; E = {$usedPerc}}

} | Out-File  C:\diskusage90.txt


Disconnect-VIServer -Server xxxx -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure why you have the wildcard at the end of the .txt file?

If you leave it out, you could do

The script assumes the .txt file contains the name of 1 datastore per line.

#powershell script to output the datastores that over over 90%used

#add datastores to be excluded


$exceptiondatastores = Get-Content C:\exceptiondatastores.txt # this file contains a list of datastores as well as * at the end for wildcard

Get-Datastore |

ForEach-Object -Process {

   $usedPerc = ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB

   if($usedPerc -ge 0.9 -and $exceptiondatastores -notcontains $_.Name) |

   Select-Object Name, @{N='UsedPerc';E={$usedPerc}}

} | Out-File  C:\diskusage90.txt

Disconnect-VIServer -Server xxxx -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lorried82
Enthusiast
Enthusiast
Jump to solution

Thanks so much LucD. I am using a wildcard if I want to exclude datastores that start with a certain name, like snap* rather than have to maintain a full list

I am getting an error with your code missing statement block after if ( condition ) - any thoughts on that?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The problem when using the 'like' syntax is that you can not combine multiple masks together.

With the RegEx syntax (used with -match) you can do that.

For example $test -match "abc|klm|xyz", will return true if $test has 'abc' or 'klm' or 'xyz' anywhere in the string.

If you want to be more specific, for example to specify that 'snap' should appear at the beginning, you should use the RegEx syntax "^snap" (where ^ indicates at the beginning of the string).

If you use the RegEx notation, the condition can be made a lot easier.

Something like this for example (but you have to get rid of the like meta-characters in your .txt file)

$exceptiondatastores = (Get-Content C:\exceptiondatastores.txt) -join '|'

Get-Datastore |

   ForEach-Object -Process {

   $usedPerc = ($_.CapacityGB - $_.FreeSpaceGB) / $_.CapacityGB

   $_ | where {$usedPerc -ge 0.9 -and $_.Name -notmatch $exceptiondatastores} |

   Select-Object Name, @{N = 'UsedPerc'; E = {$usedPerc}}

} | Out-File  C:\diskusage90.txt


Disconnect-VIServer -Server xxxx -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

lorried82
Enthusiast
Enthusiast
Jump to solution

ah ok that makes sense. I update the txt to reflect that and now I am able to exclude both specific names as well as ones starting with a certain name.

thanks so much

0 Kudos