Microsoft Virtualization Discussions

foreach from file not working

JSHACHER11
5,066 Views

I created a script to show aggregates more than 90% full on multiple filers but they would not connect

the text file is like this:

"filer1", "filer2", "filer3"

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

$ntapArrays = (get-content "C:\filers.txt")

$FasUser = "root"

$FasPasswd = Read-Host "Enter password"

$SecureFasPasswd = ConvertTo-SecureString $FasPasswd -AsPlainText -Force

$FasCred = New-Object -TypeName System.Management.Automation.PSCredential ($FasUser, $SecureFasPasswd)

Foreach ($array in $ntapArrays) {

connect-nacontroller -Name $array -Credential $FasCred

Get-Naaggr | select homename, SizePercentageUsed, name | ? {$_.SizePercentageUsed -gt 90} | ft -AutoSize

}

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

the error messages is

Connect-NaController : Cannot bind argument to parameter 'Name' because it is a

n empty string.

At C:\script2.ps1:30 char:27

+ connect-nacontroller -Name <<<<  $array -Credential $FasCred

    + CategoryInfo          : InvalidData&colon; (:) [Connect-NaController], Paramet

   erBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl

   lowed,DataONTAP.PowerShell.SDK.ConnectNaController

what am I doing wrong?

Cheers

1 ACCEPTED SOLUTION

vinith
5,062 Views

The way you are saving filer names in filers.txt is wrong, you need to append them as below

View solution in original post

10 REPLIES 10

vinith
5,063 Views

The way you are saving filer names in filers.txt is wrong, you need to append them as below

JSHACHER11
5,039 Views

OK, the Controllers are connecting now but I still don't get the aggregates info. Something is wrong here:

Foreach ($array in $ntapArrays) {

connect-nacontroller -Name $array -Credential $FasCred

Get-Naaggr | select homename, SizePercentageUsed, name | ? {$_.SizePercentageUsed -gt 90} | ft -AutoSize

}

vinith
5,039 Views

What is the output of

Get-NaAggr | select homename,SizePercentageUsed,name | ft -AutoSize

Can you confirm that you do have aggregates whose SizePercentageUsed is greater than 90?SizePercentageUsed is greater than 90?

JSHACHER11
5,039 Views

Vinith, you were right. That list didn't have aggregate with more than 90%

Thank you

vinith
5,039 Views

Thank You For letting us know that

KARL_BUSTAMANTE
5,039 Views

Hi Joel,

Instead of "filer1", "filer2", "filer3" save your txt as filer1,filer2,filer3

You can do it this way :

$ntapArrays = (get-content "C:\filers.txt")

$ntapArrays = $filertxt.Split(',')

Foreach ($array in $ntapArrays) {

...

JSHACHER11
5,039 Views

OK, the Controllers are connecting now but I still don't get the aggregates info. Something is wrong here:

Foreach ($array in $ntapArrays) {

connect-nacontroller -Name $array -Credential $FasCred

Get-Naaggr | select homename, SizePercentageUsed, name | ? {$_.SizePercentageUsed -gt 90} | ft -AutoSize

}

JGPSHNTAP
5,039 Views

The easiest thing to do here is to list them in a text file as stated above

filer1

filer2

filer3

Then just do

$filers = gc c:\filers.txt

That will create the array

Then just put that array into a loop

$filers | % {

$c = connect-nacontroller $_

Put code here

}

Also, you are doing something wrong in my opinion.  You need to put your where clause before your select statement. 

Get-Naaggr |  ? {$_.SizePercentageUsed -gt 90}| select homename, SizePercentageUsed, name |  | ft -AutoSize

JSHACHER11
5,039 Views

Thank you all - I will apply the advices

JGPSHNTAP
4,229 Views

Joel -

What your doing will look good if you want to stare at the screen - But you do you really want to do that.  Powershell is all about automation.

You want to have this as a scheduled tasks that emails you a report.

So, here's the basics.

$globalarray = @()

$filers | % {
$filer = $_
$c = connet-nacontroller $filer

$aggr = get-naaggr | ? {$_.SizePercentageUsed -gt 10}

if ($aggr -ne $NUll) {
       $aggr | % {
  $customobject = new-object psobject
       Add-Member -inputobject $customobject -membertype Noteproperty -name filer -value $filer
          Add-Member -inputobject $customobject -membertype Noteproperty -name "Aggr" -value $_.name
          Add-Member -inputobject $customobject -membertype Noteproperty -name "total size" -value (convertto-formattednumber $_.totalsize datasize "0.0")
    Add-Member -inputobject $customobject -membertype Noteproperty -name "% used" -value (convertto-formattednumber $_.used percent)

   $globalarray += $customobject
    
   }
     }
    }
   
    $globalarray | ft -autosize
   

  $globalarray | export-csv c:\temp\aggrereport.csv -notypeinformation.

Do some reading on custom objects and see if you can come up with sending this report to you.. 

Public