Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - math with "GB/MB/KB" in variables fails, without variables works?

Reply
 
Old 11-01-2008   #1 (permalink)
James R. Gentile


 
 

math with "GB/MB/KB" in variables fails, without variables works?

I'm making a script to calculate time for a file to download, I want to be
able to input things like "10GB" and "5GB" and "3.3KB" in the following
script:

$totalsize=read-host "Input file size (gb,mb,kb suffixes allowed)"
$dledsize=read-host "Input Number of downloaded bytes (gb,mb,kb suffixes
allowed)"
$dlspeed=read-host "Input download speed (gb,mb,kb suffixes allowed)"

$time=($totalsize-$dledsize)/$dlspeed/60/60/24

write-host [string]($time) + " days until file is downloaded."

but it gives me an error that says:

"Method invocation failed because [System.String] doesn't contain a method
named 'op_Subtraction'.
At line:1 char:19
+ $time=($totalsize- <<<< $dledsize)/$dlspeed/60/60/24"

but when I try the script in the shell, I can do things like
"$totalsize-5GB" and get the correct result. But I can't use two variables
with the size suffixes. Is this by design or a bug? And is there a (simple)
way around it?



My System SpecsSystem Spec
Old 11-01-2008   #2 (permalink)
Jeff


 
 

Re: math with "GB/MB/KB" in variables fails, without variables works?

On Nov 1, 12:27*pm, "James R. Gentile" <n...@xxxxxx> wrote:
Quote:

> I'm making a script to calculate time for a file to download, I want to be
> able to input things like "10GB" and "5GB" and "3.3KB" in the following
> script:
>
> $totalsize=read-host "Input file size (gb,mb,kb suffixes allowed)"
> $dledsize=read-host "Input Number of downloaded bytes (gb,mb,kb suffixes
> allowed)"
> $dlspeed=read-host "Input download speed (gb,mb,kb suffixes allowed)"
>
> $time=($totalsize-$dledsize)/$dlspeed/60/60/24
>
> write-host [string]($time) + " days until file is downloaded."
>
> but it gives me an error that says:
>
> "Method invocation failed because [System.String] doesn't contain a method
> named 'op_Subtraction'.
> At line:1 char:19
> + $time=($totalsize- <<<< $dledsize)/$dlspeed/60/60/24"
>
> but when I try the script in the shell, I can do things like
> "$totalsize-5GB" and get the correct result. But I can't use two variables
> with the size suffixes. Is this by design or a bug? And is there a (simple)
> way around it?
James,

Use the Invoke-Expression Cmdlet on each of the input values to
evaluate them to their numerical equivalents:

$value = Invoke-Expression ( Read-Host "Input file size..." )

Jeff
My System SpecsSystem Spec
Old 11-01-2008   #3 (permalink)
tojo2000


 
 

Re: math with "GB/MB/KB" in variables fails, without variables works?

On Nov 1, 2:48*am, Jeff <jeff.hill...@xxxxxx> wrote:
Quote:

> On Nov 1, 12:27*pm, "James R. Gentile" <n...@xxxxxx> wrote:
>
>
>
Quote:

> > I'm making a script to calculate time for a file to download, I want tobe
> > able to input things like "10GB" and "5GB" and "3.3KB" in the following
> > script:
>
Quote:

> > $totalsize=read-host "Input file size (gb,mb,kb suffixes allowed)"
> > $dledsize=read-host "Input Number of downloaded bytes (gb,mb,kb suffixes
> > allowed)"
> > $dlspeed=read-host "Input download speed (gb,mb,kb suffixes allowed)"
>
Quote:

> > $time=($totalsize-$dledsize)/$dlspeed/60/60/24
>
Quote:

> > write-host [string]($time) + " days until file is downloaded."
>
Quote:

> > but it gives me an error that says:
>
Quote:

> > "Method invocation failed because [System.String] doesn't contain a method
> > named 'op_Subtraction'.
> > At line:1 char:19
> > + $time=($totalsize- <<<< $dledsize)/$dlspeed/60/60/24"
>
Quote:

> > but when I try the script in the shell, I can do things like
> > "$totalsize-5GB" and get the correct result. But I can't use two variables
> > with the size suffixes. Is this by design or a bug? And is there a (simple)
> > way around it?
>
> James,
>
> Use the Invoke-Expression Cmdlet on each of the input values to
> evaluate them to their numerical equivalents:
>
> $value = Invoke-Expression ( Read-Host "Input file size..." )
>
> Jeff
If you do that you'll definitely want to run the input through a regex
before running Invoke-Expression, otherwise a user could enter a
command as their input and it would be executed:

$totalsize=read-host "Input file size (gb,mb,kb suffixes allowed)"

if ($totalsize -match '^\d+[GKM]B$') {
$totalsize = Invoke-Expression $totalsize
} else {
Write-Error "I don't think so."
}
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
When do I have do define variables with "DIM" ? VB Script
Vista Upgrade fails at "Gathering Files" when two users share the same user profile (ProfileImagePath). Message: "the upgrade was cancelled". Vista installation & setup
Patch works with "right-click-apply", fails MsiApplyPatch, MsiExec Vista hardware & devices


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46