【问题标题】:How to call a powershell script in vbscript?如何在 vbscript 中调用 powershell 脚本?
【发布时间】:2021-02-05 09:56:29
【问题描述】:

我正在尝试在 Visual Build 中的一个项目步骤之间调用一个 powershell 脚本。 我在可视化构建中创建了一个新脚本,并调用了我的 powershell 脚本。 我选择了 vbscript,这是脚本中的代码:

Dim paths, source1
paths = "C:\Dev\"
source1 = "\\10.156.3.14\win"

sCmd = "powershell.exe -noexit  -ExecutionPolicy Unrestricted -file C:\Dev\downloadfiles.ps1 -target """ & paths & """ -source """ & source1 & """"
Set xShell = CreateObject("Wscript.Shell")
rReturn = xShell.Run(sCmd, 0, true)

脚本使我的构建超时。

通过控制台运行时,powershell 脚本运行良好。 我有什么遗漏吗?

download.files.ps1 参数:

param (
    [string[]]$target,
    [string[]]$source
)

此外,还有什么方法可以让我在控制台运行时看到它的输出。即使使用“-noexit”,我什么也看不到。

更新 -- 脚本的第一部分运行并返回一些相关文件。 虽然接受参数的部分不起作用。

这似乎是更好的选择,因为脚本现在正在接受参数:

Set objShell = CreateObject("Wscript.Shell") 
objShell.run("powershell.exe -noexit -ExecutionPolicy Unrestricted -file .\downloadfiles.ps1 -target """ & paths & """ -source """ & source1 & """")

跟进问题。我将如何通过 vbscript 调用传入一个字符串数组? 例如

$target = @('c:\dev','d:\lib') 

【问题讨论】:

  • PowerShell 将以逗号分隔的单词列表解释为参数上下文中的字符串数组:-target 'c:\dev','c:\lib'
  • 我在 vbscript paths = "'C:\Dev\', 'C:\new\'" Set objShell = CreateObject("Wscript.Shell") objShell.run("powershell.exe -noexit -ExecutionPolicy Unrestricted -file .\downloadfiles.ps1 -target """ & paths & """ -source """ & source1 & """") 中尝试过,但在 powershell 中的测试路径检查失败
  • 我们还没有收到您的来信.. 我的回答解决了您的问题吗?如果是这样,请通过单击左侧的大复选标记图标 考虑accepting。这将帮助其他有类似问题的人更轻松地找到它。

标签: powershell vbscript visual-build-professional


【解决方案1】:

使用-File时,恐怕$target中的所有数组元素都会作为单个字符串转发给PowerShell脚本。

试试

VbScript

Option Explicit

'I hate writing """something""" or Chr(34) & "something" & Chr(34)
'all the time, so I use this little helper function
Private Function Quote(text)
    Quote = Chr(34) & text & Chr(34)
End Function

Dim paths, source1, script, sCmd, objShell
'join the quoted path elements with a semi-colon
paths   = Join(Array(Quote("C:\Dev"), Quote("D:\lib")), ";")
source1 = Quote("\\10.156.3.14\win")
script  = Quote("D:\Test\downloadfiles.ps1")

sCmd = "powershell.exe -NoExit -NoLogo -ExecutionPolicy Unrestricted -File " & script & " -target " & paths & " -source " & source1

Set objShell = CreateObject("Wscript.Shell")
objShell.run(sCmd)

使用 PowerShell 进行测试

downloadfiles.ps1

param (
    [string[]]$target,
    [string[]]$source
)
$target = $target | ForEach-Object { $_ -split ';' }
$source = $source | ForEach-Object { $_ -split ';' }

Write-Host "Elements in target: $($target.Count)"
Write-Host "Elements in source: $($source.Count)"
Write-Host
for ($i = 0; $i -lt $target.Count; $i++) {
    '$target[{0}]: {1}' -f $i, $target[$i]
}
Write-Host
for ($i = 0; $i -lt $source.Count; $i++) {
    '$source[{0}]: {1}' -f $i, $source[$i]
}

PowerShell 控制台中的输出:

Elements in target: 2
Elements in source: 1

$target[0]: C:\Dev
$target[1]: D:\lib

$source[0]: \\10.156.3.14\win

【讨论】:

    猜你喜欢
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    相关资源
    最近更新 更多