Search code examples
windowspowershellcommand-linecommandregistry

Powershell query not working inside Powershell -command


I spent more than 8 hours but still not able to identify where and what is reason that command alone on powershell is working fine but when you use inside Powershell -command it's failing. I want to do it in one line only.

Below is command which working on powershell terminal.

$valueData = Get-ItemPropertyValue -Path 'Registry::HKU\.DEFAULT\Software\Veeam\Veeam LT' -Name 'key';$timestamp=[Math]::Round((Get-Date).ToUniversalTime().Subtract((Get-Date '01/01/1970')).TotalSeconds);$exitValue = if ($valueData -ieq "e24040") {1}else{0};$outputString = 'veeam_backup_monitor_license_support_expiring,expected_value="e24040",current_value='+$valueData+' value='+$exitValue +$timestamp; Write-Output $outputString

But when I use same command like below it's throwing error

powershell -command "$valueData = Get-ItemPropertyValue -Path 'Registry::HKU\.DEFAULT\Software\Veeam\Veeam LT' -Name 'Monitors';$timestamp=[Math]::Round((Get-Date).ToUniversalTime().Subtract((Get-Date '01/01/1970')).TotalSeconds);$exitValue = if ($valueData -ieq "e24040") {1}else{0};$outputString = 'veeam_backup_monitor_license_support_expiring,expected_value="e24040",current_value='+$valueData+' value='+$exitValue +$timestamp; Write-Output $outputString"

I'm getting below errors.

At line:1 char:221
+ ... ract((Get-Date '01/01/1970')).TotalSeconds);0 = if (2345 -ieq  e24040 ...
+                                                                  ~
You must provide a value expression following the '-ieq' operator.
At line:1 char:223
+ ... t-Date '01/01/1970')).TotalSeconds);0 = if (2345 -ieq  e24040) {1}els ...
+                                                            ~~~~~~
Unexpected token 'e24040' in expression or statement.
At line:1 char:223
+ ... t-Date '01/01/1970')).TotalSeconds);0 = if (2345 -ieq  e24040) {1}els ...
+                                                            ~~~~~~
Missing closing ')' after expression in 'if' statement.
At line:1 char:229
+ ... -Date '01/01/1970')).TotalSeconds);0 = if (2345 -ieq  e24040) {1}else ...
+                                                                 ~
Unexpected token ')' in expression or statement.
At line:1 char:234
+ ... 1/01/1970')).TotalSeconds);0 = if (2345 -ieq  e24040) {1}else{0};veea ...
+                                                              ~~~~
Unexpected token 'else' in expression or statement.
At line:1 char:287
+ ... 40) {1}else{0};veeam_backup_monitor_license_support_expiring,expected ...
+                                                                 ~
Missing argument in parameter list.
At line:1 char:1
+ 2345 = Get-ItemPropertyValue -Path 'Registry::HKU\.DEFAULT\Software\V ...
+ ~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
At line:1 char:102
+ ... .DEFAULT\Software\Veeam\Veeam LT' -Name 'Monitors';1687879930=[Math]: ...
+                                                        ~~~~~~~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
At line:1 char:204
+ ... ersalTime().Subtract((Get-Date '01/01/1970')).TotalSeconds);0 = if (2 ...
+                                                                 ~
The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

Any reply is appreciated, Thank you in advance.


Solution

  • You double-quoted the -command "foo", but then you also double-quote this expected_value="e24040". CMD tends to eat quotes around parameters, but you can use triple-double-quotes to escape them like -c """foo""". powershell.exe will accept everything after -command as a single parameter, so it does not need to be quoted

    For example, this works for me from cmd:

    powershell.exe -c $valueData = Get-ItemPropertyValue -Path 'Registry::HKU\.DEFAULT\Software\Veeam\Veeam LT' -Name 'key';$timestamp=[Math]::Round((Get-Date).ToUniversalTime().Subtract((Get-Date '01/01/1970')).TotalSeconds);$exitValue = if ($valueData -ieq 'e24040') {1}else{0};$outputString = 'veeam_backup_monitor_license_support_expiring,expected_value="""e24040""",current_value='+$valueData+' value='+$exitValue +$timestamp;Write-Output $outputString