LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Powershell to start Labview vi from script

Solved!
Go to solution
Solution
Accepted by topic author Maarten1202

Okay... so I have managed to do it natively (sort of).

 

First of all - I've added the LabVIEW.exe folder to the system path variable, so I can use only LabVIEW to run the VI, not the whole C:\Program files\etc\etc\NI\LV\LabVIEW.exe thing.

 

First I construct the command:

$code = 'Start LabVIEW "' + $ENV:WORKSPACE + "MyProject\Build_All\BuildAllFromCommandLine.vi -- action=" + $LVaction + " path=" + $LVpath + " logpath=" + $logpath + '.txt"'

 

where the $ENV:WORKSPACE is environmental variable from Jenkins (this is carried out inside it). Please, look carefully at the apostrophes and quotation marks. The whole $code is an string, so it is surrounded by the apostrophes, and inside that the command that is send to LabVIEW is in quotation marks, so the full command in the $code looks like so:

Start LabVIEW "MyProject\Build_All\BuildAllFromCommandLine.vi -- action=do-something path=C:\something\something\project.lvproj logpath=C:\VeryImportantLog.txt"

 

Then you can just run it:

Invoke-Expression $code

So to sum up - the quotation marks and apostrophe use is the key here.

 

Unfortunatelly LV runs so, that it does not occupy the shell, so it immediately goes back to prompt, but you can monitor the LabVIEW process in the OS.

Message 11 of 13
(1,420 Views)

Thank you for your solution.

I've tested your code, and this is a much cleaner solution.

 

So you're also using it to automate builds from Jenkins. We are doing the same thing using the Autobuild.csv and Build.vi described int he CI forum: https://forums.ni.com/t5/Continuous-Integration/LabVIEW-Jenkins-Batch-File-Build-Process-Getting-Sta...

 

As before your solution we were using this piece of code to asynchronously start labview.

$Arglist=@($LV,$BuildVI,$ENV:BUILD_NUMBER,$errorfile,$runfile,$Workspace,$ENV:JOB_NAME,$mcfile,$buildlogfile)

$StartLVScript=
{
    Param($LV,$VI,$1,$2,$3,$4,$5,$6,$7)
    cmd /c "START "Labview" ""$LV"" ""$VI"" -- ""$1"" ""$2"" ""$3"" ""$4"" ""$5"" ""$6"" ""$7"" "
}
Start-Job -scriptblock $StartLVScript -Argumentlist $Arglist

Now i use this version of your solution.

$LVExecuteStr='"'+$BuildVI+'" -- "'+$ENV:BUILD_NUMBER+'" "'+$errorfile+'" "'+$runfile+'" "'+$Workspace+'" "'+$ENV:JOB_NAME+'" "'+$mcfile+'" "'+$buildlogfile+'"'
$CommandStr = 'Start '+' "$LV" '+'$LVExecuteStr'
Invoke-Expression $CommandStr

I had to use more quotes to prevent space problems in the file paths and arguments. but it is working now.

With this code you remarked that PS did not wait for Labview to finish. This was actually a needed part for me. because Labview and PS communicate trough a file on disk. You can see how this is done in the project described in the forum above.

 

I hope it may help you do some more things with Jenkins too.

If you need more help or info about how we use Jenkins let me know.

 

Also, if you have a cleaner solution for our quotes problem, your help is welcome

 

0 Kudos
Message 12 of 13
(1,388 Views)

Oh yes, the quotes are a pain in the neck for sure - I haven't found any better solution for that.

 

In my Jenkins PowerShell script I just wait till the LabVIEW process dies (or the build step timeouts):

 

while ((Get-Process LabVIEW -ErrorAction SilentlyContinue) -and ($timeoutSeconds -ge 0)) {
	Start-Sleep -s 1
	$timeoutSeconds = $timeoutSeconds - 1
}
echo ([string]$timeoutSeconds + " seconds of timeout left").
if (Test-Path ($logpath + ".err")) {
	$errorlevel = Get-Content -path ($logpath + ".err") -TotalCount 1
	Remove-Item ($logpath + ".err") -Force
	exit $errorlevel
} 
elseif ($timeoutSeconds -le 0) {
	Get-Process LabVIEW -ErrorAction SilentlyContinue | Stop-Process
	exit 666
}
else {
	exit 667
}

Where the *.err file is the output from the VI that is making the build. In the first line of this text file is only the error code from the operations inside the VI, so if it is anything other than 0 it fails the step and the build. 

0 Kudos
Message 13 of 13
(1,368 Views)