Leo Torres
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.
Error Message
$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 += @"
"@
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
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.
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.
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
ASKER
I dont understand why its says
Should be more like
$Text[5]."MB Free" = Servername T 25388 25388
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.
The error is a bit misleading.
Look at this example to illustrate.
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.
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
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.
ASKER
SO whats your solution?
Code
Error
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 += @"
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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("Sp ace",""), $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,
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}`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("Sp
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
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.
ASKER
I should know better . Please Excuse My Dear Aunt Sally Left Room.
???
ASKER
Order of operation
Ah, gotcha. Never heard the mnemonic before.
ASKER
I was a college teacher that's why I should know better
[int] ($($Text[0]."MB Free")) / [int]($($Text[3]."MB Free"))
ie
Open in new window