Link to home
Start Free TrialLog in
Avatar of techdrive
techdriveFlag for United States of America

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.


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
Avatar of oBdA
oBdA

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

@{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?
Avatar of techdrive

ASKER

thanks guys it works and thank you especially to OBDA
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.
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
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

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
Avatar of oBdA
oBdA

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