I have a small shell command i'm running to eventually get a date and time.

Get-WmiObject -ComputerName '.' -Class Win32_UserProfile -Filter "Special='False'" | select @{Name='LastUsed';Expression={$_.ConvertToDateTime($_.LastUseTime)}}| sort LastUsed -Descending | select-object -first 1 | format-table –hidetableheaders

It Returns:  7/25/2016 12:57:58 PM   
This command works just fine in PowerShell and as a Custom Inventory. 

But i needed to change the date to sort properly.  So i updated the command to this:

Get-WmiObject -ComputerName '.' -Class Win32_UserProfile -Filter "Special='False'" | select @{Name='LastUsed';Expression={$_.ConvertToDateTime($_.LastUseTime).TOSTRING("yyyy-MM-dd HH:mm:ss")}}| sort LastUsed -Descending | select-object -first 1 | format-table –hidetableheaders

It returns: 2016-07-25 12:57:58 

This one works just fine in Powerhsell, but fails as a Custom Inventory with this error:
'Missing ')' in method call.
At line:1 char:171
+ & {(Get-WmiObject -ComputerName '.' -Class Win32_UserProfile -Filter Special=
'False' | select @{Name='LastUsed';Expression={$_.ConvertToDateTime($_.LastUseT
ime).TOSTRING( <<<< yyyy-MM-dd HH:MM)}}| sort LastUsed -Descending | select-obj
ect -first 1 | format-table -hidetableheaders)}
   + CategoryInfo          : ParserError: (CloseParenToken:TokenId) [], Paren
  tContainsErrorRecordException
   + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

'

Any ideas on how to get around it?    It seems to have to do with the double parenthesis inside double parenthesis.  But i can't figure out how to get the command written to work in the Custom Inventory.

Here is the actual Custom inventory text for:

The one that works:

ShellCommandTextReturn(powershell.exe -NoLogo -NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -Command "& {(Get-WmiObject -ComputerName '.' -Class Win32_UserProfile -Filter "Special='False'" | select @{Name='LastUsed';Expression={$_.ConvertToDateTime($_.LastUseTime)}}| sort LastUsed -Descending | select-object -first 1 | format-table –hidetableheaders)})
That one that doesn't work:
ShellCommandTextReturn(powershell.exe -NoLogo -NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -Command "& {(Get-WmiObject -ComputerName '.' -Class Win32_UserProfile -Filter "Special='False'" | select @{Name='LastUsed';Expression={$_.ConvertToDateTime($_.LastUseTime).TOSTRING("yyyy-MM-dd HH:mm:ss")}}| sort LastUsed -Descending | select-object -first 1 | format-table –hidetableheaders)})

1 Comment   [ + ] Show Comment

Comments

  • What happens if you change the double quotes to single quotes in TOSTRING("yyyy-MM-dd HH:mm:ss")

    TOSTRING('yyyy-MM-dd HH:mm:ss')
Please log in to comment

There are no answers at this time

Answers

Answer this question or Comment on this question for clarity
Admin Script Editor
Admin Script Editor is an integrated scripting environment available free here at ITNinja

Share