Include PowerShell Script in Excel - excel

Hoping someone here has a suggestion. I'm a network engineer, who has done a fair amount of programming/scripting over the years, but I'm not a programmer.
My current problem is, I have created a form in Excel to collect data for a particular type of install. I have also used the data entered into the form to create configurations for various network devices involved in this install. These configs are simple text which you can copy and paste, into a telnet or ssh session.
I would like to have a way to click a button in my excel form and push the config to the device automatically, so the user doesn't have to copy and paste it to the device. The sheet already includes a few buttons tied to VBScript macros which perform various functions, including dumping the configs to files. However, as VBScript lacks a native way to open a session (telnet) to a device to send/receive text, I am looking for a way to use a PowerShell script to push the configs.
I have already written a PowerShell Script to push a file containing a text config to a device via telnet, and capture any response to a log file, but I can't find a way to include the PowerShell script as a macro in Excel like you can with a VBScript in MS Visual Basic for Applications.
I've trying to avoid having to create a .MSI or .zip bundle that would include the Excel file and a PowerShell script (or writing something in a compiled .exe). The end goal of this is to have a tool that anyone with Excel running on Windows can fill in, and click a few buttons to send the configurations (text) to a device via telnet, without installing anything on their PC. This tool would be for other network engineers who know far less than me (not that I know much) about scripting or Excel.

Sub RunAndGetCmd()
strCommand = "Powershell -File ""C:\path\to\My PS File.ps1"""
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
strOutput = WshShellExec.StdOut.ReadAll
Msgbox strOutput
End Sub

Related

Using WScript with MS Edge to download multiple files leaves open Edge tabs

I am using a Wscript shell to automate the download of multiple (Excel files). The shell command creates a new tab (and popup window) in MS Edge for every download. What I would like is to create a new instance of Edge, and close it when I have finished.
The set-up: I have a third-party company that emails me links with which to download Excel files. If I click on the link in the email, a file is downloaded as a Protected View in Excel. The Excel file is produced by the server on the fly.
To automate this, I have a VBA routine that cycles through a list of these links (which I have separately extracted from my email), and I am using a Wscript shell object to drive Edge.
Code extract:
Dim wsh As New WshShell
Dim strCmd As String
strCmd = "microsoft-edge:"
Dim strLink As String
strLink = "https:\\somesite\\someserverprocess"
wsh.Run strCmd & strLink, WaitOnReturn:=True
This code does the job of downloading the file into Excel, but leaves a new tab and a popup window in Edge for each file. Since I will be looping this over multiple files, I will have a lot of tabs and popups left at the end. I am hoping to create a process I can leave to run unattended, so really need to tidy up once this process is finished.
I have read several other questions on this topic on SO (in particular the lack of a COM automation interface for Edge), but without finding an answer. I am open to suggestions. While I am not wedded to Wscript as the method, my preference is to run this process from Excel VBA (as I have to process the actual Excel files as a second step).
This answer might help
Downloading a file in VBA and storing it

How to call macros using azure api

