Microsoft Virtualization Discussions

pulling out username object from Get-NaQuotareport

mis_admin
7,409 Views

Hi All,

Firstly apologies, I'm not a coder at heart so I may use incorrect terminology here!

I'm trying to write a script that queries the status of user based quotas in a volume holding our user's home directories. Then email a user directly who's currently over arbitrarily set limits. I cant seem to do this in Ops Mgr (I do have an alert set in Ops Mgr to alert admins when over 80%).

Using Get-NaQuotareport I can see the attribute of the object returned that I want to use as a variable (QuotaUserName) in another commandlet to pull their email address out of Active Directory. I can not it seems get to the vaule of this attribute!

The code I'm using to get the quota object is:

Get-NaQuotareport -Volume CIFS | ForEach-Object {
if (($_.Quotatype -eq "user") -and ($_.QuotaUsers -ne $null))
{
blah blah blah......

I know the first bit of code is successfully getting the right object and can see it setting the QuotaUserName attribute with a valid username. I then use any of the below to set a variable as the QuotaUserName attribute value and just set's it as $null, I do get any errors:

$current_user = $_.QuotaUserName
$current_user = $_.QuotaUsers.QuotaUserName
$current_user = $_.NetApp.Ontapi.Filer.Quota.QuotaUser.QuotaUserName

See here:

http://support.domino-printing.com/quota_object_attributes.png


Can any one tell me where I'm going wrong?

cheers

Zander

1 ACCEPTED SOLUTION

cknight
7,409 Views

Hello, Zander.  QuotaUsers is an array, so you have to specify an index.

PS C:\> Get-NaQuotaReport | ? {$_.QuotaTarget -EQ "rtprre\administrator"}


DiskLimit     : 10485760
DiskUsed      : 0
FileLimit     : -
FilesUsed     : 0
Qtree         :
QuotaTarget   : rtprre\administrator
QuotaType     : user
QuotaUsers    : {RTPRRE\Administrator}
SoftDiskLimit : -
SoftFileLimit : -
Threshold     : -
Vfiler        : vfiler0
Volume        : vol0

PS C:\> $q = Get-NaQuotaReport | ? {$_.QuotaTarget -EQ "rtprre\administrator"}



PS C:\> $q.QuotaUsers

QuotaUserId                             QuotaUserName                           QuotaUserType
-----------                             -------------                           -------------
S-1-5-21-1088268088-1183557120-14543... RTPRRE\Administrator                    sid


P
S C:\> $q.QuotaUsers[0].QuotaUserName
RTPRRE\Administrator

View solution in original post

3 REPLIES 3

cknight
7,410 Views

Hello, Zander.  QuotaUsers is an array, so you have to specify an index.

PS C:\> Get-NaQuotaReport | ? {$_.QuotaTarget -EQ "rtprre\administrator"}


DiskLimit     : 10485760
DiskUsed      : 0
FileLimit     : -
FilesUsed     : 0
Qtree         :
QuotaTarget   : rtprre\administrator
QuotaType     : user
QuotaUsers    : {RTPRRE\Administrator}
SoftDiskLimit : -
SoftFileLimit : -
Threshold     : -
Vfiler        : vfiler0
Volume        : vol0

PS C:\> $q = Get-NaQuotaReport | ? {$_.QuotaTarget -EQ "rtprre\administrator"}



PS C:\> $q.QuotaUsers

QuotaUserId                             QuotaUserName                           QuotaUserType
-----------                             -------------                           -------------
S-1-5-21-1088268088-1183557120-14543... RTPRRE\Administrator                    sid


P
S C:\> $q.QuotaUsers[0].QuotaUserName
RTPRRE\Administrator

mis_admin
7,409 Views

Hi Clinton

Thanks for your reply, that did it and I can now set the username as a variable to then get their details from AD.

I'm still not understanding it 100% but will read up on arrays to try to understand it more..

What confused me is that the powerGUI tool had put a [1] in the array index but $_.QuotaUsers[1].QuotaUserName still returned $null. Once I changed it to a zero it was fine.

Thanks for your help

Zander

mis_admin
7,410 Views

Well I've finally got my script working so I thought I'd share. Feel free to use or make suggestions. It will not yet run fully unattended which I'll eventually do as I plan to have this running as a scheduled task and I'd like to create a role (if possible) that only has read access to quota info so I can put a username and password into the script. I've used the NetApp and PowerGUI Active Directory commandlets in the script:

# make sure we have the NetApp Commandlets installed
Import-Module DataONTAP

# conenct to AD using the account credentials running the script
Add-PSSnapin Quest.ActiveRoles.ADManagement
Connect-QADService

# setup the email user function
function send_email_user
{
      $mailmessage = New-Object system.net.mail.mailmessage
     $mailmessage.from = "<an email address>"
      $mailmessage.To.add($current_user_email)
      $mailmessage.Subject = $email_subj
      $mailmessage.Body = $email_body
      $mailmessage.IsBodyHtml = $false
      $mailmessage.Priority = "High"
      $SmtpClient = New-Object system.net.mail.smtpClient
     $SmtpClient.Host = "<internal mail server>"
      $smtpclient.Send($mailmessage)

}

# connect to the controller holding the user's home directory
# area
Connect-NaController <FAS controller> -Credential <username> -ForceSecure

# get a quota report of the entire CIFS volumes and then filter
# out based on user based quotas
Get-NaQuotareport  -Volume <volume holding home directories> | ForEach-Object {
      if (($_.Quotatype -eq "user") -and ($_.QuotaUsers -ne $null) -and ($_.DiskLimit -ne '-'))
      {
           $max_space = [Int32] ($_.DiskLimit / 1024000)
           $current_usage = [Int32] ($_.DiskUsed / 1024000)
           $current_user_email = (Get-QADUser $_.QuotaUsers[0].QuotaUserName).Email
           $current_user_name = (Get-QADUser $_.QuotaUsers[0].QuotaUserName).Name
           $90percent = (($_.DiskLimit / 100) * 90)
# if user is null then ignore
           if ($_.QuotaUsers[0].QuotaUserName -eq $null)
           {
                return
           }
# if utilisation is under soft limit then ignore
           elseif ([Int32] $_.DiskUsed -le $_.SoftDiskLimit)
           {
                Write-Host $current_user_name
                Write-Host "Under soft limit. Press any key to continue or 'Ctrl+C' to exit if errors shown..."
                $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyup")
                return
           }
# if utilisation is equal to or greater than the soft limit (80%)
# and then less than 90%, email a gentle warning
           elseif ([Int32] ($_.DiskUsed -lt $90percent) -and ($_.DiskUsed -ge $_.SoftDiskLimit))
           {
                $email_subj = [string] "Please be aware your home drive is close to your storage limit"
                $email_body = [string] ("Dear " + $current_user_name + "`r`n" + "`r`n" + "Your home directory is nearly full, you are currently using" + $current_usage + "Mb out of a maximum of " + $max_space + "Mb." + "`r`n" + "`r`n" + "Please can you review the contents of your home directory." + "`r`n" + "`r`n" + "Home Directory data <SNIP>.")
                Write-Host $current_user_name
                Write-Host "Between 80% and 90%. Press any key to continue or 'Ctrl+C' to exit if errors shown..."
                $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyup")
                send_email_user
           }
# if utilisation is equal to or greater than 90% and less than
# the disk limit email a more urgent warning
           elseif ([Int32] ($_.DiskUsed -ge $90percent) -and ($_.DiskUsed -lt $_.DiskLimit))
           {
                $email_subj = [string] "Please be aware your home drive is nearly full"
                $email_body = [string] ("Dear " + $current_user_name + "`r`n" + "`r`n" + "Your home directory is very nearly full, you are currently using" + $current_usage + "Mb out of a maximum of " + $max_space + "Mb." + "`r`n" + "`r`n" + "Please can you review the contents of your home directory." + "`r`n" + "`r`n" + "Home Directory data <SNIP>.")
                Write-Host $current_user_name
                Write-Host "Between 90% and 100%. Press any key to continue or 'Ctrl+C' to exit if errors shown..."
                $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyup")
                send_email_user
           }
# only left option now is if the area full so email critical
# warning
           elseif ([Int32] $_.DiskUsed -ge $_.DiskLimit)
           {
                $email_subj = [string] "WARNING! Your home drive full!"
                $email_body = [string] ("Dear " + $current_user_name + "`r`n" + "`r`n" + "Your home directory is full!!!" + "`r`n" + "`r`n" + "Please can you review the contents of your home directory immediately." + "`r`n" + "`r`n" + "Home Directory data <SNIP>.")
                Write-Host $current_user_name
                Write-Host "FULL!!! Press any key to continue or 'Ctrl+C' to exit if errors shown..."
                $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyup")
                send_email_user
           }
      }
}

Public