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

    Powershell script for checking current power status for nexus

    leinhard

      Dear Team

      this is Power On check for Nexus  (sh env power)

       

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

      # Nexus 3048 Power 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 Power Status Check"
      #$group=read-host "INPUT NODE GROUP"

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

      $script="sh env"

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

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

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

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

      $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")

              $lines = @()
      foreach ($line in $output) {
      switch -wildcard ($line)
      {
        "*1   N2200-PAC-400W*" {$A = $line.Trim()}
      }
      switch -wildcard ($line)
      {
        "*2   N2200-PAC-400W*" {$B = $line.Trim()}
      }
      }   

      if ($A)
      {
        %{$power1 = $A.split(" ") | where {$_ -ne ""};}
      }
      else
      {
        %{$power1 = ("","","","","","Power Not exists") }
      }


      if ($B)
      {
        %{$power2 = $B.split(" ") | where {$_ -ne ""};}  
      }
      else
      {
        %{$power2 = ("","","","","","Power Not exists") }
      }


      $power1ok = 0
      $power2ok = 0
      $power1fail = 0
      $power2fail = 0
      $num = $count + 1

      If ($($power1[5]) -eq "ok")
      {
         $output = "$num $Caption POWER $($power1[0])  $($power1[5])"
        write-host $output
        $output | Out-File $File -append
        $power1ok = $power1ok + 1
        }
       
      Else
        {
        $output = "$num $Caption POWER $($power1[0])  $($power1[5])"
        write-host $output
            $output | Out-File $File -append
        $power1fail = $power1fail + 1
      }

      If ($($power2[5]) -eq "ok")
      {

         $output = "$num $Caption POWER $($power2[0])  $($power2[5])"
        write-host $output
        $output | Out-File $File -append
        $power2ok = $power2ok + 1
      }
      Else
      {
        $output = "$num $Caption POWER $($power2[0])  $($power2[5])"
        write-host $output
            $output | Out-File $File -append
        $power2fail = $power2fail + 1
      }

      $count = $count + 1
      }


      " " | Out-File $File -append
      " " | Out-File $File -append
      "$Caption POWER1 OK = $power1ok" | Out-File $File -append
      "$Caption POWER1 FAIL = $power2fail" | Out-File $File -append
      "$Caption POWER2 OK = $power2ok" | Out-File $File -append
      "$Caption POWER2 FAIL = $power2fail" | 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"