Link to home
Start Free TrialLog in
Avatar of Leo Torres
Leo TorresFlag for United States of America

asked on

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

Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

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

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.
Avatar of Leo Torres

ASKER

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

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

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.
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

ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
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.
I should know better . Please Excuse My Dear Aunt Sally Left Room.
???
Order of operation
Ah, gotcha.  Never heard the mnemonic before.
I was a college teacher that's why I should know better