gangstanthony / PowerShell

My PS Scripts
MIT License
337 stars 95 forks source link

error with Psexec #6

Open theikong opened 3 years ago

theikong commented 3 years ago

Hello Everyone ! please help ! i use this Get-ChildItem -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ | Get-ItemProperty | Where-Object {$.Displayname -match 'WinRAR'} | Select-Object DisplayName,DisplayVersion it worked but that i try it with Psexec & C:\PsISE\GetServiceStatus\PsExec.exe -nobanner \$comp cmd /c "powershell.exe Get-ChildItem -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ | Get-ItemProperty | Where-Object {$.Displayname -match 'WinRAR'} | Select-Object DisplayName,DisplayVersion
why it error ?? sorry for bad english

MattPedersen commented 8 months ago

I realize this is an old post. However, I am answering in case anyone else runs into a similar issue.

The problem with the Script above is that the Command Line Interpreter (CMD.EXE) doesn't know to Parse the PowerShell Commands.

Therefore, if anyone comes across this issue, when attempting to Run a PowerShell Command, through Command Line (regardless of whether it involves PSEXEC or Not), I start with reading the following Article, which goes over the process of Escaping Special Characters, in regard to the Command Line.

https://ss64.com/nt/syntax-esc.html

Alternatively, a you may have better results, if you simply Copy the PowerShell Commands/Scripts, to a separate PS1 File and Call the Script from the PowerShell.exe Command, via the Command Line Interpreter or via PSEXEC, as follows.

Get-WinRarDisolayName.ps1:

Get-ChildItem -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ | Get-ItemProperty | Where-Object {$.Displayname -match 'WinRAR'} | Select-Object DisplayName,DisplayVersion

CMD.EXE Command (Without PSEXEC):

PowerShell.exe -ExecutionPolicy Bypass -File "C:\Path\To\Get-WinRarDisplayName.ps1"

PSEXEC Command:

C:\Path\To\PsExec.exe -I -s cmd /c powershell.exe -ExecutionPolicy Bypass -File "C:\Path\To\Get-WinRarDisplayName.ps1"

Feel free to reach-out with questions.