Owain Esau
Owain Esau

Reputation: 1922

Appending to object with value already in object

So, I have the following code that is part of my spoof database creator - which works:

$Start_dateMin = get-date -year 2015 -month 1 -day 1 
$Start_dateMax = get-date

for ( $i = 0; $i -le (Get-Random -Minimum 1 -Maximum 2); $i++ ) {
    <# Create job data array #>
    $jobData = @{
        ReceivedDate  = (new-object datetime (Get-Random -min $Start_dateMin.ticks -max $Start_dateMax.ticks)).ToString("yyyy/MM/dd")
    }

    <# Add to job data array #>
    $jobData += @{
        StartDate   = (Get-Date $jobData.ReceivedDate).AddDays((Get-Random -Minimum 20 -Maximum 50)).ToString("yyyy/MM/dd")
        EndDate     = (Get-Date $jobData.ReceivedDate).AddDays((Get-Random -Minimum 100 -Maximum 500)).ToString("yyyy/MM/dd")
        ClosingDate = (Get-Date $jobData.ReceivedDate).AddDays((Get-Random -Minimum 30 -Maximum 100)).ToString("yyyy/MM/dd")
        UpdatedDate = (Get-Date $jobData.ReceivedDate).AddDays((Get-Random -Minimum 5 -Maximum 30)).ToString("yyyy/MM/dd")
        JobStatusID = if (( Get-Date $jobData.ReceivedDate ) -ge ((Get-Date).AddDays(-100))) { 5 } else { 1 }
    }
    <# Add to job data array #>
    $jobData += @{
        jobOutcomeID    =   if ( $jobData.JobStatusID -eq 1 -and (Get-Random -Minimum 0 -Maximum 100) -lt 70 ) { 1 }
                            elseif ( $jobData.JobStatusID -eq 1 -and (Get-Random -Minimum 0 -Maximum 100) -lt 85 ) { 2 }
                            else { (Get-Random -InputObject $JobOutcomeList -count 1).itemArray[0] }
    }
}
$jobData | format-table

There's no issue with this, all the data is populated correctly, heres some sample output:

Name                           Value                                                                                                                                                                                                         
----                           -----                                                                                                                                                                                                         
JobStatusID                    True                                                                                                                                                                                                          
UpdatedDate                    2017/01/30                                                                                                                                                                                                    
ClosingDate                    2017/04/23                                                                                                                                                                                                    
EndDate                        2017/09/16                                                                                                                                                                                                    
jobOutcomeID                   2                                                                                                                                                                                                             
StartDate                      2017/02/28                                                                                                                                                                                                    
ReceivedDate                   2017/01/14   

It is correctly using the received date from the declaration and creating dates based off that.

The second snippet of code does not work:

for ( $i = 0; $i -lt (Get-Random -Minimum 1 -Maximum 2); $i++) {
    $jobCandData = @{
        DateSelected = (Get-Date).AddDays((Get-Random -Minimum 1 -Maximum 30)).ToString("yyyy/MM/dd");
        JobCanOutcome = 7
    }


    $jobCandData += @{
        CandNote =  if ( $jobCandDate.JobCanOutcome -eq  7 ) { "test" };
        DateSubmitted = if ((Get-Random -Minimum 1 -Maximum 100) -gt 80) { (Get-Date $jobCandData.DateSelected).AddDays((Get-Random -Minimum 1 -Maximum 30)).ToString("yyyy/MM/dd") }; 
    }
    $jobCandData | Format-Table
}   

This does not work, everything in the append call does not populate, here is a sample result:

Name                           Value                                                                                                                                                                                                         
----                           -----                                                                                                                                                                                                         
DateSubmitted                                                                                                                                                                                                                                
CandNote                                                                                                                                                                                                                                     
DateSelected                   2018/03/16                                                                                                                                                                                                    
JobCanOutcome                  7            

As far as I can see there is no difference. The second snippet of code is running in a foreach loop foreach (job in jobs ) { } these jobs are created from the first section of code. The two variables DateSubmitted and CandNote do not populate with or without this foreach loop.

Basically, I am just trying to understand what it is I am missing here and why it is not populating correctly.

Upvotes: 0

Views: 45

Answers (1)

mklement0
mklement0

Reputation: 439477

It's a simple typo that

Set-StrictMode -Version 1
(or higher) would have caught, as PetSerAl recommends in a comment on the question:

The original variable names is $jobCandData- with a final a - yet you try to refer to it as $jobCandDate- with a final e.

By default, with no strict mode in effect, a reference such as $jobCandDate.JobCanOutcome - i.e., an attempt to access a property of a nonexistent variable simply yields $null, which in your case means that conditional $jobCandDate.JobCanOutcome -eq 7 always returns $False and therefore never assigns a value to the CandNote hashtable entry.

By contrast, assigning to the DateSubmitted entry works fine - it's just that it will only assign a value if (Get-Random -Minimum 1 -Maximum 100) -gt 80 happens to be $True, which is on average only true about 19% of the time.

Upvotes: 1

Related Questions