Microsoft Virtualization Discussions

PowerShell equivalent of discovery-mode

StanStewart
3,107 Views

After searching the PowerShell Toolkit/module and forums, I've been unable to find this one. Is there a command that performs the equivalent of this CLI command?

vserver cifs domain discovered-servers discovery-mode [modify|show] ...

 

Can you point me to a definitive answer?

 

Thanks!

1 ACCEPTED SOLUTION

mbeattie
3,046 Views

Hi Stan,

 

If there is no native cmdlet available in the PSTK you can you use "Invoke-NcSystemApi". Note that you can use a semicolon to concatenate commands (useful if you need to set the CLI privilege to advanced or diag mode to run a command). Here's an example:

 

 

#'------------------------------------------------------------------------------
#'Set the command to invoke.
#'------------------------------------------------------------------------------
$cluster    = "192.168.100.2"
$credential = Get-Credential
$command    = "set advanced;vserver cifs domain discovered-servers discovery-mode show"
$cmd        = $command.Split(" ")
$api        = $("<system-cli><args><arg>" + ($cmd -join "</arg><arg>") + "</arg></args></system-cli>")
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
   Connect-NcController -Name $cluster -Credential $credential -ErrorAction Stop | Out-Null
   Write-Host "Connected to cluster ""$cluster"""
}Catch{
   Write-Warning -Message $("Failed connecting to cluster ""$cluster"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Invoke the system API.
#'------------------------------------------------------------------------------
Try{
   $output = Invoke-NcSystemApi -Request $api -ErrorAction Stop
   Write-Host $("Executed Command`: " + $([String]::Join(" ", $command)))
}Catch{
   Write-Warning -Message $("Failed Executing Command`: " + $([String]::Join(" ", $command)) + ". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Display the CLI output.
#'------------------------------------------------------------------------------
If($Null -ne $output){
   If($output.results.'cli-result-value' -eq 1){
      $output.results.'cli-output'
   }Else{
      Write-Warning -Message $("Failed Executing Command`: " + $([String]::Join(" ", $command)) + ". Error " + $_.Exception.Message)
   }
}
#'------------------------------------------------------------------------------

 

 

Here's what the output looks like:

 

 

 

PS C:\Scripts\PowerShell\Projects\GetVserverDomainDiscovery> .\GetVserverDomainDiscovery.ps1

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Connected to cluster "192.168.100.2"
Executed Command: set advanced;vserver cifs domain discovered-servers discovery-mode show

Vserver         Mode
--------------- ----------
vserver1        all
vserver2        all
vserver3        all
3 entries were displayed.

 

 

Hope that helps

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

View solution in original post

7 REPLIES 7

mbeattie
3,047 Views

Hi Stan,

 

If there is no native cmdlet available in the PSTK you can you use "Invoke-NcSystemApi". Note that you can use a semicolon to concatenate commands (useful if you need to set the CLI privilege to advanced or diag mode to run a command). Here's an example:

 

 

#'------------------------------------------------------------------------------
#'Set the command to invoke.
#'------------------------------------------------------------------------------
$cluster    = "192.168.100.2"
$credential = Get-Credential
$command    = "set advanced;vserver cifs domain discovered-servers discovery-mode show"
$cmd        = $command.Split(" ")
$api        = $("<system-cli><args><arg>" + ($cmd -join "</arg><arg>") + "</arg></args></system-cli>")
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
   Connect-NcController -Name $cluster -Credential $credential -ErrorAction Stop | Out-Null
   Write-Host "Connected to cluster ""$cluster"""
}Catch{
   Write-Warning -Message $("Failed connecting to cluster ""$cluster"". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Invoke the system API.
#'------------------------------------------------------------------------------
Try{
   $output = Invoke-NcSystemApi -Request $api -ErrorAction Stop
   Write-Host $("Executed Command`: " + $([String]::Join(" ", $command)))
}Catch{
   Write-Warning -Message $("Failed Executing Command`: " + $([String]::Join(" ", $command)) + ". Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Display the CLI output.
#'------------------------------------------------------------------------------
If($Null -ne $output){
   If($output.results.'cli-result-value' -eq 1){
      $output.results.'cli-output'
   }Else{
      Write-Warning -Message $("Failed Executing Command`: " + $([String]::Join(" ", $command)) + ". Error " + $_.Exception.Message)
   }
}
#'------------------------------------------------------------------------------

 

 

Here's what the output looks like:

 

 

 

PS C:\Scripts\PowerShell\Projects\GetVserverDomainDiscovery> .\GetVserverDomainDiscovery.ps1

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Connected to cluster "192.168.100.2"
Executed Command: set advanced;vserver cifs domain discovered-servers discovery-mode show

Vserver         Mode
--------------- ----------
vserver1        all
vserver2        all
vserver3        all
3 entries were displayed.

 

 

Hope that helps

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

StanStewart
3,011 Views

Thanks. We had some initial challenges with this approach, so were hoping the actual command would be in 9.14. So far, I've been unable to find it, so the API method you shared may come in handy. Much appreciated!

JimRobertson
2,624 Views

Thank you, @mbeattie, this worked great for me, and it is WAY better than the Invoke-NcSSH.  Now I have to go back and tweak some other scripts where I was forced to use SSH as well!

For anyone coming from the other thread I started, this was the URL I used to find all of the "Deprioritized" volumes on a cluster:

$uri = https://$OnTapCluster/api/private/cli/volume/efficiency?auto-state=Deprioritized&fields=auto-state,schedule,compression,inline-compression,inline-dedu...

Jim_Robertson
2,464 Views

@mbeattie in the API docs, it mentions a way to display the available OPTIONS for a particular command.  Is there a way to initiate this through PowerShell?  I can't figure out a way to add the "--include" to the PowerShell command, and if I just swap GET for OPTIONS the output is blank.

curl -X OPTIONS "https://<mgmt-ip>/api/private/cli/volume" --include
Allow: GET, HEAD, OPTIONS, POST, DELETE, PATCH
{
}



mbeattie
2,430 Views

Hi Jim,

 

Good question, i'm not sure, i've never looked into trying to display the options via the API. I generally just use the Swagger UI to review the documentation. I believe the curl command is an alias to the Invoke-WebRequest in windows. Perhaps it would be an option to install curl and use it natively?

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

StanStewart
2,937 Views

We are also looking at using invoke-ncssh as an option since the CLI options are well-known to us.

 

The other alternative as many on this forum know is to use the "-template" flag, parse out the equivalent of "discovery-mode" from one of the native commands, and then modify that object. However, I have been unable to find the existing command (I have 9.10 and 9.14 versions of the Netapp Toolkit/module) that includes this object. As you know, the equivalent of "vserver cifs ..." has been separated into dozens of commands in the PowerShell module. I've spooled out all of the ones that seem to be related without finding this object.

mbeattie
2,905 Views

Hi Stan,

 

I recommend you don't go down the Invoke-SSH route (there's been a lot of issues with that method over time). You can now use the private REST API to invoke a CLI command in ONTAP via HTTPS. Here is an example of how to run that command:

 

#'------------------------------------------------------------------------------
#'Set the certificate policy and TLS version.
#'------------------------------------------------------------------------------
Add-Type @"
   using System.Net;
   using System.Security.Cryptography.X509Certificates;
   public class TrustAllCertsPolicy : ICertificatePolicy {
   public bool CheckValidationResult(
   ServicePoint srvPoint, X509Certificate certificate,
   WebRequest request, int certificateProblem) {
      return true;
   }
}
"@
[System.Net.ServicePointManager]::SecurityProtocol  = [System.Net.SecurityProtocolType]'Tls12'
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
#'------------------------------------------------------------------------------
Function Get-NcAuthorization{
   [Alias("Get-NcAuth")]
   [CmdletBinding()]
   Param(
      [Parameter(Mandatory = $True, HelpMessage = "The Credential to authenticate to the cluster")]
      [ValidateNotNullOrEmpty()]
      [System.Management.Automation.PSCredential]$Credential
   )
   #'---------------------------------------------------------------------------
   #'Set the authentication header to connect to the cluster.
   #'---------------------------------------------------------------------------
   $auth    = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Credential.UserName + ':' + $Credential.GetNetworkCredential().Password))
   $headers = @{
      "Authorization" = "Basic $auth"
      "Accept"        = "application/json"
      "Content-Type"  = "application/json"
   }
   Return $headers;
}#'End Function Get-NcAuthorization.
#'------------------------------------------------------------------------------
$cluster    = "192.168.100.2"
$credential = Get-Credential
$command    = "set advanced`;vserver cifs domain discovered-servers discovery-mode show"
$headers    = Get-NcAuth -Credential $Credential
$uri        = "https://$Cluster/api/private/cli/vserver/cifs/domain/discovered-servers/discovery-mode"
#'------------------------------------------------------------------------------
#'Invoke the command via the private REST CLI.
#'------------------------------------------------------------------------------
Write-Host "Executing Command`: $command"
Try{
   $response = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers -ErrorAction Stop
}Catch{
   Write-Warning -Message $("Failed Executing Command`: $command. Error " + $_.Exception.Message)
   Break;
}
#'------------------------------------------------------------------------------
#'Display the records.
#'------------------------------------------------------------------------------
If($Null -eq $response -Or $response.num_records -eq 0){
   Write-Warning -Message "No records were returned from executing command`: $command"
}Else{
   $response.records
}
#'------------------------------------------------------------------------------

 

Example output:

 

PS C:\Scripts\PowerShell\Projects\GetVserverDomainDiscovery> .\GetVserverDomainDiscoveryRestCli.ps1

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Executing Command: set advanced;vserver cifs domain discovered-servers discovery-mode show

vserver
-------
vserver1
vserver2
vserver3

Hope this helps.

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.
Public