@@ -4,7 +4,7 @@ import { logger } from '../core/logger.js';
44
55export async function isServiceInstalled ( ) : Promise < boolean > {
66 try {
7- await runPs ( ` Get-Service -Name " ${ SERVICE_NAME } " -ErrorAction Stop` ) ;
7+ await runPs ( '& { Get-Service -Name $args[0] -ErrorAction Stop }' , [ SERVICE_NAME ] ) ;
88 return true ;
99 } catch {
1010 return false ;
@@ -13,7 +13,7 @@ export async function isServiceInstalled(): Promise<boolean> {
1313
1414export async function isServiceRunning ( ) : Promise < boolean > {
1515 try {
16- const status = await runPs ( ` (Get-Service -Name " ${ SERVICE_NAME } " ).Status` ) ;
16+ const status = await runPs ( '& { (Get-Service -Name $args[0] ).Status }' , [ SERVICE_NAME ] ) ;
1717 return status === 'Running' ;
1818 } catch {
1919 return false ;
@@ -54,8 +54,8 @@ export async function installService(instance: string, port: number = 5432, extr
5454
5555 const binPath = buildServiceBinPath ( SYSTEM_PATHS . PROXY_EXE , instance , port , extraArgs ) ;
5656
57- // Use New-Service with single-quoted BinaryPathName
58- await runPs ( ` New-Service -Name " ${ SERVICE_NAME } " -BinaryPathName ' ${ binPath } ' -StartupType Automatic` ) ;
57+ // Use New-Service with arguments to avoid injection
58+ await runPs ( '& { New-Service -Name $args[0] -BinaryPathName $args[1] -StartupType Automatic }' , [ SERVICE_NAME , binPath ] ) ;
5959}
6060
6161export async function updateServiceBinPath ( instance : string , port : number = 5432 , extraArgs : string [ ] = [ ] ) {
@@ -73,8 +73,8 @@ export async function updateServiceBinPath(instance: string, port: number = 5432
7373
7474 const binPath = buildServiceBinPath ( SYSTEM_PATHS . PROXY_EXE , instance , port , extraArgs ) ;
7575
76- // Use CIM/WMI to update service path
77- await runPs ( ` $svc = Get-CimInstance Win32_Service -Filter "Name=' ${ SERVICE_NAME } ' "; Invoke-CimMethod -InputObject $svc -MethodName Change -Arguments @{ PathName = ' ${ binPath } ' }` ) ;
76+ // Use CIM/WMI to update service path with arguments
77+ await runPs ( '& { $svc = Get-CimInstance Win32_Service -Filter "Name=$($args[0]) "; Invoke-CimMethod -InputObject $svc -MethodName Change -Arguments @{ PathName = $args[1] } }' , [ SERVICE_NAME , binPath ] ) ;
7878}
7979
8080export async function uninstallService ( ) {
@@ -90,23 +90,23 @@ export async function uninstallService() {
9090 await stopService ( ) ;
9191
9292 // Use sc.exe delete instead of Remove-Service for better compatibility
93- await runPs ( ` sc.exe delete " ${ SERVICE_NAME } "` ) ;
93+ await runPs ( '& { sc.exe delete $args[0] }' , [ SERVICE_NAME ] ) ;
9494}
9595
9696export async function startService ( ) {
9797 if ( ! await isAdmin ( ) ) {
9898 throw new Error ( 'Admin privileges required to start service.' ) ;
9999 }
100100 logger . info ( `Starting service ${ SERVICE_NAME } ...` ) ;
101- await runPs ( ` Start-Service -Name " ${ SERVICE_NAME } "` ) ;
101+ await runPs ( '& { Start-Service -Name $args[0] }' , [ SERVICE_NAME ] ) ;
102102}
103103
104104export async function stopService ( ) {
105105 if ( ! await isAdmin ( ) ) {
106106 throw new Error ( 'Admin privileges required to stop service.' ) ;
107107 }
108108 logger . info ( `Stopping service ${ SERVICE_NAME } ...` ) ;
109- await runPs ( ` Stop-Service -Name " ${ SERVICE_NAME } " -Force` ) ;
109+ await runPs ( '& { Stop-Service -Name $args[0] -Force }' , [ SERVICE_NAME ] ) ;
110110}
111111
112112export async function setServiceStartupType ( type : 'Automatic' | 'Manual' | 'Disabled' | 'Delayed' ) {
@@ -116,10 +116,10 @@ export async function setServiceStartupType(type: 'Automatic' | 'Manual' | 'Disa
116116
117117 if ( type === 'Delayed' ) {
118118 // Delayed start is a special case of Automatic
119- await runPs ( ` Set-Service -Name " ${ SERVICE_NAME } " -StartupType Automatic` ) ;
120- await runPs ( ` sc.exe config " ${ SERVICE_NAME } " start= delayed-auto` ) ;
119+ await runPs ( '& { Set-Service -Name $args[0] -StartupType Automatic }' , [ SERVICE_NAME ] ) ;
120+ await runPs ( '& { sc.exe config $args[0] start= delayed-auto }' , [ SERVICE_NAME ] ) ;
121121 } else {
122- await runPs ( ` Set-Service -Name " ${ SERVICE_NAME } " -StartupType ${ type } ` ) ;
122+ await runPs ( '& { Set-Service -Name $args[0] -StartupType $args[1] }' , [ SERVICE_NAME , type ] ) ;
123123 }
124124 logger . info ( `Service startup type set to ${ type } .` ) ;
125125}
0 commit comments