Microsoft Virtualization Discussions

Not sure why this script errors

grahamk
3,591 Views

Please help a serious newbie to powershell. 🙂

Thanks to a lot of plagiarism from this community I was able to put together powershell line that outputs exactly what I need to see in the format I want to see it. It outputs snapshots older than 14 days and their sizes in GB:

Get-NaVol |
Get-NaSnapshot |
where-object {$_.AccessTimeDT -le $Now.AddDays(-14) -and $_.Dependency -eq "" } |
select TargetName,Name,Created,Total,CumulativeTotal,Dependency |
ft @{Expression={$_.TargetName};Label="Volume";Width=20},@{Expression={$_.Name};Label="Name";Width=25},@{Expression={$_.Created.ToShortDateString()};Label="Created";Width=12},@{Expression={ConvertTo-FormattedNumber $_.Total DataSize "0.0"};Label="Total";Width=10},@{Expression={ConvertTo-FormattedNumber $_.CumulativeTotal DataSize "0.0"};Label="Cumulative";Width=10},@{Expression={$_.Dependency};Label="Dependency"}

This works perfectly if I run it as a standalone line after I have already connected to a filer.

But if I put it it in  the most simple of loops it errors out - I cannot for the life of me figure out why:

$pass="p@ssword"
$user = "root"
$NetappList=("90.0.0.2","90.0.0.3")
$password = ConvertTo-SecureString $pass -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$password
$WarningDays = 14
foreach ($np in $NetappList) {

    Connect-nacontroller $np -Credential $cred


    Get-NaVol |
    Get-NaSnapshot |
    where-object {$_.AccessTimeDT -le $Now.AddDays(-14) -and $_.Dependency -eq "" } |
    select TargetName,Name,Created,Total,CumulativeTotal,Dependency |
    ft @{Expression={$_.TargetName};Label="Volume";Width=20},@{Expression={$_.Name};Label="Name";Width=25},@{Expression={$_.Created.ToShortDateString()};Label="Created";Width=12},@{Expression={ConvertTo-FormattedNumber $_.Total DataSize "0.0"};Label="Total";Width=10},@{Expression={ConvertTo-FormattedNumber $_.CumulativeTotal DataSize "0.0"};Label="Cumulative";Width=10},@{Expression={$_.Dependency};Label="Dependency"}


}

Output and error:

Name                 Address           Ontapi   Version
----                 -------           ------   -------
90.0.0.2             90.0.0.2          1.15     NetApp Release 8.1RC3 7-Mode: Wed Feb 15 19:28:21 PST 2012
out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not
in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with th
e default formatting.
    + CategoryInfo          : InvalidData: (:) [out-lineoutput], InvalidOperationException
    + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand

Would anyone happen to know why this happens or know how to fix it??

Thanks!!!

1 ACCEPTED SOLUTION

JSHACHER11
3,591 Views
2 REPLIES 2

JSHACHER11
3,592 Views

grahamk
3,591 Views

THANKS!!! Very much appreciated....

Graham

Public