@@ -74,6 +74,31 @@ export const GrantTUNPermission = async (path: string) => {
7474 }
7575}
7676
77+ export const RunWithPowerShell = async (
78+ path : string ,
79+ args : string [ ] = [ ] ,
80+ options : { admin ?: boolean ; hidden ?: boolean ; wait ?: boolean } ,
81+ ) => {
82+ const { admin = false , hidden = false , wait = true , ...others } = options
83+ const psArgs : string [ ] = [ ]
84+ let command = `Start-Process -FilePath "${ path } "`
85+ if ( args . length > 0 ) {
86+ const argList = args . map ( ( a ) => `"${ a . replace ( / " / g, '""' ) } "` ) . join ( ',' )
87+ command += ` -ArgumentList ${ argList } `
88+ }
89+ if ( admin ) {
90+ command += ' -Verb RunAs'
91+ }
92+ if ( hidden ) {
93+ command += ' -WindowStyle Hidden'
94+ }
95+ if ( wait ) {
96+ command += ' -Wait'
97+ }
98+ psArgs . push ( '-NoProfile' , '-Command' , command )
99+ await Exec ( 'powershell' , psArgs , { Convert : true , ...others } )
100+ }
101+
77102// SystemProxy Helper
78103export const SetSystemProxy = async (
79104 enable : boolean ,
@@ -531,11 +556,16 @@ export const QuerySchTask = async (taskName: string) => {
531556}
532557
533558export const CreateSchTask = async ( taskName : string , xmlPath : string ) => {
534- await Exec ( 'SchTasks' , [ '/Create' , '/F' , '/TN' , taskName , '/XML' , xmlPath ] , { Convert : true } )
559+ const fn = useEnvStore ( ) . env . isPrivileged ? Exec : RunWithPowerShell
560+ await fn ( 'SchTasks' , [ '/Create' , '/F' , '/TN' , taskName , '/XML' , xmlPath ] , {
561+ admin : true ,
562+ hidden : true ,
563+ } )
535564}
536565
537566export const DeleteSchTask = async ( taskName : string ) => {
538- await Exec ( 'SchTasks' , [ '/Delete' , '/F' , '/TN' , taskName ] , { Convert : true } )
567+ const fn = useEnvStore ( ) . env . isPrivileged ? Exec : RunWithPowerShell
568+ await fn ( 'SchTasks' , [ '/Delete' , '/F' , '/TN' , taskName ] , { admin : true , hidden : true } )
539569}
540570
541571// Others
0 commit comments