techdrive
asked on
powershell if statement
Good day guys wanted help with an issue. I am trying to write an if statement on the command line on the totalbytes and this is failing. I have tried to do this several ways and all failing please help. I have the latest error message below.
At line:1 char:209
+ ... alBytes';e={ If "$($_.TotalBytes.Value.ToM B())MB"} Else {"$($_.TotalBytes.Value. ..
+ ~
Missing '(' after 'If' in if statement.
At line:1 char:245
+ ... ue.ToMB())MB"} Else {"$($_.TotalBytes.Value.To KB())KB"}}
+ ~~~~
Unexpected token 'Else' in expression or statement.
At line:1 char:244
+ ... lue.ToMB())MB"} Else {"$($_.TotalBytes.Value.To KB())KB"}}
+ ~
The hash literal was incomplete.
At line:1 char:285
+ ... lue.ToKB())KB"}}
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE xception
+ FullyQualifiedErrorId : MissingOpenParenthesisInIf Statement
Get-TransportService | get-messagetrackinglog -ResultSize:unlimited -recipient someuser@company.com -Start "5/25/2015 3:00:00 AM"-End "6/9/2015 7:30:00 PM" | select timestamp, @{n="TotalBytes";e={ If ($_.TotalBytes.Value.ToMB())MB"} Else {"$($_.TotalBytes.Value.ToKB())KB"}, RecipientCount, RelatedRecipientAddress, Reference, MessageSubject, Sender, ReturnPath, MessageInfo
At line:1 char:209
+ ... alBytes';e={ If "$($_.TotalBytes.Value.ToM
+ ~
Missing '(' after 'If' in if statement.
At line:1 char:245
+ ... ue.ToMB())MB"} Else {"$($_.TotalBytes.Value.To
+ ~~~~
Unexpected token 'Else' in expression or statement.
At line:1 char:244
+ ... lue.ToMB())MB"} Else {"$($_.TotalBytes.Value.To
+ ~
The hash literal was incomplete.
At line:1 char:285
+ ... lue.ToKB())KB"}}
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE
+ FullyQualifiedErrorId : MissingOpenParenthesisInIf
@{n="TotalBytes";e={ If ($_.TotalBytes.Value.ToMB( ))MB"} Else {"$($_.TotalBytes.Value.To KB())KB"}
What are you trying to do here? What is the condition you're trying to check?
What are you trying to do here? What is the condition you're trying to check?
ASKER
thanks guys it works and thank you especially to OBDA
ASKER
uh oh nothing from the totalbytes field footech I am trying to take a number from the command output totalbytes and convert that number to kb if the number becomes too large for kilobytes then make any number bigger than kb and conver it to a MB number.
ASKER
By using command below I am trying to get output as such from a number "@{n="TotalBytes";e={ If ($_.TotalBytes.Value.ToMB( ))MB"} Else {"$($_.TotalBytes.Value.To KB())KB"}" when running message tracking. I would like to get my output to this below.
1K
24K
82K
1mb
3mb
892k
1K
24K
82K
1mb
3mb
892k
Try this then:
Get-TransportService |
get-messagetrackinglog -ResultSize:unlimited -recipient someuser@company.com -Start "5/25/2015 3:00:00 AM" -End "6/9/2015 7:30:00 PM" |
Select-Object -Property `
Timestamp,
@{n="TotalBytes"; e={
If ($_.TotalBytes -gt 1024 * 1024) {
"$([int]($a.TotalBytes / 1MB))MB"
} Else {
"$([int]($a.TotalBytes / 1KB))KB"
}
}},
RecipientCount, RelatedRecipientAddress, Reference, MessageSubject, Sender, ReturnPath, MessageInfo
ASKER
ok OBDA the code you sent only put 0 next to the k. I have verified and have emails in MB and KB and this is not measuring them and thanks for your help./
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
- Line 6: There is no real expression in the If clause; what's in there seems to be the desired output.- Line 6: A closing double quote without a matching opening double quote (so now an opening double quote)
- Lines 8/9: The calculated property is missing two closing curly brackets.
No idea what sort of "if" expression you had in mind; this should be syntactically correct (can't test it):
Open in new window