Microsoft Virtualization Discussions

loop

JSHACHER11
8,341 Views

Following up on my last post, I have this script that I need to loop

++++++++++++++++++++++++++++++++++++++

Import-Module Dataontap

write-host ""

write-host ""

write-host "This script will list volumes over 80% full, the containing aggregates and snapmirror relationships" -foregroundcolor yellow

write-host ""

write-host ""

write-host "You will also get an option to increase a volume size" -foregroundcolor yellow

write-host ""

write-host ""

$filer = Read-Host "Enter the filer name"

$user = Read-Host "Enter username"

Connect-NaController -Name $filer -Credential $user

get-navol | ? {$_.used -gt 80} | select name,used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.Available/1gb)}},@{Name="Total Size (in GB)";Expression={[math]::truncate($_.sizetotal/1gb)}} | ft -Autosize | Out-String

write-host ""

write-host ""

write-host "Here are the aggregates:" -foregroundcolor yellow

write-host ""

write-host ""

get-naaggr | select name,@{Name="Total Size (in GB)";Expression={[math]::truncate($_.totalsize/1gb)}},used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.available/1gb)}} | ft -Autosize | Out-String

write-host ""

write-host ""

write-host "and the snapmirror relationships:" -foregroundcolor yellow

write-host ""

write-host ""

get-navol | ? {$_.used -gt 80} | get-nasnapmirror | select source,destination | ft -Autosize | Out-String

write-host ""

write-host ""

$ans = Read-Host -prompt "Would you like to increase one of the volumes? [Y/N]"

write-host ""

write-host ""

if ($ans -eq "N") {

exit

} else {

$vol = Read-Host "Enter volume name"

$size = Read-Host "How much more space do you want to give it? (example: +100g)"

Set-NaVolsize -Name $vol -NewSize $size -Confirm

Write-Host ""

Write-Host ""

Write-Host "Check the new size:" -foregroundcolor yellow

Write-Host ""

Write-Host ""

Get-NaVol -Name $vol | select name,used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.Available/1gb)}},@{Name="Total Size (in GB)";Expression={[math]::truncate($_.sizetotal/1gb)}}

}

+++++++++++++++++++++++++++++++++++++++++++

The above gives me the option to increase a volume size but will only run once. I'm after a loop that will give me an option to choose to run the Set-NaVolsize again.

It should be something like this at the end of the script:

$ans = Read-Host -prompt "Would you like to increase another volumes? [Y/N]"

if ($ans -eq "N") {

exit

} else {

$vol = Read-Host "Enter volume name"

$size = Read-Host "How much more space do you want to give it? (example: +100g)"

Set-NaVolsize -Name $vol -NewSize $size -Confirm

Write-Host ""

Write-Host ""

Write-Host "Check the new size:" -foregroundcolor yellow

Write-Host ""

Write-Host ""

Get-NaVol -Name $vol | select name,used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.Available/1gb)}},@{Name="Total Size (in GB)";Expression={[math]::truncate($_.sizetotal/1gb)}}

}

so the above should loop until the input is N (no) - how do I do that?

Thank you

1 ACCEPTED SOLUTION

JGPSHNTAP
8,339 Views

Ok, so let me address a few issues here

First, you shouldn't be importing the module every time you run the script.  You should add it to your profile.  And someone mentioned to me that the module will load automatically in 3.0, but i haven't tested that.  Look up Powershell profiles. 

First, you should eliminate all the Write-host ""

You should use something like

Write-Host "`n`nCheck the new size:`n`n" -foregroundcolor yellow

Also, as stated in previous email, you should be using the convertto-formattednumber cmdlet in place of - Expression={[math]::truncate($_.Available/1gb)}},    It's a custom Dataontap module cmdlet that will help with other future scripts

As for your loop. you need to focus on something like Do.... Until.

something like

Do {

$ans = Read-Host -prompt "Would you like to increase one of the volumes? [Y/N]"

write-host ""

write-host ""

if ($ans -eq "N") {

exit

} else {

$vol = Read-Host "Enter volume name"

$size = Read-Host "How much more space do you want to give it? (example: +100g)"

Set-NaVolsize -Name $vol -NewSize $size -Confirm

Write-Host ""

Write-Host ""

Write-Host "Check the new size:" -foregroundcolor yellow

Write-Host ""

Write-Host ""

Get-NaVol -Name $vol | select name,used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.Available/1gb)}},@{Name="Total Size (in GB)";Expression={[math]::truncate($_.sizetotal/1gb)}}

}

}

Until ( $ans -eq "N")

Enjoy!

View solution in original post

10 REPLIES 10

JGPSHNTAP
8,340 Views

Ok, so let me address a few issues here

First, you shouldn't be importing the module every time you run the script.  You should add it to your profile.  And someone mentioned to me that the module will load automatically in 3.0, but i haven't tested that.  Look up Powershell profiles. 

First, you should eliminate all the Write-host ""

You should use something like

Write-Host "`n`nCheck the new size:`n`n" -foregroundcolor yellow

Also, as stated in previous email, you should be using the convertto-formattednumber cmdlet in place of - Expression={[math]::truncate($_.Available/1gb)}},    It's a custom Dataontap module cmdlet that will help with other future scripts

As for your loop. you need to focus on something like Do.... Until.

something like

