(-: Fibergeek's Blog :-)
Inhoud blog
  • Python 2 of 3
  • IDA-python: GetStructureIdAt
  • Geslaagd in Microsoft's 70-680 examen
  • minibio: kort vervolg
  • miniboa: een voorbeeld
    Zoeken in blog

    Categorieën
  • Code: C/C++ (2)
  • Code: Powershell (1)
  • Code: Python (8)
  • Code: WPF (2)
  • Programmeren (5)
  • 18-08-2011
    Klik hier om een link te hebben waarmee u dit artikel later terug kunt lezen.iSCSI via de Powershell

    De onderstaande PowerShell-scripts heb ik geschreven als aanvulling op de beperkte Windows Server 2008 backup. In een latere blog zal ik het exacte hoe-en-waarom uitleggen.

    Veel uitleg over de scripts is niet nodig denk ik, maar weet dat het verre van een complete PowerShell-interface naar de Microsoft iSCSI-Initiator toe is. De volgende functies kan je gebruiken, de namen spreken voor zichzelf, op voorwarde dat je Engels kan:
    Get-All-iSCSI-Targets
    Get-All-iSCSI-Sessions
    Get-All-iSCSI-Sessions-And-Names
    Print-All-iSCSI-Targets
    Print-All-iSCSI-Sessions
    Print-All-iSCSI-Sessions-And-Names
    Mount-iSCSI-Target
    Close-iSCSI-Session
    Close-iSCSI-Sessions-With-Matching-Target
    Close-All-iSCSI-Sessions


    Ik ben geen PowerShell-fan en ik 'vrees' dat ik het nooit zal worden. Sommige functies zijn zo cryptisch dat het lijkt alsof de code altijd obfuscated is. Ik heb de Clean Code-principes zo goed als mogelijk toegepast. Als je deze scripts handig vind, laat dan aub een berichtje achter. Bedankt.


    #
    # Support functions related with iSCSI
    #

    Function GetRawISCSISessionAndNameStrings()
    {
      iscsicli ReportTargetMappings | findstr "Session Name"
    }

    Function GetRawISCSISessionStrings()
    {
      iscsicli ReportTargetMappings | findstr /C:"Session Id"
    }

    Function GetRawISCSITargetStrings()
    {
      iscsicli ListTargets | findstr /C:"    " | ForEach-Object { $_.Trim() }
    }

     

    #
    # All the cool functions related with iSCSI
    #

    Function Get-All-iSCSI-Sessions-And-Names()
    {
      $ret = @()
      $SessionsAndNames = GetRawISCSISessionAndNameStrings
      if($SessionsAndNames)
      {
        for($i = 0; $i -lt $SessionsAndNames.Length; $i += 2)
        {
          $session = $SessionsAndNames[$i+0].split(":")
          $name    = $SessionsAndNames[$i+1].split(":")
          if($session[0].trim() -eq "Session Id")
          {
            if($name[0].trim() -eq "Target Name")
            {
              $session = $session[1].trim()
              $name    = $name[1].trim()
              $ret += ,($session,$name)
            }
            else
            {
              throw (new-object Exception("Target Name-field was not found!"))
            }
          }
          else
          {
            throw (new-object Exception("Session Id-field was not found!"))
          }
        }
      }
      ,$ret
    }

    Function Get-All-iSCSI-Targets()
    {
      GetRawISCSITargetStrings | ForEach-Object { $_.Trim() }
    }

    Function Get-All-iSCSI-Sessions()
    {
      $sessions = GetRawISCSISessionStrings
      if($sessions)
      {
        $sessions = $sessions | ForEach-Object{ $_.Split(":")[1].Trim() }
      }
      $sessions
    }

    Function Print-All-iSCSI-Targets()
    {
      $targets = Get-All-iSCSI-Targets
      if($targets)
      {
        foreach($target in $targets)
        {
          Write-Host $target
        }
      }
    }

    Function Print-All-iSCSI-Sessions()
    {
      $sessions = Get-All-iSCSI-Sessions
      if($sessions)
      {
        foreach($session in $sessions)
        {
          Write-Host $session
        }
      }
    }

    Function Print-All-iSCSI-Sessions-And-Names()
    {
      $sessions = Get-All-iSCSI-Sessions-And-Names
      if($sessions)
      {
        foreach($pair in $sessions)
        {
          $session = $pair[0]
          $name    = $pair[1]
          Write-Host $session "->" $name
        }
      }
    }

    Function Mount-iSCSI-Target($TargetToMount)
    {
      iscsicli LoginTarget $TargetToMount T * * * * * * * * * * * * * * * 0
    }

    Function Close-iSCSI-Session($session)
    {
      iscsicli LogoutTarget $Session
    }

    Function Close-iSCSI-Sessions-With-Matching-Target($mustContain)
    {
      $sessions = Get-All-iSCSI-Sessions-And-Names
      if($sessions)
      {
        foreach($pair in $sessions)
        {
          $session = $pair[0]
          $name    = $pair[1]
          if($name.contains($iscsiTargetPrefix))
          {
            Close-iSCSI-Session $session
          }
        }
      }
    }

    Function Close-All-iSCSI-Sessions()
    {
      $sessions = Get-All-iSCSI-Sessions
      if($sessions)
      {
        foreach($session in $Sessions)
        {
          "Closing session " + $session + "..."
          Close-iSCSI-Session $session
        }
      }
    }

    18-08-2011, 00:00 Geschreven door Fibergeek  


    Categorie:Code: Powershell
    Tags:PowerShell,Windows Server Backup,iSCSI,iscsicli
    Archief per week
  • 25/11-01/12 2013
  • 05/11-11/11 2012
  • 07/05-13/05 2012
  • 05/03-11/03 2012
  • 20/02-26/02 2012
  • 13/02-19/02 2012
  • 30/01-05/02 2012
  • 12/12-18/12 2011
  • 05/12-11/12 2011
  • 19/09-25/09 2011
  • 15/08-21/08 2011
  • 01/08-07/08 2011
  • 04/07-10/07 2011
  • 06/06-12/06 2011

    E-mail mij

    Druk op onderstaande knop om mij te e-mailen.


    Gastenboek

    Druk op onderstaande knop om een berichtje achter te laten in mijn gastenboek


    Blog als favoriet !


    Blog tegen de wet? Klik hier.
    Gratis blog op https://www.bloggen.be - Meer blogs