Microsoft Virtualization Discussions

Not sure why this script errors

grahamk

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
2 REPLIES 2

JSHACHER11

grahamk

THANKS!!! Very much appreciated....

Graham

Announcements
Register for Insight 2021 Digital

INSIGHT 2021 Digital: Meet the Specialists 2

On October 20-22, gear up for a fully digital, totally immersive virtual experience with a downright legendary lineup of world-renowned specialists. Tune in for visionary conversations, solution deep dives, technical sessions and more.

NetApp on Discord Image

We're on Discord, are you?

Live Chat, Watch Parties, and More!

Explore Banner

Meet Explore, NetApp’s digital sales platform

Engage digitally throughout the sales process, from product discovery to configuration, and handle all your post-purchase needs.

NetApp Insights to Action
I2A Banner
Public