ONTAP Discussions

powershell script

Ashun
586 Views

I am recently learning powershell scripting, how can I write scripts to successfully access adv permissions,

I tried to pass y after set adv, but it didn't work, I don't know how to solve this problem, hope someone can help modify my script

 

 

my script

# Set connection information
$ONTAPHost = Read-Host "Enter the IP address of the ONTAP cluster"
$User = Read-Host "Enter username"
$Password = Read-Host "Enter password"
$OutputFile = "commands_output.txt" # Output file path


# Import Posh-SSH module from removable drive, need to change the drive letter
Import-Module F:\Posh-SSH

# Create an SSH session and pass credentials
$securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($User, $securePassword)

# Define command array
$commands = @(
"cluster show",
"set adv",
"system node image show"
)

# Try to connect to the ONTAP cluster
try {
# Creating an SSH Session
$session = New-SSHSession -ComputerName $ONTAPHost -Credential $credential -ErrorAction Stop
Write-Host "SSH session established successfully."

# If the file already exists, clear the file contents
if (Test-Path $OutputFile) {
Clear-Content -Path $OutputFile
}

# Loop through the array of commands and execute them in turn
foreach ($command in $commands) {
Write-Host "`nExecuting command: $command"

try {
# Execute the current command and get the result
$result = Invoke-SSHCommand -SessionId $session.SessionId -Command $command -ErrorAction Stop

# Record the command execution to a file
Add-Content -Path $OutputFile -Value "`n::> $command"

# Output the result of the command
if ($result.Output) {
$result.Output -split "`n" | ForEach-Object {
Add-Content -Path $OutputFile -Value $_
}
} else {
Add-Content -Path $OutputFile -Value "No output received for '$command'."
}
} catch {
Write-Host "Error executing command: $command"
Add-Content -Path $OutputFile -Value "Error executing command: $command - $_"
}
}
} catch {
Write-Host "Failed to establish SSH session or execute command: $_"
Add-Content -Path $OutputFile -Value "Failed to establish SSH session or execute command: $_"
exit
} finally {
# Make sure to close the SSH session at the end of the script
if ($session) {
Remove-SSHSession -SessionId $session.SessionId
Write-Host "SSH session closed."
}
}

 

outputfile


::> cluster show


Last login time: 11/25/2024 16:21:20

Node Health Eligibility
--------------------- ------- ------------
FAS2720-01 true true
FAS2720-02 true true
2 entries were displayed.


::> set adv


Last login time: 11/25/2024 16:22:14

 

::> system node image show


Last login time: 11/25/2024 16:22:14


Error: "image" is not a recognized command

 

1 ACCEPTED SOLUTION

Ashun
464 Views

 

I'm really laughing at myself for thinking so much, I just need to use the parameter -confirmations off after set adv.......... T_T

 

Turn off confirmation

"set adv -c off"

View solution in original post

7 REPLIES 7

Sanaman
511 Views

$commands = "cluster show;set adv;system node image show;set adm"


Invoke-Ncssh -Command $commands

 

NcController : cluster01
Value :
Last login time: 11/26/2024 01:49:19
Node Health Eligibility
--------------------- ------- ------------
node01a true true
node01b true true
2 entries were displayed.


                           Is             Is                             Install
Node Image Default Current Version Date
-------- ------- ------- ------- ------------------------- -------------------
node01a
image1 false false 9.13.1P6 3/25/2024 11:52:38
image2 true true 9.13.1P7 6/7/2024 11:27:28
node01b
image1 false false 9.13.1P6 3/25/2024 11:53:24
image2 true true 9.13.1P7 6/7/2024 11:28:18
4 entries were displayed.

 

 

( my version of toolkit is 9.15.x so not Invoke-SSHCommand)

Ashun
498 Views

hi sanaman

 

Yes, first of all, thank you for your reply. PSTK can indeed get the result I want, so is it possible to Invoke-SSHCommand ?

Ashun
465 Views

 

I'm really laughing at myself for thinking so much, I just need to use the parameter -confirmations off after set adv.......... T_T

 

Turn off confirmation

"set adv -c off"

manistorage
305 Views

can you share the completed/fixed scripts. am also looking for health check scripts for bunch of clusters.

Ashun
266 Views

You can put your commands into the command array, most likely it will run (I'm not sure about your environment), the script is fine because I was unable to execute the system node image show, other commands will run. This script is only used for log collection and does not have the function of health check. I need to check logs to determine whether there is a problem. I think the script may not be able to accurately identify the problem, and I'm sorry that it may not be able to help you.......

manistorage
256 Views

I receive the below error - Enter password: ***********
Failed to establish SSH session or execute command: The term 'New-SSHSession' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.

Ashun
227 Views

Whether you have downloaded and installed the Posh-SSH module,

Run the following command to install the Posh-SSH module from PowerShell Gallery:
Install-Module -Name Posh-SSH -Force -AllowClobber
-Force: forcibly installs the module, even if the module is already installed.
-AllowClobber: allows existing commands to be overwritten.

Re-import the module Modify the following information to try again
Import-Module F:\Posh-SSH ---> Import-Module Posh-SSH

Public