ONTAP Discussions
ONTAP Discussions
Hi Guys, I need help with Powershell output i am getting.
I am able to get below output but i need below output to be arranged in format which i can put into CSV.
Is it possible?
PS D:\OCI\SOE_Compliance\System_Health> Invoke-ncssh -Command "system health subsystem show" | select Value | format-list
Keyboard-interactive authentication prompts from server:
End of keyboard-interactive prompts from server
Value : Subsystem Health
----------------- ------------------
SAS-connect ok
Environment ok
Memory ok
Service-Processor ok
Switch-Health ok
CIFS-NDO ok
Motherboard ok
IO ok
MetroCluster ok
MetroCluster_Node ok
FHM-Switch ok
FHM-Bridge ok
SAS-connect_Cluster ok
13 entries were displayed.
Solved! See The Solution
Hi Raj,
I would not recommend using "invoke-ncssh". Consider using "invoke-ncsystemapi" instead which enables you to run an SSH command via HTTPS. Here is an example to connect to a cluster, run the system health subsystem show command and format the CLI results using comma delimited output:
Source Code:
Param(
[Parameter(Mandatory = $True, HelpMessage = "The cluster to show the 'system health subsystem show' command for")]
[String]$Cluster,
[Parameter(Mandatory = $True, HelpMessage = "The credentials to connect to the cluster")]
[System.Management.Automation.PSCredential]$Credentials
)
#'------------------------------------------------------------------------------
Function Format-SystemHealthSubsystem{
Param(
[Parameter(Mandatory = $True, HelpMessage = "The CLI output of the 'system health subsystem show' command")]
[Array]$CLIOutput
)
[Array]$results = @();
[String]$header = $($CLIOutput[0] -Replace '(^\s+|\s+$)', '' -Replace '\s+', ' ').ToString().Trim()
[Array]$results += $header -replace ' ', ','
For($i=2; ($i -le $CLIOutput.Count - 2); $i++){
[String]$result = $($CLIOutput[$i] -Replace '(^\s+|\s+$)', '' -Replace '\s+', ' ').ToString().Trim()
[Array]$results += $result -replace ' ', ','
}
Return $results;
}#End Function
#'------------------------------------------------------------------------------
#'Import the NetApp DataONTAP PowerShell toolkit.
#'------------------------------------------------------------------------------
[String]$moduleName = "DataONTAP"
Try{
Import-Module -Name $moduleName -ErrorAction Stop
Write-Host "Imported module ""$moduleName"""
}Catch{
Write-Warning -Message $("Failed importing module ""$moduleName"". Error " + $_.Exception.Message)
Break;
}
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
Connect-NcController -Name $cluster -HTTPS -Credential $Credentials -ErrorAction Stop | Out-Null
Write-Host $("Connected to cluster ""$Cluster"" as user " + $Credentials.GetNetworkCredential().UserName)
}Catch{
Write-Warning -Message $("Failed connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
Break;
}
#'------------------------------------------------------------------------------
#'Invoke the SSH command via HTTPS.
#'------------------------------------------------------------------------------
Try{
$command = @("system", "health", "subsystem", "show")
$api = $("<system-cli><args><arg>" + ($command -join "</arg><arg>") + "</arg></args></system-cli>")
$output = Invoke-NcSystemApi -Request $api -ErrorAction Stop
Write-Host $("Executed Command`: " + $([String]::Join(" ", $command)))
}Catch{
Write-Warning -Message $("Failed Executing Command`: $command. Error " + $_.Exception.Message)
Break;
}
If($output.results."cli-result-value" -eq 1){
[Array]$CLIOutput = $output.results."cli-output".Trim().Split("`n")
Format-SystemHealthSubsystem -CLIOutput $CLIOutput
}Else{
Write-Warning -Message $("Failed Executing Command`: " + $([String]::Join(" ", $command)))
Break;
}
#'------------------------------------------------------------------------------
Output:
PS C:\Scripts\PowerShell\Projects\SystemHealthSubSystemShow> $credentials = Get-Credential -Credential admin
PS C:\Scripts\PowerShell\Projects\SystemHealthSubSystemShow> .\SystemHealthSubSystemShow.ps1 -Cluster cluster1.testlab.local -Credentials $credentials
Imported module "DataONTAP"
Connected to cluster "cluster1.testlab.local" as user admin
Executed Command: system health subsystem show
Subsystem,Health
Switch-Health,ok
CIFS-NDO,ok
MetroCluster,ok
MetroCluster_Node,ok
FHM-Switch,ok
FHM-Bridge,ok
Hope that helps
/Matt
Hi Raj,
I would not recommend using "invoke-ncssh". Consider using "invoke-ncsystemapi" instead which enables you to run an SSH command via HTTPS. Here is an example to connect to a cluster, run the system health subsystem show command and format the CLI results using comma delimited output:
Source Code:
Param(
[Parameter(Mandatory = $True, HelpMessage = "The cluster to show the 'system health subsystem show' command for")]
[String]$Cluster,
[Parameter(Mandatory = $True, HelpMessage = "The credentials to connect to the cluster")]
[System.Management.Automation.PSCredential]$Credentials
)
#'------------------------------------------------------------------------------
Function Format-SystemHealthSubsystem{
Param(
[Parameter(Mandatory = $True, HelpMessage = "The CLI output of the 'system health subsystem show' command")]
[Array]$CLIOutput
)
[Array]$results = @();
[String]$header = $($CLIOutput[0] -Replace '(^\s+|\s+$)', '' -Replace '\s+', ' ').ToString().Trim()
[Array]$results += $header -replace ' ', ','
For($i=2; ($i -le $CLIOutput.Count - 2); $i++){
[String]$result = $($CLIOutput[$i] -Replace '(^\s+|\s+$)', '' -Replace '\s+', ' ').ToString().Trim()
[Array]$results += $result -replace ' ', ','
}
Return $results;
}#End Function
#'------------------------------------------------------------------------------
#'Import the NetApp DataONTAP PowerShell toolkit.
#'------------------------------------------------------------------------------
[String]$moduleName = "DataONTAP"
Try{
Import-Module -Name $moduleName -ErrorAction Stop
Write-Host "Imported module ""$moduleName"""
}Catch{
Write-Warning -Message $("Failed importing module ""$moduleName"". Error " + $_.Exception.Message)
Break;
}
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
Connect-NcController -Name $cluster -HTTPS -Credential $Credentials -ErrorAction Stop | Out-Null
Write-Host $("Connected to cluster ""$Cluster"" as user " + $Credentials.GetNetworkCredential().UserName)
}Catch{
Write-Warning -Message $("Failed connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
Break;
}
#'------------------------------------------------------------------------------
#'Invoke the SSH command via HTTPS.
#'------------------------------------------------------------------------------
Try{
$command = @("system", "health", "subsystem", "show")
$api = $("<system-cli><args><arg>" + ($command -join "</arg><arg>") + "</arg></args></system-cli>")
$output = Invoke-NcSystemApi -Request $api -ErrorAction Stop
Write-Host $("Executed Command`: " + $([String]::Join(" ", $command)))
}Catch{
Write-Warning -Message $("Failed Executing Command`: $command. Error " + $_.Exception.Message)
Break;
}
If($output.results."cli-result-value" -eq 1){
[Array]$CLIOutput = $output.results."cli-output".Trim().Split("`n")
Format-SystemHealthSubsystem -CLIOutput $CLIOutput
}Else{
Write-Warning -Message $("Failed Executing Command`: " + $([String]::Join(" ", $command)))
Break;
}
#'------------------------------------------------------------------------------
Output:
PS C:\Scripts\PowerShell\Projects\SystemHealthSubSystemShow> $credentials = Get-Credential -Credential admin
PS C:\Scripts\PowerShell\Projects\SystemHealthSubSystemShow> .\SystemHealthSubSystemShow.ps1 -Cluster cluster1.testlab.local -Credentials $credentials
Imported module "DataONTAP"
Connected to cluster "cluster1.testlab.local" as user admin
Executed Command: system health subsystem show
Subsystem,Health
Switch-Health,ok
CIFS-NDO,ok
MetroCluster,ok
MetroCluster_Node,ok
FHM-Switch,ok
FHM-Bridge,ok
Hope that helps
/Matt
Hey matt
I hope you see my comment, because the post is already 2 years old.
When I run the identical script from you (we have a Netapp Metrocluster) the variable "$output.results" has the "status: Passed" but the variable "cli-result-value" is empty.
Did you have this problem before?
Kind Regards
WhiteBakedBanana
Hi,
I've not seen that before. Does it work if you change the command? Does it give you any output if you change the command to:
$command = @("version")
That should display the ONTAP system version in the value of "$output.results" EG:
PS C:\Scripts\PowerShell\Projects\InvokeCommand> .\InvokeCommand.ps1 -Cluster cluster1.testlab.local -Credentials $credential
Imported module "DataONTAP"
Connected to cluster "cluster1.testlab.local" as user admin
Executed Command: version
NetApp,Release,9.11.1:,Tue,Jul,12,04:38:28,UTC,2022
/Matt
Hello Matt
thank you very much. But also unfortunately with the command:
$output.results.status = passed
$output.results.cli-output = empty
$output.results.'cli-result-value' = 0
I connect with my admin user. But are there special rights that I may need?
Kind Regards
WhiteBakedBanana
Hi,
When you say "admin" user, are you referring to the builtin admin user or another administrative account. What's the output of the following command for the user that you are using?
cluster1::> login show -user-or-group-name admin
(security login show)
Vserver: cluster1
Second
User/Group Authentication Acct Authentication
Name Application Method Role Name Locked Method
-------------- ----------- ------------- ---------------- ------ --------------
admin amqp password admin no none
admin console password admin no none
admin http password admin no none
admin ontapi password admin no none
admin service-processor
password admin no none
admin ssh password admin no none
6 entries were displayed.
/Matt