Powershell Division

I am trying to do division with the last column column but I am getting an error. I looks like the array value has the entire string not just the integer size value of drive. What am I doing wrong.

$Text = SQL-ClearTempDB -Computers $Server -ServerType $ServerYear | ? { $_.Drive -in 'E', 'T','S' } 
$OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f $Server, $Text[0].Drive.Replace("Space",""), $Text[0]."MB Free", $Text[3]."MB Free", [int]($Text[0]."MB Free") / [int]($Text[3]."MB Free")
$OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f $Server, $Text[1].Drive.Replace("Space",""), $Text[1]."MB Free", $Text[4]."MB Free", [int]($Text[1]."MB Free") / [int]($Text[4]."MB Free")
$OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f $Server, $Text[2].Drive.Replace("Space",""), $Text[2]."MB Free", $Text[5]."MB Free", [int]($Text[2]."MB Free") / [int]($Text[5]."MB Free")
                     $OutputBox.text += @"
"@

Open in new window


Error Message
Cannot convert value "ServerName        C            25388            25388            25388" to type "System.Int32". Error: "Input string was not 
in a correct format."
At C:\Automation\Front-End-GUIs\GUI_ClearTempDB.ps1:363 char:22
+                      $OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

Open in new window

LVL 8
Leo TorresSQL DeveloperAsked:
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.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
replace with:
[int] ($($Text[0]."MB Free")) / [int]($($Text[3]."MB Free"))
ie
$Text = SQL-ClearTempDB -Computers $Server -ServerType $ServerYear | ? { $_.Drive -in 'E', 'T','S' } 
$OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f $Server, $Text[0].Drive.Replace("Space",""), $Text[0]."MB Free", $Text[3]."MB Free", [int] ($($Text[0]."MB Free")) / [int]($($Text[3]."MB Free"))
$OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f $Server, $Text[1].Drive.Replace("Space",""), $Text[1]."MB Free", $Text[4]."MB Free", [int] ($($Text[1]."MB Free")) / [int]($($Text[4]."MB Free"))
$OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f $Server, $Text[2].Drive.Replace("Space",""), $Text[2]."MB Free", $Text[5]."MB Free", [int] ($($Text[2]."MB Free")) / [int]($($Text[5]."MB Free"))
                     $OutputBox.text += @"
"@

Open in new window

0
footechCommented:
Keep in mind that the "D" format specifier only does integers, so if you're interested in displaying decimal places, then you have to use something else.
0
Leo TorresSQL DeveloperAuthor Commented:
Sorry i have been out last 2 days but now I am getting the same error. The 25388 is the drive space but I dont understand why the the value has the entire row content I thought the  value in the [int] ($($Text[0]."MB Free"))  was just the drive space value?


Looks here look at what the value that's erroring out.
Cannot convert value "Servername         T            25388            25388" to type "System.Int32". Error: "Input string was 
not in a correct format."
At C:\Automation\Front-End-GUIs\GUI_ClearTempDB.ps1:202 char:212
+ ... e")) / [int]($($Text[5]."MB Free"))
+                    ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger
 

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Leo TorresSQL DeveloperAuthor Commented:
I dont understand why its says
$Text[5]."MB Free" = Servername         T            25388            25388

Open in new window

This is why this its failing. This value should be 25388. I dont understand whats going on here.
Should be more like
$Text[5]."MB Free" = 25388 
#This would allow the int conversion to work.

Open in new window

0
footechCommented:
The error is a bit misleading.
Look at this example to illustrate.
PS K:\powershell-test> $Text = @()
$Text += New-Object psobject -Property @{ "MB Free" = "100" }
$Text += New-Object psobject -Property @{ "MB Free" = "90" }
$Text += New-Object psobject -Property @{ "MB Free" = "80" }
$Text += New-Object psobject -Property @{ "MB Free" = "70" }

PS K:\powershell-test> "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f "blah0", "blah1", $Text[0]."MB Free", $Text[3]."MB Free", [int]($Text[0]."MB Free") / [int]($Text[3]."MB Free")
Cannot convert value "blah0         blah1              100               70              100" to type "System.Int32". 
Error: "Input string was not in a correct format."
At line:1 char:1
+ "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f "blah0", "blah1", $Tex ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger
 

