If you did not read the first post in this series, then do now. It is vital, as I am not about to explain everything that is going on with the updated function that will be a part of this post. So, from this point forward, it is assumed that you have read it.
In the first post of this series, there was only a single apartment complex to watch. Now there are three. I mentioned that inventory is low. Therefore, the first option may not actually be an option for him. He, and I, kind of lucked out here. That is because the other two complexes of interest must be owned/run/whatever by the same management company. Lucky for everyone I suppose, the floor plan webpages all have the same embedded JSON. Let’s start with an image of the function from part one of the series. You may want to open this in a separate tab. It depends on how close you are going to follow along.
The changes that have been made from the above function to the below are function are the following.
- The
CmdletBinding
attribute was added along with aParam
block. - A parameter, named
ApartmentComplex
, was added.
Note: This usesĀValidateSet
and includes each of the three apartment complexes. Additionally, when the function is invoked by default all three complexes are chosen. - A variable reassignment happens to the
$ApartmentComplex
variable usingSelect-Object -Unique
.
Note: I have been known to do this to remove duplicates, such asWatch-Apartment -ApartmentComplex Edgewood,Creekside,Edgewood
. - The
$ComplexUriHash
hash table is created in order that we have a unique URI available for each apartment complex. - A
Foreach
loop was added so we are able to loop through each apartment complex.
If you ever need to loop through an array and select a specific key-value pair from a hash table during each iteration, then this is the post to remember. I cannot be sure if I have done this before, but I know I have now. Up until I search some search engine and end up back on my own site; it has happened.
Set-Alias -Name wa -Value Watch-Apartment function Watch-Apartment { # Version 2.0.0 [CmdletBinding()] Param ( [Parameter()] [ValidateSet('Creekside','Edgewood','Presidio')] [System.Array]$ApartmentComplex = ('Creekside','Edgewood','Presidio') ) $ApartmentComplex = $ApartmentComplex | Select-Object -Unique $ComplexUriHash = @{ Creekside = 'https://theplaceatcreekside.securecafe.com/onlineleasing/the-place-at-creekside/floorplans' Edgewood = 'https://mclife.securecafe.com/onlineleasing/the-place-at-edgewood/floorplans' Presidio = 'https://mclife.securecafe.com/onlineleasing/the-place-at-presidio-trails/floorplans' } Foreach ($Complex in $ApartmentComplex) { if ([System.Boolean]$ComplexUriHash[$Complex] -eq $true) { $Uri = $ComplexUriHash[$Complex] $WebRequestContent = (Invoke-WebRequest -Uri $Uri).Content $ContentPath = "C:\users\tmaynard\Dropbox\_tommymaynard.com\Apartment Hunting with PowerShell\WebpageContents$Complex.txt" # $ContentPath = "C:\users\tmaynard\Dropbox\_tommymaynard.com\Apartment Hunting with PowerShell\WebpageContentsCreekside.txt" Set-Content -Path $ContentPath -Value $WebRequestContent $File = Get-Content -Path $ContentPath $Pattern = "floorplans:(.*?)propertyID:" $ParsedPage = [regex]::Match($File,$Pattern).Groups[1].Value $ParsedPage = $ParsedPage.Trim(); $ParsedPage = $ParsedPage.TrimEnd(',') $JsonDocument = ConvertFrom-Json -InputObject $ParsedPage $JsonDocument | Select-Object -Property @{Name='Complex';Expression={$Complex}}, @{Name='Available';Expression={if ($_.isFullyOccupied -eq 0) {"Yes ($($_.availableCount))"} else {'No'}}}, @{Name='Model';Expression={$_.name}}, @{Name='Sq.Ft.';Expression={$_.sqft}}, @{Name='Beds';Expression={$_.beds}}, @{Name='Baths';Expression={$_.baths}}, @{Name='Price';Expression={$_.tilePrice}} | Format-Table -AutoSize } } }
The below image shows the results when this is run against all three apartment complexes. Fun project, but no more updates for me on this one! I did not mention these above, but there are a couple more additions, however. On the left, I have added the Complex name so these are distinguishable from one another. And on the right, I added the prices. You can see the modified Select-Object
in the above function. Again, I am done here.