Solved

Powershell Excel VBS run the powershell command in the fields below

Posted on 2013-06-16
9
672 Views
Last Modified: 2013-06-24
example
hi,

if i push the button in excel i like to run the powershell code below. I know how to run
a ps1 from excel.  This one should have variable data and i like to use a lot of different
powershell commands within excel.

open the shell run the command below and leave the shell open.

appreciate for your help
mandy
0
Comment
Question by:Mandy_
  • 4
  • 4
9 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39251196
>>open the shell run the command below and leave the shell open.

what command below?
0
 
LVL 2

Author Comment

by:Mandy_
ID: 39251652
below the button in excel pls see attached picture

e.g. enable-mailbox -identify testuser -database db001

thx
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39251891
Please elaborate on what you are after. You can use an invisible PowerShell (acting as a PS host, like the ISE or the PS command prompt), you can call PowerShell with a command, you can call PowerShell as a command prompt executing some initialization code, ...

As I read it, you want to use Excel as "command center" for PS, providing some parameters entered in PowerShell. You say you know how to call PS from Excel, so what is it especially you want us to help you with?
0
 
LVL 2

Author Comment

by:Mandy_
ID: 39266708
Hi

sorry for my late answer.  I know how can i call a complete ps1 script but i want is call
the command after the button only. I could write it to a script (ps1) but i like to
use different variables in excel like the user id or other variables that are linked
in excel itself

a simple example .  userid and group are variable and are linked in excel to the command line
after the button. If i change the userid or group it will be automatically changed in the
command line after the button.
if i have only a ps1 script after the button like "adduser.ps1" and i call it with the button in excel i have to change every time the complete script for other groups or user and that is more work as copy and paste the command directly in powershell window.



simple example
0
Do email signature updates give you a headache?

Constantly trying to correctly format email signatures? Spending all of your time at every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39268012
If you have a small amount of scripts to run, use them with the PARAM() syntax, and provide the dynamic values when calling:
# Script C:\Scripts\Example.ps to do some stuff with user and group
Param($usr, $grp)
write-host "User $usr with group $grp"

Open in new window

and call that as
"PowerShell -command ""C:\Scripts\Example.ps " & [UserID] & " " & [Group] & """"

Open in new window

or
"PowerShell -command {C:\Scripts\Example.ps " & [UserID] & " " & [Group] & "}"

Open in new window

For more dynamic code it might be better to write them into a string building a script block:
strCmd = "PowerShell -Command { Import-Module ActiveDirectory; Add-ADGroupMember " & [UserID] & "  " & [Group] & " }"

Open in new window

0
 
LVL 2

Author Comment

by:Mandy_
ID: 39268281
Thank you so much. Could you pls explain how to use the activeX Button in excel
to execute the script block or the first example?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39269178
You would start it the same as a .ps1 file, usually with a Shell command.
0
 
LVL 2

Author Comment

by:Mandy_
ID: 39269511
Okay, thanks.

Do you also know how to implement this in AHK (AutoHotKey) ?

I've this behind. but how i can change the variables (group,Userid) in AHK with Edit windows?

ButtonAddGroup:
Run C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command -noexit { Import-Module ActiveDirectory; Add-ADGroupMember " & [UserID] & "  " & [Group] & " }"
Return

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39269890
Willing to use AHK for this purpose is a very important fact you need to mention from start. The way you have asked the question, you wanted to know the VBA and/or PS code for calling PS scripts or commands with vars. Now that morphed into using ActiveX Controls or AHK - both are something needing a completely different logic and knowledge.

I strongly recommend to close this question and ask a new one for using AHK with Excel (with a reference to this question). What is called does not matter much, anyway you have the code as it would be used in VBA, and that should be translatable into AHK without much ado.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now