Script to get media agents associated to a client/sublicent

Last post 02-23-2018, 2:48 PM by jng. 8 replies.
Sort Posts: Previous Next
  • Script to get media agents associated to a client/sublicent
    Posted: 01-25-2018, 11:36 AM

    Hello, I have a list of about 100 clients, and I would like to do a powershell script, using the REST API, to loop through them, and for each, check for each agent/subclient all storagepolicies associated and for each of those, get all media agents.

    Is this possibe using the REST API or should I go another path?

     

    Thank you!

    Diego

  • Re: Script to get media agents associated to a client/sublicent
    Posted: 01-25-2018, 5:49 PM

    Looking at the POSTMAN collection on https://api.commvault.com

    • GET /Client would give you a list of Clients & their respective Client IDs
    • GET /Subclient would then give you all subclients for a given client irrespective of backupsets, instances, agents
    • GET /Subclient/{x} (Subclient Properties) would then give you the storage policy/policies
    Then...
    • GET /MediaAgent would get you a list of MA's
    • GET /StoragePolicyListAssociatedToMediaAgent would get you a list of Storage Policies associated with each MA
    At which point you could then blend the data and end up with your mapping of subclients <-> media agents.
    Curious question - what's the solution you're trying to script here?

    Cheers,
    Luke
    (@cv_skywalker)
  • Re: Script to get media agents associated to a client/sublicent
    Posted: 01-26-2018, 7:43 AM

    Thanks! I will try that

  • Re: Script to get media agents associated to a client/sublicent
    Posted: 02-22-2018, 11:24 AM

    How'd you go with trying those API calls? :)


    Cheers,
    Luke
    (@cv_skywalker)
  • Re: Script to get media agents associated to a client/sublicent
    Posted: 02-22-2018, 7:59 PM

    Hi Diegoxom,

    here's an example to try:

    $URL = "http://localhost:81/SearchSvr/CVWebService.svc/QCommand"

    $SCPClientArray = @()

    $SubClientPolicyName = "MySubClientPolicy1"

    $RESTHeaders = @{"Accept" = "application/xml";"Authtoken" = $AuthToken}

    $Body = "qoperation execscript -sn GetSubclientPolicy.sql -si scp=$SubClientPolicyName"

    $ResponseXML = Invoke-RestMethod -Uri $URL -Method Post -ContentType 'text/plain' -Headers $RESTHeaders -Body $Body

    <#output will be something like: <?xml version="1.0" encoding="UTF-8" standalone="no"?><ExecScriptOutput><FieldValue BackupsetName="defaultBackupSet" ClientName="proxy01" /><FieldValue BackupsetName="[] or subclientPolicy [MySubClientPolicy1]. ErrorCode (0)." ClientName="GetSubclientPolicycompleted at Feb 15 2018 12:55PM with parameters client " /></ExecScriptOutput>#>

    If($ResponseXML.CVGui_GenericResp.errorCode){ # Error handling } else {

    $ResponseXML.ExecScriptOutput.FieldValue.Clientname | ForEach-Object {
    If($_ -notlike "*GetSubclientPolicycompleted*") {
    $SCPClientArray += $_ }
  • Re: Script to get media agents associated to a client/sublicent
    Posted: 02-23-2018, 11:50 AM

    thanks both for the update and follow up.

    If I hit the URL directly to test I get (from PS)

    HTTP Error 401
    401 Unauthorized: Access is denied due to invalid credentials

    In your script I'm trying to get $AuthToken with no luck yet

  • Re: Script to get media agents associated to a client/sublicent
    Posted: 02-23-2018, 1:43 PM

    Would check two things:

    1. Are you converting your password to Base64 before calling /Login?
    ie $password = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($password))
    2. Try a local commcell user - ie. admin/commvault.  Or if you are trying an AD SSO based user, try domain\user

    Cheers,
    Luke
    (@cv_skywalker)
  • Re: Script to get media agents associated to a client/sublicent
    Posted: 02-23-2018, 1:48 PM

    sorry - you need to first login to CommServe. However, i realised the script may not be useful for you as it assumes that you used subclient policy to assign storage policy. Sky's solution should work for you.. here's a sample up to obtaining the storage policy from the subclient properties

    $WebServer = "HTTP://Localhost:81/SearchSvc/CVWebService.svc/"
    $username = "<username>"
    $password = "<password>"
    $headers = @{}
    $headers["Accept"] = "application/xml"
    $Body = @{
    username = $username
    password = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($password))
    }
    # Login to Commvault REST API
    $Result = Invoke-RestMethod -Method POST -Uri "$WebServer/Login" -Headers $headers -Body $($Body | ConvertTo-Json) -ContentType "application/json"
    # Save QSDK token
    $headers["Authtoken"] = $Result. DM2ContentIndexing_CheckCredentialResp.token
    $Clients = Invoke-RestMethod -Method GET -Uri "$WebServer/Client" -Headers $headers -ContentType "application/xml"
    $ClientStoragePolicyArray = @()

    $Clients.App_GetClientPropertiesResponse.clientProperties.client.cliententity | ForEach-Object {
    $ClientEntity = $_
    $SubClient = Invoke-RestMethod -Method GET -Uri "$WebServer/Subclient?Clientid=$($ClientEntity.clientid)" -Headers $headers -ContentType "application/xml"
    $SubClient.App_GetSubClientPropertiesResponse.subClientProperties.subClientEntity | ForEach-Object {
    $SubClientEntity = $_
    $SubClientProp = Invoke-RestMethod -Method GET -Uri "$WebServer/Subclient/$($SubClientEntity.subclientId)" -Headers $headers -ContentType "application/xml"
    $ClientObj = New-Object -TypeName psobject -Property @{
    ClientName = $ClientEntity.clientName
    ClientId = $ClientEntity.clientid
    BackupSet = $SubClientEntity.backupsetName
    Subclientname = $SubClientEntity.subclientName
    DataStoragePolicyName = $SubClientProp.App_GetSubClientPropertiesResponse.subClientProperties.commonProperties.storageDevice.dataBackupStoragePolicy.storagePolicyName
    LogStoragePolicyName = $SubClientProp.App_GetSubClientPropertiesResponse.subClientProperties.commonProperties.storageDevice.logBackupStoragePolicy.storagePolicyName
    }
    $ClientStoragePolicyArray += $ClientObj
    }
    }
  • Re: Script to get media agents associated to a client/sublicent
    Posted: 02-23-2018, 2:48 PM

    here's the second part - getting the media agent list associated with the storage policies

    $SPHash = @{}
    $MediaAgent = Invoke-RestMethod -Method GET -Uri "$WebServer/MediaAgent" -Headers $headers -ContentType "application/xml"
    $MediaAgent.App_GenericEntityResp.response.entityInfo | ForEach-Object {
    $MA = $_
    $StoragePolicy = Invoke-RestMethod -Method GET -Uri "$WebServer/StoragePolicyListAssociatedToMediaAgent?MediaAgent=$($MA.id)" -Headers $headers -ContentType "application/xml"
    $StoragePolicy.App_GetStoragePoliciesForMediaAgentResp.storagePolicyInformationAssociatedToMA.storagepolicyandcopy | ForEach-Object {
    $SPName = $_.storagePolicyName
    $MAArray = @()
    If(![string]::IsNullOrWhiteSpace($SPName)){
    if($SPHash.containskey($SPName)){
    $MAArray = $SPHash[$SPName]
    $MAArray += $MA.name
    $MAArray = $MAArray | Select-Object -Unique
    $SPHash[$SPName] = $MAArray
    } else {
    $MAArray += $MA.name
    $SPHash[$SPName] = $MAArray
    }
    }}
    }
    the rest is just linking the ClientStoragePolicyArray and SPHash 
The content of the forums, threads and posts reflects the thoughts and opinions of each author, and does not represent the thoughts, opinions, plans or strategies of Commvault Systems, Inc. ("Commvault") and Commvault undertakes no obligation to update, correct or modify any statements made in this forum. Any and all third party links, statements, comments, or feedback posted to, or otherwise provided by this forum, thread or post are not affiliated with, nor endorsed by, Commvault.
Commvault, Commvault and logo, the “CV” logo, Commvault Systems, Solving Forward, SIM, Singular Information Management, Simpana, Commvault Galaxy, Unified Data Management, QiNetix, Quick Recovery, QR, CommNet, GridStor, Vault Tracker, InnerVault, QuickSnap, QSnap, Recovery Director, CommServe, CommCell, SnapProtect, ROMS, and CommValue, are trademarks or registered trademarks of Commvault Systems, Inc. All other third party brands, products, service names, trademarks, or registered service marks are the property of and used to identify the products or services of their respective owners. All specifications are subject to change without notice.
Close
Copyright © 2018 Commvault | All Rights Reserved. | Legal | Privacy Policy