Powershell - determining provided file size from customers

I'm expecting end-users to send an email with a request containing a file size limitation.  My script reads the email then dumps those values into a file that gets read in by another script.

The field I'm checking is simply called size (so the file contains a single line that reads size:xxx)

I'd like to capture as many scenarios as possible with powershell, so that I don't have to force the customer to fit a single mold.
What kind of logic could I have that would take as many values as possible and ensure everything is configured as a KB?

Example - if the user simply supplies an integer, I'm fine with assuming that's in KB.
But if the user supplies a trailing 'k' or 'kb', I'd want to 'only' gather the numeric value (500k or 500kb)
If the user supplies a trailing 'm' or 'mb', I'd want to gather the numeric value and convert it to kb (10m or 10mb).

I'm not sure if there are any easy methods for this type of conditional logic in powershell?
LVL 67
sirbountyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
That's a classic case for a regular expression, for example "\A\s*size:\s*(?<Size>\d+)\s*(?<Unit>[kmgt]?b?)\s*\Z":
\A - Match the start of the string
\s* - Match no or several whitespaces
(?<Size> - Open a capturing named group "Size"
\d+ - Match one or more decimal digits
) - Close the capturing group "Size"
\s* - Match no or several whitespaces
(?<Unit> - Open a capturing named group "Unit"
[kmgt]?b? - Match no or one character out of the set "kmgt", followed by no or one "b"
) - Close the capturing group "Size"
\s* - Match no or several whitespaces
\Z - Match the end of the string
Regular expression in PS are by default case insensitive.
This script expects whatever the customer would enter after "size", then turns that into the string line you'd read from the script, and calculates the KB size:
Param(
	[string]$InputSize = "100"
)
$Line = "size:" + $InputSize

$DefaultUnit = "kb"
If ($Line -match "\A\s*size:\s*(?<Size>\d+)\s*(?<Unit>[kmgt]?b?)\s*\Z") {
	$Size = [uint32]$Matches["Size"]
	$Unit = $Matches["Unit"]
	If ($Unit -eq "") {
		$Unit = $DefaultUnit
	}
	If (-Not $Unit.ToLower().EndsWith("b")) {
		$Unit += "b"
	}
	Switch ($Unit) {
		"b"		{$SizeInKB = [math]::Max($Size / 1024, 1)}
		"kb"	{$SizeInKB = $Size}
		"mb"	{$SizeInKB = $Size * 1mb / 1kb}
		"gb"	{$SizeInKB = $Size * 1gb / 1kb}
		"tb"	{$SizeInKB = $Size * 1tb / 1kb}
	}
	"Input size was: '$($InputSize)'; calculated size in KB: $($SizeInKB)" | Write-Host
} Else {
	"Unexpected format in Size line: $($Line)" | Write-Error
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.