I have an excel file containing around 100K rows in onedrive. This file contains some macros. I want to run these macros using api.I want to pass these macro functions as parameters.
For example
https://graph.microsoft.com/beta/me/drive/root:/book1.xlsx:/workbook/tables/{id|name}
Here, I want to call macros function in place tables/{id|name}
Any way to do this?
Thanks
To be able to run an Excel macro, you need a running Excel instance that opens the file containing the macro and runs the code. There is no way around this I'm afraid.
So you need a machine - physical or virtual - where Excel is installed. Then you can have a script open the Excel file there and run the macros.
If you are working in a cloud environment or on a web server for example, you would be better off rewriting the code in another language, independent of Excel. (Trust me, I've been there, done that.)

How to create excel file by copying CSV files and running VBA macros remotely from Linux

I'm running some task on Linux which produces certain CSVs. Once they’re produced, I need to copy them into different sheets of an excel file and then run some VBA macros to generate a report. Since this has to be done multiple times a day (with no pre-defined/fixed schedule), I’d like to automate it.
Here is what I tried:
I wrote a VBScript, which takes the CSV files and other required input files, opens an excel file, copies the CSVs into the excel, runs some VBA macros, and saves and closes the result. This VBScript works fine when triggered locally on a windows server.
I have uploaded a simpler version of the files involved here which somewhat clarify my requirements (Note that the macros I need to run are much more complicated (~500 lines) compared with the macros here).
There are four files inside the archive:
Template.xlsm: The Excel template of the report into which data is to be pasted
Input_Sheet2.csv : The input data to be pasted into the template into sheet 2
Input_Sheet3.csv : The input data to be pasted into the template into sheet 3
Run.vbs: The vbscript which triggers the “copy-paste” macro present inside the Excel template.
The command to run the VBScript is: ‘cscript //Nologo Run.vbs /NoCancel’
Now, in order to automate whole task, I thought to trigger this VBScript remotely from Linux over ssh. I installed the cygwin ssh daemon on the windows box and facilitated ssh connections from the Linux machine. This cygwin ssh connection from Linux allows to run several windows commands, but fails to run the VBScript and doesn't produce any excel output.
I can't figure out which component has limitations. Is it cygwin ssh not facilitating execution of VBScript? Is it VBScript not facilitating creation of files remotely? Is it ssh client on Linux not allowing opening of excel in GUI format? Or there is another way to automate this task from Linux.
Thanks,
Shubham
I'd focus on:
Did you test running your script from the Cygwin Terminal? (I did try a simple script, that opened Execl (.Visible = True) successfully, so I believe your fears concerning cygwin not allowing to open the Excel GUI are unnecessary).
Do the sshd user account's path and cwd allow the short invocation "cscript Run.vbs" or should the long version "/cygdrive/c/WINDOWS/system32/cscript.exe /home/user/Run.vbs" be used?
Does the sshd user have access/permissions to all involved files and folders?
You didn't forget the unix file systems are case sensitive? (I did and wasted some time)?

How to run a Excel spreadsheet on another computer with VBScript

I have the following situation:
There is a Excel workbook saved on another computer on my network. I need to execute the file on that computer, because it is connected with a Big Screen to show some data. I was trying to automate the execution of the workbook but all that I can get is to make it run on MY computer, and I need it to run on THAT computer
The relevant part of the vbs code that I am using is the following:
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Set objXLWorkbook = objXL.Workbooks.Open(path)
I know that this method can only open Workbooks on my computer. Anyone knows how to make it run Excel on another computer?
You cannot - out of the box - run programs or scripts on remote computers. Otherwise, it would be even less difficult than it is today to install a virus remotely. Your vbs could simply install and activate a virus instead of an excel file.
Nonetheless its quite a common thing that one wants to do every now and again (the remote task, not the virus - especially administrators love to scare users by remotely opening some funny stuff).
Have a look at an article from Bill Stewart where he explains how to use Powershell to accomplish your task.
The required setup on the remote computer still needs to be done once.

Is this possible in Excel: Open XLS via commandline, OnLoad import CSV data, Print as PDF, Close Doc?

Thinking that to solve a problem I've got this is the fastest solution:
Generate a custom CSV file on the file (this is already done via Perl).
Have a XLS document opened via commandline via a scripting language (clients already got a few Perl scripts running in this pipeline.)
Write VBA or record a macro that executes the following OnLoad:
Imports a the data from the CSV file into the report template,
Print the file via PDF driver to fixed location using data in the CSV to name the file.
Closes the XLS file.
So, is this possible via Excel macros, if not is it possible via VBA -- thanks!
NOTE: Appears I've got to have a copy of MS Office anyway, so this is much faster to get going than using Visual Studio Tools for Office (VSTO). The report template is going to be on a server, and this way the end user can build as many reports as they like, "test" by printing a PDF using a demo CSV file, and import/embed the marco or VBA when they're done. I'd looked in Jasper Reports, but the end user is putting ad-hoc static text and groupings all over the report and I figure this way they can build reports how ever they want and then automate them. Both of these questions by me and the resulting comments/feedback are related to this question:
In Excel, is it possible to automate reading of CSV data into a template and printing it to PDF from the commandline?
Is it possible to deploy a VB application made in Excel as a stand alone app?
FOCUS OF QUESTION: Again, focus of the question is if this is possible via Excel marcos, if not macros VBA, and if there's any huge issue with this approach; for example, I know this is going to be "slow" since Excel would be loaded per job, but there's 16GB of ram on the server and it's not used at all. Figure since I've got to have a copy of office on the server anyway, this is a much faster approach.
If you've got any questions, let me know via comments.
I suppose you could launch the report file from perl and then have a macro inside the report file automatically look for the newest csv file to import. Then you could process and output. So you just need to launch the proper excel file with the embedded macros from perl and then let excel and VBA take over.

Resources