Do {

$ans = Read-Host -prompt "Would you like to increase one of the volumes? [Y/N]"

write-host ""

write-host ""

if ($ans -eq "N") {

exit

} else {

$vol = Read-Host "Enter volume name"

$size = Read-Host "How much more space do you want to give it? (example: +100g)"

Set-NaVolsize -Name $vol -NewSize $size -Confirm

Write-Host ""

Write-Host ""

Write-Host "Check the new size:" -foregroundcolor yellow

Write-Host ""

Write-Host ""

Get-NaVol -Name $vol | select name,used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.Available/1gb)}},@{Name="Total Size (in GB)";Expression={[math]::truncate($_.sizetotal/1gb)}}

}

}

Until ( $ans -eq "N")

Enjoy!

JGPSHNTAP
8,320 Views

And if you are going to do it that way, you should probably remove the if/else

JSHACHER11
8,320 Views

sweet, thanks

so it should look like that?

Do {

$ans = Read-Host -prompt "Would you like to increase one of the volumes? [Y/N]"

$vol = Read-Host "`n`nEnter volume name`n`n"

$size = Read-Host "`n`nHow much more space do you want to give it? (example: +100g)`n`n"

Set-NaVolsize -Name $vol -NewSize $size -Confirm

Write-Host "`n`nCheck the new size:`n`n" -foregroundcolor yellow

Get-NaVol -Name $vol | select name,used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.Available/1gb)}},@{Name="Total Size (in GB)";Expression={[math]::truncate($_.sizetotal/1gb)}}

}

Until ( $ans -eq "N")

KARL_BUSTAMANTE
8,320 Views

Hi Joel,

Why not use :

function volincrease {

    $vol = Read-Host "`n`nEnter volume name`n`n"

    $size = Read-Host "`n`nHow much more space do you want to give it? (example: +100g)`n`n"

    Set-NaVolsize -Name $vol -NewSize $size -Confirm

    Write-Host "`n`nCheck the new size:`n`n" -foregroundcolor yellow

    Get-NaVol -Name $vol | select name,used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.Available/1gb)}},@{Name="Total Size (in GB)";Expression={[math]::truncate($_.sizetotal/1gb)}}

}

        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""

        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""

        $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)

        $caption = "Volume Increase"

        $message = "Would you like to increase one of the volumes?"

        $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)

        if($result -eq 0) { volincrease }

        if($result -eq 1) { exit }

JSHACHER11
8,320 Views

Hi Karl

a. Josh told me not to use functions

b. That is a bit beyond my basic knowledge. What is 'New-Object System.Management.Automation.Host.ChoiceDescription'?

Cheers

JGPSHNTAP
8,320 Views

No, I said, in your last implementation of the script a function was pointless...

A function in this case would be good...

JGPSHNTAP
8,320 Views

I tend to lean more towards switch statements as well in this nstance.

Switch ( $ans )

{

    0 { volincrease}

    1 {

        Write-Host "`nYou have selected Exit`n"

        timeout /t -1

         cls

        Exit

}

}

KARL_BUSTAMANTE
8,319 Views

Import-Module Dataontap

function volincrease {

    $vol = Read-Host "`n`nEnter volume name`n`n"

    $size = Read-Host "`n`nHow much more space do you want to give it? (example: +100g)`n`n"

    Set-NaVolsize -Name $vol -NewSize $size -Confirm

    Write-Host "`n`nCheck the new size:`n`n" -foregroundcolor yellow

    Get-NaVol -Name $vol | select name,used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.Available/1gb)}},@{Name="Total Size (in GB)";Expression={[math]::truncate($_.sizetotal/1gb)}}

}

write-host "This script will list volumes over 80% full, the containing aggregates and snapmirror relationships" -foregroundcolor yellow

write-host "`n`nYou will also get an option to increase a volume size`n`n" -foregroundcolor yellow

$filer = Read-Host "Enter the filer name"

$user = Read-Host "Enter username"

Connect-NaController -Name $filer -Credential $user

do {

    get-navol | ? {$_.used -gt 80} | select name,used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.Available/1gb)}},@{Name="Total Size (in GB)";Expression={[math]::truncate($_.sizetotal/1gb)}} | ft -Autosize | Out-String

    write-host "`n`nHere are the aggregates:`n`n" -foregroundcolor yellow

    get-naaggr | select name,@{Name="Total Size (in GB)";Expression={[math]::truncate($_.totalsize/1gb)}},used,@{Name="Available Size (in GB)";Expression={[math]::truncate($_.available/1gb)}} | ft -Autosize | Out-String

    write-host "`n`nand the snapmirror relationships:`n`n" -foregroundcolor yellow

    get-navol | ? {$_.used -gt 80} | get-nasnapmirror | select source,destination | ft -Autosize | Out-String

       

    write-host "`n`n"

    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""

    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""

    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)

    $caption = "Volume Increase"

    $message = "Would you like to increase one of the volumes?"

    $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)

    if($result -eq 0) { volincrease }

    if($result -eq 1) { exit }

} while (1 -gt 0)

KARL_BUSTAMANTE
8,320 Views

Check out my script for powershell examples :         http://mymsworld.kahsky.com/?p=54

This works actually only for Cluster mode, I'll add some 7-mode utilities soon.

Cheers,

Karl

JSHACHER11
7,429 Views

thanks guys!

Public