0 Replies Latest reply on May 29, 2014 7:10 AM by leinhard

    Powershell code for checking the Nexus NXOS

    leinhard

      Dear, Team,

      this is code for the checing NXOS version written by me.

      anyone who can test it is running ok or not

       

       

      ============CODE START===========

      # Nexus 3048 OS Version check v1.0
      # Last Update: 2014.05.29
      # Email : david.shin@hostingglobal.co.kr
      # Author : David, Shin.


      if (! (Get-PSSnapin | where {$_.Name -eq "SwisSnapin"})) {
          Add-PSSnapin "SwisSnapin"
      }


      function CustomInputBox([string] $title, [string] $message, [string] $defaultText)
      {
      [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
      [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
      $userForm = New-Object System.Windows.Forms.Form
      $userForm.Text = "$title"
      $userForm.Size = New-Object System.Drawing.Size(290,150)
      $userForm.StartPosition = "CenterScreen"
      $userForm.AutoSize = $False
      $userForm.MinimizeBox = $False
      $userForm.MaximizeBox = $False
      $userForm.SizeGripStyle= "Hide"
      $userForm.WindowState = "Normal"
      $userForm.FormBorderStyle="Fixed3D"
         
      $OKButton = New-Object System.Windows.Forms.Button
      $OKButton.Location = New-Object System.Drawing.Size(115,80)
      $OKButton.Size = New-Object System.Drawing.Size(75,23)
      $OKButton.Text = "OK"
      $OKButton.Add_Click({$value=$objTextBox.Text;$userForm.Close()})
      $userForm.Controls.Add($OKButton)
      $CancelButton = New-Object System.Windows.Forms.Button
      $CancelButton.Location = New-Object System.Drawing.Size(195,80)
      $CancelButton.Size = New-Object System.Drawing.Size(75,23)
      $CancelButton.Text = "Cancel"
      $CancelButton.Add_Click({$userForm.Close()})
      $userForm.Controls.Add($CancelButton)
      $userLabel = New-Object System.Windows.Forms.Label
      $userLabel.Location = New-Object System.Drawing.Size(10,20)
      $userLabel.Size = New-Object System.Drawing.Size(280,20)
      $userLabel.Text = "$message"
      $userForm.Controls.Add($userLabel)
      $objTextBox = New-Object System.Windows.Forms.TextBox
      $objTextBox.Location = New-Object System.Drawing.Size(10,40)
      $objTextBox.Size = New-Object System.Drawing.Size(260,20)
      $objTextBox.Text="$defaultText"
      $userForm.Controls.Add($objTextBox)
      $userForm.Topmost = $True
      $userForm.Opacity = 1
           $userForm.ShowIcon = $False
      $userForm.Add_Shown({$userForm.Activate()})
      [void] $userForm.ShowDialog()
      return $value

      }


      $hostname = "127.0.0.1"
      $username = "admin"
      $password = New-Object System.Security.SecureString
      $cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
      $swis = Connect-Swis -v2 -host $hostname -cred $cred

      #write-host "Device OS Version Check"
      #$group=read-host "INPUT NODE GROUP"
      #$osver=read-host "OS VERSION"

      $group = CustomInputBox "NXOS OS VERSION CHECK" "INPUT NODE GROUP" "w3_z3_oob"
      if ( $group -ne $null )
      {
      }
      else
      {
      "Cancelled!!"
      break
      }

      $osver = CustomInputBox "NXOS OS VERSION CHECK" "INPUT NXOS VERSION" "6.0(2)U2(2)"
      if ( $osver -ne $null )
      {
      }
      else
      {
      "Cancelled!!"
      break
      }

      $script="sh ver"

      $date=Get-Date -format "yyMMddhhmm"
      $File = "C:\NBP\RESULT\$date-$script-OS_CHECK-$group.txt"

      If ( Test-Path $File)
      {
         Remove-Item $File
      }


      $nodeId = get-swisdata $swis "SELECT NodeID FROM Cirrus.Nodes WHERE NodeGroup='$group'"

      #$ip=read-host "INPUT IP"
      #$nodeId = get-swisdata $swis "SELECT NodeID FROM Cirrus.Nodes WHERE AgentIP='$ip'"

      $count = 0
      foreach ($node in $nodeId) {

      #write-host $node
      $Caption = get-swisdata $swis "SELECT NodeCaption FROM Cirrus.Nodes WHERE NodeId='$node'"

      $nodeIdList = New-Object -TypeName "System.Guid[]" 1
      $nodeIdList[0] = $node


      Invoke-SwisVerb $swis Cirrus.ConfigArchive Execute @($nodeIdList, $script, $username) | Out-Null
      $transferID = "{$node}:${username}:ExecuteScript"

      do {
           Start-Sleep -Seconds 3
           $status = Get-SwisData $swis "SELECT T.Status, T.Error FROM Cirrus.TransferQueue T WHERE T.TransferID=@transfer" @{transfer=$transferID}
           Write-Host $status.Status
           #write-host $Caption
      }
      while (($status.Status -ne 'Complete') -and (-not $status.Error))

      $output = Get-SwisData $swis "SELECT T.Log FROM Cirrus.TransferQueue T WHERE T.TransferID=@transfer" @{transfer=$transferID}
      #$output = $output.TrimStart($script)

              $output = $output -split ("`r`n")

              foreach ($line in $output) {
                  switch -wildcard ($line)
                  {
                      "*kickstart: version*" {$A = $line.trim()}
           }

           switch -wildcard ($line)
                  { 
                      "*system:    version*" {$B = $line.trim()}
           }
               } 

      %{$datasystem = $B.split(" ") | where {$_ -ne ""};}  
      %{$datakick = $A.split(" ") | where {$_ -ne ""};}  

      $kickok = 0
      $sysok = 0
      $kickfail = 0
      $sysfail = 0
      $num = $count + 1

      If ($($datakick[2]) -eq $osver)
      {
         $output = "$num $Caption $($datakick[2]) KCKSTART OS VER OK"
        write-host $output
        $output | Out-File $File -append
        $kickok = $kickok + 1
        }
       
      Else
        {
        $output = "$num $Caption $($datakick[2]) KICKSTART OS VER Different"
        write-host $output
            $output | Out-File $File -append
        $kickfail = $kickfail + 1
      }

      If ($($datasystem[2]) -eq $osver)
      {

         $output = "$num $Caption $($datasystem[2]) SYSTEM   OS VER OK"
        write-host $output
        $output | Out-File $File -append
        $sysok = $sysok + 1
      }
      Else
      {
        $output = "$num $Caption $($datasystem[2]) SYSTEM    OS VER Different"
        write-host $output
            $output | Out-File $File -append
        $sysfail = $sysfail + 1
      }

      $count = $count + 1
      }

      " " | Out-File $File -append
      " " | Out-File $File -append
      "KICKSTART VERSION OK   TOTAL = $kickok " | Out-File $File -append
      "KICKSTART VERSION FAIL TOTAL = $kickfail " | Out-File $File -append
      "SYSTEM    VERSION OK   TOTAL = $sysok " | Out-File $File -append
      "SYSTEM    VERSION FAIL TOTAL = $sysfail " | Out-File $File -append
      " " | Out-File $File -append
      " " | Out-File $File -append
      "Total : $count Devices Check Completed" | Out-File $File -append
      Write-Host " "
      Write-Host " "
      Write-Host "Checking Total $count Devices Completed"