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.


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

Open in new window


At line:1 char:209
+ ... alBytes';e={ If "$($_.TotalBytes.Value.ToMB())MB"} Else {"$($_.TotalBytes.Value. ..
+                    ~
Missing '(' after 'If' in if statement.
At line:1 char:245
+ ... ue.ToMB())MB"} Else {"$($_.TotalBytes.Value.ToKB())KB"}}
+                    ~~~~
Unexpected token 'Else' in expression or statement.
At line:1 char:244
+ ... lue.ToMB())MB"} Else {"$($_.TotalBytes.Value.ToKB())KB"}}
+                    ~
The hash literal was incomplete.
At line:1 char:285
+ ... lue.ToKB())KB"}}
+                    ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement
techdriveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

oBdACommented:
First thing to do in a case like this is take a syntax highlighting editor (the PS ISE, Notepad++, whatever) and reformat the line.
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

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):
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.Value.ToMB() -gt 0) {
				"$($_.TotalBytes.Value.ToMB())MB"
			} Else {
				"$($_.TotalBytes.Value.ToKB())KB"
			}
		}},
		RecipientCount, RelatedRecipientAddress, Reference, MessageSubject, Sender, ReturnPath, MessageInfo

Open in new window

footechCommented:
@{n="TotalBytes";e={ If ($_.TotalBytes.Value.ToMB())MB"} Else {"$($_.TotalBytes.Value.ToKB())KB"}
What are you trying to do here?  What is the condition you're trying to check?
techdriveAuthor Commented:
thanks guys it works and thank you especially to OBDA
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

techdriveAuthor Commented:
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.
techdriveAuthor Commented:
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.ToKB())KB"}" when running message tracking. I would like to get my output to this below.

1K
24K
82K
1mb
3mb
892k
oBdACommented:
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

Open in new window

techdriveAuthor Commented:
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./
oBdACommented:
Sorry, typo form testing:
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]($_.TotalBytes / 1MB))MB"
			} Else {
				"$([int]($_.TotalBytes / 1KB))KB"
			}
		}},
		RecipientCount, RelatedRecipientAddress, Reference, MessageSubject, Sender, ReturnPath, MessageInfo

Open in new window

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