Solved

Powershell Excel VBS run the powershell command in the fields below

Posted on 2013-06-16
9
857 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 70

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 70

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 70

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 70

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
This video discusses moving either the default database or any database to a new volume.
Suggested Courses

623 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