-1

I need to create an object and each iteration add values to. The problem what i have is I thing with the logic of the code. I think I have to create another foreach and place New-Object psobject -Property $object outside...but i don't know how to do it.

Can someone help me?

Import-Module BEMCLI  #BackupExec

$object= @{}
#For each last 15 job backups
ForEach ($i in $(Get-BEJob "FULL" | Get-BEJobHistory -FromStartTime (Get-Date).AddDays("-15") -JobType Backup))
 { 

 $object.TAPE= ($i | Get-BEJobLog | Get-BEJobLogFiles) # get Tape
 $object.START_TIME=$i.StartTime #get starttime
 $object.END_TIME=$i.EndTime #get endtime

 New-Object psobject -Property $object
 } 

$object | ft
4

2 Answers 2

1

Create another Array Object, and add your $object data into it on every iteration

Import-Module BEMCLI  #BackupExec

$Array = @()
$object= @{}
#For each last 15 job backups
ForEach ($i in $(Get-BEJob "FULL" | Get-BEJobHistory -FromStartTime (Get-Date).AddDays("-15") -JobType Backup))
 { 

 $object.TAPE= ($i | Get-BEJobLog | Get-BEJobLogFiles) # get Tape
 $object.START_TIME=$i.StartTime #get starttime
 $object.END_TIME=$i.EndTime #get endtime

 $Array += $Object
 } 

$Array | ft
Sign up to request clarification or add additional context in comments.

Comments

0

That foreach construct you have looks like it is designed to send data down the pipeline. Not only are you not attempting to capture the data it does not send items down the pipeline anyway. The simplest thing to change would be to assign the foreach loop to a variable

Import-Module BEMCLI  #BackupExec

#For each last 15 job backups
$objects = ForEach ($i in $(Get-BEJob "FULL" | Get-BEJobHistory -FromStartTime (Get-Date).AddDays("-15") -JobType Backup))
{ 
    $object = @{} # Wipe object for next run. 
    $object.TAPE= ($i | Get-BEJobLog | Get-BEJobLogFiles) # get Tape
    $object.START_TIME=$i.StartTime #get starttime
    $object.END_TIME=$i.EndTime #get endtime

    New-Object psobject -Property $object
} 

$objects | ft

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.