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 
|