Validate all SMB shares on a Tintri storage system, check their permissions, and output the results to a file in Hyper-V

NOTE: This is not an offical script from Tintri

To validate all SMB shares on a Tintri storage system, check their permissions, and output the results to a file in Hyper-V, we can use the Tintri REST API along with PowerShell. Below is a PowerShell script that accomplishes this task:

# Specify the Tintri storage system details
$TintriServer = "https://<Tintri_Server_IP>"
$Username = "your_username"
$Password = "your_password"

# Create a credential object
$Credential = New-Object System.Management.Automation.PSCredential ($Username, (ConvertTo-SecureString $Password -AsPlainText -Force))

# Base64 encode the credentials
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Credential.UserName, $Credential.GetNetworkCredential().Password)))

# Function to invoke the Tintri REST API
function Invoke-TintriRestAPI {
    param (
        [string]$Url,
        [string]$Method,
        [string]$Headers,
        [string]$Body
    )

    $result = Invoke-RestMethod -Uri $Url -Method $Method -Headers $Headers -Body $Body -ContentType "application/json"
    return $result
}

# Function to get SMB shares and their permissions
function Get-TintriSMBShares {
    $TintriUrl = "$TintriServer/v310/smbshare"
    $Headers = @{
        "Authorization" = "Basic $base64AuthInfo"
    }

    $smbShares = Invoke-TintriRestAPI -Url $TintriUrl -Method "GET" -Headers $Headers

    return $smbShares
}

# Main script
try {
    # Get the list of SMB shares
    $smbShares = Get-TintriSMBShares

    # Output the results to a file
    $OutputFile = "C:\Temp\Tintri_SMB_Share_Permissions.txt"

    foreach ($share in $smbShares) {
        $shareName = $share.name
        $sharePath = $share.exportPath
        $sharePermissions = $share.permissions

        # Format the permissions information
        $permissionsInfo = ""
        foreach ($permission in $sharePermissions) {
            $permissionsInfo += "User/Group: $($permission.entityName), Access: $($permission.accessRights)`r`n"
        }

        # Write the share details to the output file
        "$shareName ($sharePath)`r`n$permissionsInfo`r`n" | Out-File -FilePath $OutputFile -Append
    }

    Write-Host "SMB share validation completed. Results saved to: $OutputFile"
}
catch {
    Write-Host "Error occurred: $($_.Exception.Message)" -ForegroundColor Red
}

Before running the script, replace <Tintri_Server_IP>, your_username, and your_password with the appropriate values for your Tintri storage system. The script will retrieve all SMB shares from the Tintri storage system, along with their permissions, and output the results to the specified file (C:\Temp\Tintri_SMB_Share_Permissions.txt).

Please note that you need to have PowerShell and the required modules (e.g., Invoke-RestMethod) installed on the Hyper-V server to run this script successfully. Additionally, ensure that the Hyper-V server can reach the Tintri storage system’s IP address and that the specified user has appropriate permissions to access the Tintri REST API.

Leave a comment