Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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 & """"""

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...