-2

I'm actually working on a "Computer Info Dumper" and I need to get the ram name + capacity, the problem is that the line that make this: it works in CMD, but not in a .bat file.

Here is the 'command': powershell -command "GWmi CIM_PhysicalMemory|%{\"\"+ ([int]$_.banklabel.Replace('BANK ','') + 1) + \") \" + ($_.capacity/1GB)+\"GB from \"+($_.manufacturer)}"

In CMD, it gives me the wanted result:

8GB from Corsair
4GB from Kingston
8GB from Corsair
4GB from Kingston

But in a .bat file, it gives me this error: (it will be translated because it's in French so the translation won't be perfect)

To character Line:1 : 25

+ ... sicalMemory|{""+ ([int]$_.banklabel.Replace('BANK ','') + 1) + ") " + ...

Expressions are only allowed as the first element of a pipeline.

+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException

+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

Can anyone help me on that?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Dazd-PKZ
  • 27
  • 5
  • Why are you running this from CMD and not directly from PowerShell? And, if having a `.bat` is needed for some reason I cannot understand, why not call from you batch file to `powershell.exe -File "path/to/script.ps1"` or encode the expression to base64 and call `powershell.exe -EncodedCommand 'b64encodedstring'` ? – Santiago Squarzon Mar 29 '22 at 21:21
  • @SantiagoSquarzon it's a entire code, not only for ram, it's for all the info of the computer, and i don't wan't external file / code, everything in my file ^^ – Dazd-PKZ Mar 29 '22 at 21:22
  • The question to why not encode all to base64 still stands and the question to why not run this directly with powershell still stands. – Santiago Squarzon Mar 29 '22 at 21:23
  • for the base64: i wan't the code to be readable because i think that i gonna put it on my github, and for the powershell part, i wan't it to be in batch, it's all, i'm too bad in powershell to make a entire program in it :D – Dazd-PKZ Mar 29 '22 at 21:26
  • 1
    Your code is PowerShell code tho, and having escaped PowerShell code in your batch file is not "readable" – Santiago Squarzon Mar 29 '22 at 21:29
  • @SantiagoSquarzon if i give you the entire code, i will be more understandable i think – Dazd-PKZ Mar 29 '22 at 21:30
  • @SantiagoSquarzon https://pastebin.com/raw/MWdJ5Ctu – Dazd-PKZ Mar 29 '22 at 21:33
  • This can be translated to PowerShell very easily, I can show you how it looks – Santiago Squarzon Mar 29 '22 at 21:41
  • what can be translated ?? it's already in powershell x) – Dazd-PKZ Mar 29 '22 at 21:48
  • All batch code from your pastebin I meant – Santiago Squarzon Mar 29 '22 at 21:51
  • 3
    @Dazd-PKZ, just escape the special character, `%` becomes `%%` in a batch file! – Compo Mar 29 '22 at 21:56
  • @Compo thanks you ! make an answer if you want me to +rep you :D – Dazd-PKZ Mar 29 '22 at 22:03

1 Answers1

1

As requested, my comment as an answer:

In a the % character has a special meaning, so a literal character would require escaping. The escape character for a percent is another, i.e. %%.

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -Command "Get-CimInstance CIM_PhysicalMemory | %% { \"\" + ([Int]$_.BankLabel.Replace('BANK ','') + 1) + \") \" + ($_.Capacity / 1GB) + \"GiB from \" + ($_.Manufacturer) }"

Or preferably, don't use the % alias for ForEach-Object in a , and then you can match your Command Prompt code withn your script.

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -Command "Get-CimInstance CIM_PhysicalMemory | ForEach-Object { \"\" + ([Int]$_.BankLabel.Replace('BANK ','') + 1) + \") \" + ($_.Capacity / 1GB) + \"GiB from \" + ($_.Manufacturer) }"

You will see that I have more correctly allocated the Capacity units as GiB, and that I have replaced your GWmi alias for Get-WmiObject, with the recommended PowerShell v3.0+ alternative Get-CimInstance, (alias gcim).

Compo
  • 36,585
  • 5
  • 27
  • 39