Link to home
Start Free TrialLog in
Avatar of janhoedt
janhoedt

asked on

Run powershell with specific powershell modules loaded

Hi,

I'd like to run powershell-commands to vmware, AD etc.
Now when I run a powershell ps1 (right mouse "run with powershell" it hasn't the modules loaded.

I guess that's something to do with profiles? How does it work + how can I do this automatically by system startup (load + connect to vcenter) so preventing slow down startup of a script (if it needs to load the modules each time I run a script it takes seconds before it runs).

Thanks for your advice.
J.
Avatar of Andrew Hancock (VMware vExpert PRO / EE Fellow/British Beekeeper)
Andrew Hancock (VMware vExpert PRO / EE Fellow/British Beekeeper)
Flag of United Kingdom of Great Britain and Northern Ireland image

I would recommend using the following, also VMware recommends the use of PowerCLI, which is PowerShell interface with vSphere-specific additions.

http://communities.vmware.com/community/vmtn/server/vsphere/automationtools/powercli

One of the simplest PowerCLI examples, and something which is actually extremely useful on its own, is the Get-VM cmdlet. Which lists VMs.

PowerCLI can be a bit overwhelming to use,  But it can be enhanced with a nice toolset and a library of preconfigured scripts to jump-start your mass changes, which is possible with PowerGUI

http://powergui.org/index.jspa

and

VMware Community Pack
http://www.virtu-al.net/featured-scripts/vmware-powerpack/
You need to import the respective modules while running the script..

You can do this by adding command Import-Module <Module Name> @ the beginning of the script..

For example...

    Import-Module -Name Activedirectory

    Import-Module -Name c:\snapins\Scripting\Scripting.dll


Refer..
http://blogs.technet.com/b/heyscriptingguy/archive/2010/10/16/learn-how-to-load-and-use-powershell-snap-ins.aspx

Or use Add-pssnapin
Ref : http://ss64.com/ps/add-pssnapin.html
Additionally,

 If you want to use PowerShell for Actrive Directory (On you client system) , You need install RSAT First to get the AD Snap in to your client system.

RSAT Can be downloded from here..

http://www.microsoft.com/en-in/download/details.aspx?id=7887

http://technet.microsoft.com/en-us/library/cc730825.aspx


once done run import-module activeDirectory


If you are runing powershell on DC , then no need of installtion RSAT , SUBSUN suggestion will work .

Thanks,

_Prashant_
You need to add all the import commands in profile.ps1 and keep it in the folder where powershell.exe is. It will load all the modules mentioned in profile whenever powershell starts.
This is not much secure method. Also try profiles.ps1, don't remember if S is needed in that file name.
Avatar of janhoedt
janhoedt

ASKER

Please advise more on the profile and how it works/to configure it.
Much easier to use PowerGUI as a beginner.
Ok, but I want to run a powershell command with right-click button. Then I 'd need profiles, right? Please advise on that (had already powergui installed).
What is the OS and powershell version?
I will give you a detailed write up tomorrow.
Windows 2008 r2 64 bit and windows 8 32 bit mainly. No changes to default powershell but I can update them. How does this matter?
Not sure about windows 8 but I can definitely tell you how to do it on 2008 tomorrow as i am away from my computer right now. You will have to do some research on windows 8.
Did you try adding modules inside script as I suggested in my previous comment?
For windows 2008 R2 follow these steps -

1. Open powershell and run this command -  Get-ExecutionPolicy

2. Execution policy should be at least "RemoteSigned" or change it by command Set-ExecutionPolicy
*** This is a security risk and you need to check your org. policies before changing the execution type.

3. Download attached file and modify it as per your needs, I hope it is self explanatory.

4. Rename the file from Profile.txt to Profile.PS1 and put it in below location -
 
C:\WINDOWS\system32\WindowsPowerShell\v1.0

5. Open powershell and see if the commands in profile.ps1 are loaded/executed.



Above steps are much simpler but you can also refer below links if you need more info -

http://www.computerperformance.co.uk/powershell/powershell_profile_ps1.htm
http://technet.microsoft.com/en-us/library/ee692764.aspx
Profile.txt
Thanks pramod_ubhe!

Now, 3 more questions:

1. I would like to impport SCCM 2012 modules on the sccm-server.
To load them, I need to execute this command:
Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
How come in your example the path is not needed + can I add them with the path in the profile?

2.howto execute these commands remotely? Do I need these modules loaded remotely or can I exectue them from another pc/workstation?

3.What about Windows Powershell ISE? It's included in Powershell 3.0 and completes/suggests completions. Is it as good as Powergui?
ASKER CERTIFIED SOLUTION
Avatar of Pramod Ubhe
Pramod Ubhe
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A few additional comments..

If you have Powershell v3.0, it will autoload the necessary modules when called, as long as they are loaded on the system.  I.e. If you have the AD tools installed on the system, and you call get-aduser, it will dynamically load the ActiveDirectory module.   Your Windows 8 systems will have PS3 on them already, and you can load it yourself on Win2k8R2.

The MS ISE in v3.0 is a substantial upgrade from v2.0, but it is not as good as PowerGUI.  

If you want to execute the commands remotely with v3.0, you will want to enable remoting on all your target devices, and spend time with the invoke-command cmdlet. It will allow you to easily run your commands on the remote machines.    Check the remoting help topic in powershell.  I believe it is about_remoting.  

Coralon