Weight.psm1
function Add-WeightData { <# .SYNOPSIS Imports the entered data for the specific week to a csv file .DESCRIPTION Imports the weight data imported by the user in a csv file. The csv file can be found on the users desktop. This command is used to import data to a specific place so that data later can be used with other commands. Useful commands to use after imported data: Get-WeightData Compare-WeightData Remove-WeightData .EXAMPLE Add-WeightData -Date 2020-04-20 -Week 1 -Weight 80 -Bodyfat 17 -Hydration 57 -Muscle 43 Date Week Weight Bodyfat Hydration Muscle ---- ---- ------ ------- --------- ------ 2020-04-20 1 80 17% 57% 43% .FUNCTIONALITY This command is used to import data to a specific place so that data later can be used with other commands. Useful commands to use after imported data: Get-WeightData Compare-WeightData Remove-WeightData #> [Alias('addW')] [CmdletBinding()] param ( [Parameter( HelpMessage='Enter the date.', Mandatory=$true, ValueFromPipeline=$true, Position = 0 )] [datetime] $Date, [Parameter( HelpMessage='Enter the specific week, not the wwek of the year.', Mandatory=$true, ValueFromPipeline=$true, Position = 1 )] [int] $Week, [Parameter( HelpMessage='Enter how much you weigh, only the digits', Mandatory=$true, ValueFromPipeline=$true, Position = 2 )] [decimal] $Weight, [Parameter( HelpMessage='Enter the amount of bodyfat, only the digits.', Mandatory=$true, ValueFromPipeline=$true, Position = 3 )] [decimal] $Bodyfat, [Parameter( HelpMessage='Enter the amount of hydration, only the digits.', Mandatory=$true, ValueFromPipeline=$true, Position = 4 )] [decimal] $Hydration, [Parameter( HelpMessage='Enter the amount of muscle, only the digits.', Mandatory=$true, ValueFromPipeline=$true, Position = 5 )] [decimal] $Muscle ) $BodyfatWithPercentage = "$Bodyfat%" $HydrationWithPercentage = "$Hydration%" $MuscleWithPercentage = "$Muscle%" $Output = [PSCustomObject]@{ Date = "{0:yyyy}-{0:MM}-{0:dd}" -f $Date Week = $Week Weight = $Weight Bodyfat = $BodyfatWithPercentage Hydration = $HydrationWithPercentage Muscle = $MuscleWithPercentage DataImported = "{0:yyyy}-{0:MM}-{0:dd} {0:HH}:{0:mm}" -f (Get-Date) } try { $TestPath = Test-Path -Path $env:HOMEDRIVE\Users\$env:USERNAME\Desktop\WeightData.csv if ($TestPath -eq $false){ Write-Verbose -Message 'Gathering and imports the entered data into a newly created CSV file.' New-Item -Path "$env:HOMEDRIVE\Users\$env:USERNAME\Desktop\WeightData.csv" $Output | ConvertTo-Csv -NoTypeInformation | Add-Content -Path $env:HOMEDRIVE\Users\$env:USERNAME\Desktop\WeightData.csv -ErrorAction Stop Write-Output $Output | Format-Table -Property Date, Week, Weight, Bodyfat, Hydration, Muscle -ErrorAction Stop } else{ Write-Verbose -Message 'Gathering and imports the entered data into a CSV file' $Output | ConvertTo-Csv -NoTypeInformation | Add-Content -Path $env:HOMEDRIVE\Users\$env:USERNAME\Desktop\WeightData.csv -ErrorAction Stop Write-Output $Output | Format-Table -Property Date, Week, Weight, Bodyfat, Hydration, Muscle -ErrorAction Stop } } catch { Write-Error -Message "$($Error[0].Exception.Message)" } } function Get-WeightData { <# .SYNOPSIS Gatheres weight data from specified week and presents it to the user. .DESCRIPTION Before using this, make sure to use "Add-WeightData", otherwise it won't work. If user specifies a week, data will be gathered for that week from a CSV file and presents it to the user. If user specifies wildcard '*', all weeks will be presented to the user. This command is used to display weight data. Before using, make sure to use "Add-WeightData" first, otherwise it won't work Other commands related to Get-WeightData: Add-WeightData Compare-WeightData Remove-WeightData .EXAMPLE Get-WeightData -Week 1 Date Week Weight Bodyfat Hydration Muscle ---- ---- ------ ------- --------- ------ 2020-04-20 1 80 17% 57% 43% 2020-04-20 1 80 17% 57% 43% .EXAMPLE Get-WeightData -Week * Date Week Weight Bodyfat Hydration Muscle ---- ---- ------ ------- --------- ------ 2020-04-20 1 80 17% 57% 43% 2020-04-20 2 82 16% 57% 43% 2020-04-20 3 83 15% 57% 44% .FUNCTIONALITY This command is used to display weight data. Before using, make sure to use "Add-WeightData" first, otherwise it won't work Other commands related to Get-WeightData: Add-WeightData Compare-WeightData Remove-WeightData #> [Alias('getW')] [CmdletBinding()] param ( [Parameter( HelpMessage='Specify a week or wildcard "*". * = All weeks.', Mandatory=$true, ValueFromPipeline=$true, Position = 0 )] [SupportsWildcards()] [string] $Week ) $OutputData = Import-Csv -Path $env:HOMEDRIVE\Users\$env:USERNAME\Desktop\WeightData.csv if ($Week -eq '*'){ Write-Verbose -Message "Gathering data from all weeks" $OutputData | Where-Object -Property Week -NE Week| Format-Table Date, Week, Weight, Bodyfat, Hydration, Muscle Write-Verbose -Message "Gathered data from all weeks and presented it to the user" } else { Write-Verbose -Message "Gathering data from week$($Week)" $OutputData | Where-Object -Property 'Week' -EQ $Week | Format-Table -Property Date, Week, Weight, Bodyfat, Hydration, Muscle -ErrorAction Stop Write-Verbose -Message "Gathered data from week$($Week) and presented it to the user" } if ($Week -notin $OutputData.week -and $Week -ne '*') { Write-Warning "Cannot get the data for 'Week$($Week)', beacuse the specified week does not exist" } } function Compare-WeightData { <# .SYNOPSIS Compares weight data from two different weeks. .DESCRIPTION Gathers data from the two specified weeks and compares them with eachother. The data comes from a CSV file. There is a option to compare all data between two weeks or just compare a specific type of data. Other commands related to Remove-WeightData: Add-WeightData Get-WeightData Compare-WeightData .EXAMPLE Compare-WeightData -Week 1 -With 2 Date Week Weight Bodyfat Hydration Muscle DataImported ---- ---- ------ ------- --------- ------ ------------ 2020-04-20 1 80 17% 57% 43% 2020-04-29 20:18 2020-04-20 2 82 16% 57% 43% 2020-04-25 12:54 .EXAMPLE Compare-WeightData -Week 1 -With 2 -DataType Bodyfat Bodyfat Week ------- ---- 17% 1 16% 2 .FUNCTIONALITY This command is used to compare data from CSV file. Other commands related to Compare-WeightData: Add-WeightData Get-WeightData Remove-WeightData #> [Alias('compareW')] [CmdletBinding()] param ( [Parameter( HelpMessage = 'Specify one week thats going to be compared to the other one.', ValueFromPipeline=$true, Position = 0 )] [int] $Week, [Parameter( HelpMessage = 'Specify one week thats going to be compared to the other one.', ValueFromPipeline=$true, Position = 1 )] [int] $With, [Parameter( HelpMessage = 'Specify the type of data thats going to be compared, example: Weight, Bodyfat, Hydration, Muscle', ValueFromPipeline=$true, Position = 2 )] [SupportsWildcards()] [string] $DataType ) $OutputData = Import-Csv -Path $env:HOMEDRIVE\Users\$env:USERNAME\Desktop\WeightData.csv Write-Verbose -Message "Imports data from CSV file" if ($DataType -eq "" -or $DataType -eq '*') { $Week1Data = $OutputData | Where-Object -Property Week -EQ $Week $Week2Data = $OutputData | Where-Object -Property Week -EQ $With $Week1Data, $Week2Data | Format-Table -Property * Write-Verbose -Message "Displays all data and compares them." } elseif ($DataType -ne $null -and $DataType -ne '*'){ $Week1Data = $OutputData | Where-Object -Property 'Week' -EQ $Week $Week2Data = $OutputData | Where-Object -Property 'Week' -EQ $With $Week1Data, $Week2Data | Format-Table -Property $DataType, Week } if ($DataType -ne "" -and $DataType -notin $OutputData.$DataType -and $DataType -ne '*') { Write-Warning "The specified datatype: $($DataType) does not exist." } if ($Week -notin $OutputData.week) { Write-Warning "Week $($Week) does not exist." } if ($With -notin $OutputData.week) { Write-Warning "Week $($With) does not exist." } } function Remove-WeightData (){ <# .SYNOPSIS Removes the specified week. .DESCRIPTION Removes the data from a specific week entered by the user. When the data has been removed it's gone for ever. This command is used to remove data from CSV file so the user gets a easy way to remove incorrectly entered data. Other commands related to Remove-WeightData: Add-WeightData Get-WeightData Compare-WeightData .EXAMPLE Remove-WeightData -Week 1 Week 1 were successfully removed. .FUNCTIONALITY This command is used to remove data from CSV file so the user gets a easy way to remove incorrectly entered data. Other commands related to Remove-WeightData: Add-WeightData Get-WeightData Compare-WeightData #> [Alias('removeW')] [CmdletBinding()] param ( [Parameter( HelpMessage='Enter the week that should be removed.', Mandatory=$true, ValueFromPipeline=$true, Position = 0 )] [int] $Week ) Write-Verbose -Message "Imports the CSV file." $OutputData = Import-Csv -Path $env:HOMEDRIVE\Users\$env:USERNAME\Desktop\WeightData.csv if ($Week -notin $OutputData.Week){ Write-Warning -Message "Cannot remove week $Week, beacuse it does not exist." } else { Write-Verbose -Message "Deletes week $week from the CSV file." $OutputData | Where-Object {$_.Week -ne $Week} | Export-Csv -Path $env:HOMEDRIVE\Users\$env:USERNAME\Desktop\WeightData.csv -NoTypeInformation Write-Output "Week $Week were successfully removed." } } <# F�rslag till att f� '%', funkar n�r hydration slutar p� 0, annars inte. - ta bort $Hydration = 57 $Hydration1 = 0 | % {$_.ToString("0.$Hydration")} $Hydration2 = $Hydration1 -replace ',', "." "{0:p0}" -f [decimal]$Hydration2 #> |