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

    Powershell Code for Checking NXOS Image Size

    leinhard

      Dear, Team

      this is code for the kickstart and system image size check using "dir" command

       

      ===========CODE START HERE============

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

       

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

      $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


      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

      }

      function button ($title, $kick, $kick_size, $system, $system_size) {

      ###################Load Assembly for creating form & button######

      [void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
      [void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)

      #####Define the form size & placement

      $form = New-Object “System.Windows.Forms.Form”;
      $form.Width = 600;
      $form.Height = 150;
      $form.Text = $title;
      $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
      $form.AutoSize = $False
      $form.MinimizeBox = $False
      $form.MaximizeBox = $False
      $form.SizeGripStyle= "Hide"
      $form.WindowState = "Normal"
      $form.FormBorderStyle="Fixed3D"

      ##############Define text label1
      $textLabel1 = New-Object “System.Windows.Forms.Label”;
      $textLabel1.Left = 25;
      $textLabel1.Top = 10;
      $textLabel1.width = 150;
      $textLabel1.Text = $kick;

      ##############Define text label2

      $textLabel2 = New-Object “System.Windows.Forms.Label”;
      $textLabel2.Left = 25;
      $textLabel2.Top = 35;
      $textLabel2.Text = $kick_size;

      ##############Define text label3

      $textLabel3 = New-Object “System.Windows.Forms.Label”;
      $textLabel3.Left = 25;
      $textLabel3.Top = 60;
      $textLabel3.Text = $system;

      ##############Define text label4

      $textLabel4 = New-Object “System.Windows.Forms.Label”;
      $textLabel4.Left = 25;
      $textLabel4.Top = 85;
      $textLabel4.Text = $system_size;

      ############Define text box1 for input
      $textBox1 = New-Object “System.Windows.Forms.TextBox”;
      $textBox1.Left = 200;
      $textBox1.Top = 10;
      $textBox1.width = 200;

      ############Define text box2 for input

      $textBox2 = New-Object “System.Windows.Forms.TextBox”;
      $textBox2.Left = 200;
      $textBox2.Top = 35;
      $textBox2.width = 200;

      ############Define text box3 for input

      $textBox3 = New-Object “System.Windows.Forms.TextBox”;
      $textBox3.Left = 200;
      $textBox3.Top = 60;
      $textBox3.width = 200;

      ############Define text box4 for input

      $textBox4 = New-Object “System.Windows.Forms.TextBox”;
      $textBox4.Left = 200;
      $textBox4.Top = 85;
      $textBox4.width = 200;

      #############Define default values for the input boxes
      # $defaultValue = “”
      # $textBox1.Text = $defaultValue;
      # $textBox2.Text = $defaultValue;
      # $textBox3.Text = $defaultValue;
      # $textBox4.Text = $defaultValue;

      $textBox1.Text = "n3000-uk9-kickstart.6.0.2.U3.1.bin";
      $textBox2.Text = "32491008";
      $textBox3.Text = "n3000-uk9.6.0.2.U3.1.bin";
      $textBox4.Text = "215973850";

      #############define OK button
      $button1 = New-Object “System.Windows.Forms.Button”;
      $button1.Left = 410;
      $button1.Top = 10;
      $button1.Width = 100;
      $button1.Text = “Ok”;

      #############define OK button
      $button2 = New-Object “System.Windows.Forms.Button”;
      $button2.Left = 410;
      $button2.Top = 35;
      $button2.Width = 100;
      $button2.Text = “CANCEL”;

      ############# This is when you have to close the form after getting values
      $eventHandler1 = [System.EventHandler]{
      $textBox1.Text;
      $textBox2.Text;
      $textBox3.Text;
      $textBox4.Text;
      $form.Close();
      };

      $eventHandler2 = [System.EventHandler]{
      $form.Close();
      };

      $button1.Add_Click($eventHandler1) ;
      $button2.Add_Click($eventHandler2) ;

      #############Add controls to all the above objects defined
      $form.Controls.Add($button1);
      $form.Controls.Add($button2);
      $form.Controls.Add($textLabel1);
      $form.Controls.Add($textLabel2);
      $form.Controls.Add($textLabel3);
      $form.Controls.Add($textLabel4);
      $form.Controls.Add($textBox1);
      $form.Controls.Add($textBox2);
      $form.Controls.Add($textBox3);
      $form.Controls.Add($textBox4);
      $ret = $form.ShowDialog();

      #################return values


      return $textBox1.Text, $textBox2.Text, $textBox3.Text, $textBox4.Text
      }


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

      $return= button "INPUT EACH IMAGE SIZE" "KICKSTART IMAGE" "KICKSTART SIZE" "SYSTEM IMAGE" "SYSTEM SIZE"

      if ( $return -ne $null )
      {
      "KICKSTART IMAGE " + $return[0]
      "KICKSTART SIZE  " + $return[1]
      "SYSTEM    IMAGE " + $return[2]
      "SYSTEM    SIZE  " + $return[3]
      }
      else
      {
      "Cancelled!!"
      break
      }


      $script="dir"

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

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


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


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


      $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) {
            if ($line -match $return[0]) {$A = $line.trim()} 
             if ($line -match $return[2]) {$B = $line.trim()} 
              }   

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

      if ($A)
      {
        %{$datakick = $A.split(" ") | where {$_ -ne ""};}
        If ($($datakick[0]) -eq $return[1])
        {
        $output = "$num $Caption KISKSTART File is OK"
        write-host $output
        $output | Out-File $File -append
        $kickok = $kickok + 1
        }
        Else
        {
        $output = "$num $Caption KICKSTART Size Different"
        write-host $output
            $output | Out-File $File -append
        $kickfail = $kickfail + 1
        }
      }
      else
      {
        $output = "$num $Caption KICKSTART File Not Exist."
        write-host $output
            $output | Out-File $File -append

      }


      if ($B)
      {
        %{$datasystem = $B.split(" ") | where {$_ -ne ""};}  
        If ($($datasystem[0]) -eq $return[3])
        {
        $output = "$num $Caption SYSTEM File is OK"
        write-host $output
        $output | Out-File $File -append
        $sysok = $sysok + 1
        }
        Else
        {
        $output = "$num $Caption SYSTEM Size Different"
        write-host $output
            $output | Out-File $File -append
        $sysfail = $sysfail + 1
        }
      }
      else
      {
        $output = "$num $Caption SYSTEM File Not Exist."
        write-host $output
            $output | Out-File $File -append
      }


      $count = $count + 1
      }


      " " | Out-File $File -append
      " " | Out-File $File -append
      "KICKSTART IMAGE OK   TOTAL = $kickok " | Out-File $File -append
      "KICKSTART IMAGE FAIL TOTAL = $kickfail " | Out-File $File -append
      "SYSTEM    IMAGE OK   TOTAL = $sysok " | Out-File $File -append
      "SYSTEM    IMAGE 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"