I am trying to use build-->windows power-shell option in Jenkins job and calling the following code:
cd C:\Users\username\Desktop\
cscript create.vbs
My create.vbs has the following code:
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
'Remove the following line to open Excel in the background
xlApp.Visible = true
Set xlBook = xlApp.Workbooks.Open("C:\Users\username\Desktop\excelname.xlsm",0, True)
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit
But I am getting the following error in console of failed job in jenkins:
C:\Users\username\Desktop\create.vbs(8, 1)
Microsoft Excel: Microsoft Excel cannot access the file
'C:\Users\username\Desktop\excelname.xlsm'.
There are several possible reasons:
The file name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a currently open workbook.
My create.vbs and excel sheet are at the same location, none of the excel workbook is opened and I am able to run successfully cscript create.vbs through Windows PowerShell directly.
Launch a slave in cmd and label the jenins job to run on this new slave.It works! the issue was because jenkins was running as a service and was unable to access excel being a desktop application.
Related
I have a .exe file which when called from the Windows Command prompt, first connects to a server and then requests for user input as a confirmation.
Something like below:
C:\SomePath\AnotherFolder>TheApplication.exe download
Logging in to Vault server myserver.app.cosmos...
Logged in to the Vault server
Downloading will overwrite existing files.
Download scripts for context 'COSMOS' to folder:
C:\Work\MyFolder\TEST?
(y/n): n <--- n was my input
Press any key to exit... <--- here i pressed y
I want to automate this process.
I tried the below line from the Windows command prompt:
echo n|TheApplication.exe download
I even tried :
(echo n && echo y)|TheApplication.exe download <-- the y for the last user input request to close the app.
But in both cases its throwing the below error:
Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
at System.Console.ReadKey(Boolean intercept)
at System.Console.ReadKey()
at TheApplication.Program.Main(String[] args)
**disclaimer: TheApplication.exe is a third party executable. I cannot change anything inside it.
PS: If needed I can run this from a powershell console if its not possible from windows command prompt or I can also include the whole thing inside a batch script.
But any how I need to automate this.
If you can use PowerShell...
autoinput.ps1
Add-Type -AssemblyName System.Windows.Forms
Start-Sleep -m 1000
[System.Windows.Forms.SendKeys]::SendWait("ny")
The command is:
powershell .\autoinput.ps1 & .\TheApplication.exe
I have created a small program in Visual Studio - a form with a button to execute the program when clicked - and built the .exe.
This program is supposed to open PowerShell and run a ps1 file.
When I run it from within VS in debug mode, it works as expected running the ps1 script without error.
When I build the project and run the exe as an administrator, it briefly opens PowerShell but closes immediately.
The output file states that the execution of scripts is disabled on this system, although this is incorrect as I can run any number of PowerShell scripts manually by right clicking them and running them with PowerShell.
I have tried changing the execution policy from Signed, to AllSigned, as well as Unrestricted, but it did not help.
Here is my code:
if (File.Exists("LiveSiteTests.ps1"))
{
File.GetAttributes("LiveSiteTests.ps1");
string strCmdText = Path.Combine(Directory.GetCurrentDirectory(), "LiveSiteTests.ps1");
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = #"C:\windows\system32\windowspowershell\v1.0\powershell.exe";
process.StartInfo.Arguments = "\"&'" + strCmdText + "'\"";
process.Start();
string s = process.StandardOutput.ReadToEnd();
process.WaitForExit();
using (StreamWriter outfile = new StreamWriter("StandardOutput.txt", true))
{
outfile.Write(s);
}
}
I am trying to create a PS script that would read an xml file (server name list) & then run click once app on each of these servers. In my case ,Power shell script execution will be scheduled via Task scheduler.
Now the problem is : I can't find a way to run click once app via Power shell script
Any ideas?
I run app ref files by running it through the explorer so an example would be
$fileLocation="C:\temp\program.appref-ms"
explorer.exe $fileLocation
This article documents a process to getting the file path to run the ClickOnce app
https://robindotnet.wordpress.com/2010/03/21/how-to-pass-arguments-to-an-offline-clickonce-application/
If you can translate this C#/NET code into PowerShell, it should work
StringBuilder sb = new StringBuilder();
sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.Programs));
sb.Append("\\Nightbird"); //ClickOnce Publisher name (eg Nightbird)
sb.Append("\\TestRunningWithArgs.appref-ms "); //ClickOnce Product name (eg TestRunningWithArgs)
string shortcutPath = sb.ToString();
System.Diagnostics.Process.Start(shortcutPath, argsToPass);
You can run a ClickOnce application by doing a start process on iexplore.exe and passing it the URL to the ClickOnce deployment. Can you do that in PowerShell? If so, then it should work.
I have a very simple VBScript file that interacts with iTunes via a COM object:
If I double-click on the .vbs file, it works fine. But if I run it from the command line, it fails:
c:\windows\SysWOW64\wscript myscript.vbs
iTunes opens, but the commands don't work, and after a short delay I get this:
ActiveX component can't create object: 'iTunes.Application'
Code: 800A01AD
The problem remains when I use cscript, and when I use the system32 version.
So, two questions:
Why is the behaviour different when double-clicking/running from the CLI?
How do I fix it, so it runs from the CLI, too?
Edited to add script:
Dim oiTunes, oTracks, oAdd
Set oiTunes = CreateObject("iTunes.Application")
Set oTracks = oiTunes.LibraryPlaylist
Set oAdd = oTracks.AddFile("D:\Users\Mark\Music\Downloaded iPlayer\Temp\temp.mp4")
Do : Loop While oAdd.InProgress = True
Set oAdd = Nothing
Set oTracks = Nothing
Set oiTunes = Nothing
The script you have shared works fine for me. Perhaps one of the CSLIDs has become corrupt - try reinstalling or repairing iTunes.
I'm trying to display messages as my vbscript program runs. It runs off a command prompt in xp, for example: cscript.exe test.vbs. I don't want to use msgBox while this is running as I just want it to post the scripts progress, but I don't want any user interaction.
I tried using Wscript.echo "Some text", but I'm getting compile errors when I step through the program using Words built-in vbeditor.
I found this code and it runs fine in another file:
Option Explicit
Dim strComputer
strComputer = "LocalHost"
WScript.Echo "Computer: " _
& strComputer
WScript.Quit
I then tried using Dim and set to setup a Wscript variable, but that didn't work either.
Any ideas as to what I'm doing wrong? I did verify Wscript is running on this machine.
Thanks,
James
Word uses VBA (Visual Basic for Applications), not VBScript. Although both languages belong to the Visual Basic family, they have differences. One of them is that the WScript object isn't available in VBA - that's why you're getting errors when debugging your script in Word.
Having said that, your code is valid and runs perfectly fine with both cscript and wscript.