Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

vbscript - Build path and use in external command

How to insert my SCRIPT_PATH Variable into PowerShell script path? Also anyone know any Visual Studio Like tool I can write those? I think Visual Studio uses different .net vbs.

Here is the code:

SCRIPT_PATH = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - (Len(WScript.ScriptName)))
MsgBox SCRIPT_PATH
Set objShell = CreateObject("WScript.Shell")
objShell.Run "RUNAS  /user:DomainUser ""powershell SCRIPT_PATH & Delete_App_Script.ps1"""
Set objShell = Nothing

How do I do SCRIPT_PATH = SCRIPT_PATH + Delete_App_Script.ps1? SCRIPT_PATH = SCRIPT_PATH & "Delete_App_Script.ps1" doesn't work. It gives no errors, so I am not sure what is the problem. Am I missing some "" or some ,,?

I've seen some .Run ,,, syntax for Admin running and this one, doesn't explain what goes into "". Not looking forward creating interface for this.

This doesn't work:

Set objShell = CreateObject("WScript.Shell")
SCRIPT_PATH = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - (Len(WScript.ScriptName)))
SCRIPT_PATH = SCRIPT_PATH & "Delete_App_Script.ps1"    
objShell.Run "RUNAS  /user:Domadm ""powershell SCRIPT_PATH"""
Set objShell = Nothing

But this works:

Set objShell = CreateObject("WScript.Shell")
objShell.Run "RUNAS  /user:Domadm ""powershell C:Delete_App_Script.ps1"""
Set objShell = Nothing

What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the appropriate FileSystemObject methods when handling paths:

Set fso = CreateObject("Scripting.FileSystemObject")

SCRIPT_PATH = fso.GetParentFolderName(WScript.ScriptFullName)
SCRIPT_PATH = fso.BuildPath(SCRIPT_PATH, "Delete_App_Script.ps1")

For using variables inside strings you need to concatenate the variables to the rest of the string, as @Lankymart pointed out. This is also documented in the language tag info.

objShell.Run "RUNAS /user:DomainUser ""powershell " & SCRIPT_PATH & """"

Note also that you should put the path in double quotes in case it contains spaces:

objShell.Run "RUNAS /user:DomainUser ""powershell """ & SCRIPT_PATH & """"""

and you should use the parameter -File, otherwise PowerShell would interpret the script path as a command string, which wouldn't fail for paths with spaces.

objShell.Run "RUNAS /user:DomainUser ""powershell -File """ & SCRIPT_PATH & """"""

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...