0

I'm trying to call a powershell script in-between one of my project steps in Visual Build. I've created a new script within visual build and having that call my powershell script. I selected vbscript and this is the code within the script:

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)

The script timeouts my build.

The powershell script works fine when ran through the console. Is there something I'm missing?

download.files.ps1 paramaters:

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

Additionally is there any way I could see the console output whilst it's running. Even with "-noexit" I'm seeing nothing.

Update -- The first part of the script runs and returns some of the relevant files. Although the part that takes in the parameters are not functioning.

This seems to be the better alternative as the script is now taking in parameters :

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

Follow up question. How would I go about passing in a string arrray through the vbscript call? e.g

$target = @('c:\dev','d:\lib') 
Mango99
  • 13
  • 3
  • PowerShell will interpret a comma-separated list of words as a string array in argument context: `-target 'c:\dev','c:\lib'` – Mathias R. Jessen Feb 05 '21 at 12:28
  • I tried that in 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 & """")` which failed the test-path check in powershell – Mango99 Feb 05 '21 at 12:38

1 Answers1

0

When using -File, I'm afraid all array elements in $target will be forwarded to the PowerShell script as a single string.

Try

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)

Test with 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]
}

Output in PowerShell console:

Elements in target: 2
Elements in source: 1

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

$source[0]: \\10.156.3.14\win
Theo
  • 57,719
  • 8
  • 24
  • 41