Solved

Powershell Excel VBS run the powershell command in the fields below

Posted on 2013-06-16
9
706 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
how to add IIS SMTP to handle application/Scanner relays into office 365.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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