0

我试图在 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 脚本运行良好。有什么我想念的吗?

下载.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') 
4

1 回答 1

0

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

尝试

脚本

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 进行测试

下载文件.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
于 2021-02-05T16:36:27.580 回答