PS K:\powershell-test> "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f "blah0", "blah1", $Text[0]."MB Free", $Text[3]."MB Free", ([int]($Text[0]."MB Free") / [int]($Text[3]."MB Free"))
Error formatting a string: Format specifier was invalid..
At line:1 char:1
+ "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f "blah0", "blah1", $Tex ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: ({0}         {1}         {2,...d}         {4,8:d} 
:String) [], RuntimeException
    + FullyQualifiedErrorId : FormatError
 

PS K:\powershell-test> "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:d} `r`n" -f "blah0", "blah1", $Text[0]."MB Free", $Text[3]."MB Free", [int]([int]($Text[0]."MB Free") / [int]($Text[3]."MB Free"))
blah0		 blah1		      100		       70		        1 


PS K:\powershell-test> "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:n} `r`n" -f "blah0", "blah1", $Text[0]."MB Free", $Text[3]."MB Free", ([int]($Text[0]."MB Free") / [int]($Text[3]."MB Free"))
blah0		 blah1		      100		       70		     1.43 

Open in new window

In case 1, it just can't figure out what it should substitute for the format operator.
In case 2, the format specifier "d" can't convert the number which is being substituted (which has a type of "double").
In case 3, the number has been converted to an int, before being substituted and displayed with the "d" specifier.
In case 4, the format specifier "n" converts the double which is being substituted.
0
Leo TorresSQL DeveloperAuthor Commented:
SO whats your solution?

Code
$Text = SQL-ClearTempDB -Computers $Server -ServerType $ServerYear | ? { $_.Drive -in 'E', 'T','S' } 
$OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:n} `r`n" -f $Server, $Text[0].Drive.Replace("Space",""), $Text[0]."MB Free", $Text[3]."MB Free", [int] ($($Text[0]."MB Free")) / [int]($($Text[3]."MB Free"))
 $OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:n} `r`n" -f $Server, $Text[1].Drive.Replace("Space",""), $Text[1]."MB Free", $Text[4]."MB Free", [int] ($($Text[1]."MB Free")) / [int]($($Text[4]."MB Free"))
$OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:n} `r`n" -f $Server, $Text[2].Drive.Replace("Space",""), $Text[2]."MB Free", $Text[5]."MB Free", [int] ($($Text[2]."MB Free")) / [int]($($Text[5]."MB Free"))
$OutputBox.text += @"

Open in new window


Error
Cannot convert value "Servername         T            25388            25388         25,388.00" to type "System.Int32". Error: "Input string was 
not in a correct format."
At C:\Automation\Front-End-GUIs\GUI_ClearTempDB.ps1:202 char:224
+ ... e")) / [int]($($Text[5]."MB Free"))
+                    ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

Open in new window

0
footechCommented:
I think if you look closely at the examples I posted and compared them to what you just posted you would see the difference.
$OutputBox.text += "{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:n} `r`n" -f $Server, $Text[0].Drive.Replace("Space",""), $Text[0]."MB Free", $Text[3]."MB Free", ([int]($Text[0]."MB Free") / [int]($Text[3]."MB Free"))

Open in new window

To overcome the error shown in your last post, you need to make sure that the item being substituted is seen as a distinct, individual value.  Easiest way is to surround your statement doing division with quotes.
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
Leo TorresSQL DeveloperAuthor Commented:
Wow is that it a matter of parentheses placement. I don't know how long it would have taken me to get that rights.

Thank you yes it worked. I still don't understand how the array value is looked at differently based on parentheses outside of it.

Thanks again!
0
footechCommented:
Without the parentheses it's trying to take
"{0}`t`t {1}`t`t {2,8:d}`t`t {3,8:d}`t`t {4,8:n} `r`n" -f $Server, $Text[0].Drive.Replace("Space",""), $Text[0]."MB Free", $Text[3]."MB Free", ([int]($Text[0]."MB Free")
and then divide the result of that (which is a string), by the last bit
/ [int]($Text[3]."MB Free"))

Run these commands to get a clearer picture,
"{0} stuff" -f 100/20
"jlasdf" / 20

Open in new window


The parentheses let's it know to process ([int]($Text[0]."MB Free") / [int]($Text[3]."MB Free")) first, and then evaluate the rest of the line.
0
Leo TorresSQL DeveloperAuthor Commented:
I should know better . Please Excuse My Dear Aunt Sally Left Room.
0
footechCommented:
???
0
Leo TorresSQL DeveloperAuthor Commented:
Order of operation
0
footechCommented:
Ah, gotcha.  Never heard the mnemonic before.
0
Leo TorresSQL DeveloperAuthor Commented:
I was a college teacher that's why I should know better
0
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.