Home > OS >  How to pass double quoted string to sc.exe for binPath
How to pass double quoted string to sc.exe for binPath

Time:01-21

I am trying to come up with a PowerShell process to quote service paths that contain spaces. This is because of Qualys 105484.

I know that the parsing engine is complicated and running cmd commands with embedded quotes can be difficult to say the least. I'm hoping I am close and another set of eyes will help.

the cmd command I am trying to run from PowerShell is

sc create OcaLogSvc binpath= "\"C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc\""

Here are some attempts and a comment below each showing the result that appears for the binPath

$serviceName = "OcaLogSvc"
& sc.exe config $serviceName binPath= '""\""C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc\""""'
# ""C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc""

& sc.exe config $serviceName binPath= '""\""C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc""\""'
# ""C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc""

& sc.exe config $serviceName binPath= '"""\"C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc\""""'
# ""C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc""

& sc.exe config $serviceName binPath= "\C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc\"
# \C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc"

Which I verify by running

sc.exe qc $serviceName | sls "BINARY"

Tried several other combinations but those above were the only ones that succeeded.

Likely I am going to give up and instead pipe some string into .bat files that I can call instead. That or edit the registry directly. I just feel that this is something possible.

I cannot use the stop parsing parameter as I intend to use variables for the path names as I discover them. I also am only mass targeting commands that do not have arguments. I will address those manually or at least in another fashion than what I am trying to do here.

I also tried to examine cmd strings with CommandLineToArgvW via Split-CommandLine but that knowledge hasn't translated well to the examples above.

CodePudding user response:

Seems your looking for:

'"C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc"' - which will result in a string "C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc"

Issue your trying to remediate only exists due to spaces in the command. Another option might be to use the old DOS name c:\progra~1\ or install you app to a folder that doesnt contain a space like C:\AppRecovery\Agent\R3LogService\OcaLogSvc

CodePudding user response:

Thanks for pointing out to the this document

I tried to modify a service in my machine using sc.exe command but failed to update it, although the command ran successfully! Then I thought that we could update the exe paths using registry keys

as you may now, the services details exist on registry under:

HKLM:\SYSTEM\CurrentControlSet\Services\$service

so in order to update your service with quotes you just need to update it in registry using the command Set-Itemproperty as follow:

$service = "OcaLogSvc"
Set-Itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\$service" -Name 'ImagePath' -value '"C:\Program Files\AppRecovery\Agent\R3LogService\OcaLogSvc"'
  •  Tags:  
  • Related