Solved

Powershell Excel VBS run the powershell command in the fields below

Posted on 2013-06-16
9
753 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 69

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 69

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 69

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 69

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

679 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