构建路径并在外部命令中使用 [英] Build path and use in external command
问题描述
如何将我的 SCRIPT_PATH
变量插入到 PowerShell 脚本路径中?也有人知道我可以编写的任何类似 Visual Studio 的工具吗?我认为 Visual Studio 使用不同的 .net vbs.
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.
代码如下:
SCRIPT_PATH = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - (Len(WScript.ScriptName)))
MsgBox SCRIPT_PATH
Set objShell = CreateObject("WScript.Shell")
objShell.Run "RUNAS /user:Domain\User ""powershell SCRIPT_PATH & Delete_App_Script.ps1"""
Set objShell = Nothing
我该怎么做SCRIPT_PATH = SCRIPT_PATH + Delete_App_Script.ps1
?SCRIPT_PATH = SCRIPT_PATH &Delete_App_Script.ps1"
不起作用.它没有给出任何错误,所以我不确定是什么问题.我是否遗漏了一些 ""
或一些 ,,,
?
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 ,,
?
我见过一些 .Run ,,,
管理员运行的语法,而这个语法并没有解释 ""
的内容.不期待为此创建界面.
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.
这不起作用:
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:Dom\adm ""powershell SCRIPT_PATH"""
Set objShell = Nothing
但这有效:
Set objShell = CreateObject("WScript.Shell")
objShell.Run "RUNAS /user:Dom\adm ""powershell C:\Delete_App_Script.ps1"""
Set objShell = Nothing
我错过了什么?
推荐答案
使用适当的FileSystemObject
处理路径时的方法:
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")
要在字符串中使用变量,您需要将变量连接到字符串的其余部分,如 @Lankymart 指出.这也记录在 语言标签信息中.
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:Domain\User ""powershell " & SCRIPT_PATH & """"
还要注意,如果路径包含空格,您应该用双引号括起来:
Note also that you should put the path in double quotes in case it contains spaces:
objShell.Run "RUNAS /user:Domain\User ""powershell \""" & SCRIPT_PATH & "\"""""
并且您应该使用参数 -File
,否则 PowerShell 会将脚本路径解释为命令字符串,对于带有空格的路径不会失败.
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:Domain\User ""powershell -File \""" & SCRIPT_PATH & "\"""""
这篇关于构建路径并在外部命令中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!