Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Excel VBS run the powershell command in the fields below

Posted on 2013-06-16
9
Medium Priority
?
922 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 71

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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 71

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

705 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