MS Powershell -All About

MSPowershellWhat is  PowerShell?

Powershell is Microsoft’s newest scripting language
focused on providing administrators with an easy and
powerful way to perform certain tasks.

Why should I brother with Powershell? 

Powershell versatility new technology “Eureka moments”

Here are a few features:

  • Windows OS Administration Cmdlets
  • Logon Scripts
  • WinPE environment
  • AD and Exchange Management(ADSI)
  • Cmdlets
  • Pipeline
  • Providers
  • Help systems

powershell

Supported Environments:,Window 7, Windows 8.1 and Windows 2K8 and 2K12.

II. Powershell 4.0 or 5.0 DownloadRequired:
Powershell 4 or 5 Download Reference

i. Powershell 5. 

Windows Management Framework (WMF) 5.0 Preview April 2015 contains functionality that has been updated from WMF 4.0. WMF 5.0 Preview April 2015 is available for installation on Windows 8.1, Windows Server 2012 R2, Windows Server 2012, Windows 2008 R2 and Windows 7, and contains updated versions of the following features:

PowerShell is either included or can be downloaded and installed on the following operating systems:

Why Powershell?

ii. OS and Service Pack Requirements:

Operating System Service Pack Level Editions
Windows 8.1 All
Windows Server 2012 R2 All except IA64
Windows Server 2012 All except IA64
Windows Server 2008 R2 SP1 All except IA64
Windows 7 SP1

1II. Installing PowwerShell on Windows Platform- For this demonstration, we will install Powershell 5.0 on windows 7 Professional SP1. Workstation Edition.

a. Download (Powershell 4 or 5 Download Reference) and Install Powershell 5.0 on Workstation Platform. 

*Illustrates which Power shell versions specific to the Server 2k(8 and 12) or Workstation(7 sp1 or 8.1) Platform Environment.

powershell1

powershell2

*Please note – click here will ease any download issue specific to client workstation web browsers pop up blocker 

b. Copy Windows61.-KB2908075-x64.msu locally on the workstation client and open. 

powerinstall5.01

c. Click Yes to continue with installation.

power5install2

d. Click I agree with EULA to proceed with installation.

powershell5install2*installation in progress- Restart workstation 

powershell5install3II. Open PowerShell – Search Powershell in windows search –

a.Right Click on Windows Powershell -Run As Administrator.

searchpowershellrunpowershellrunasadmin

 

b.  Enable Poweshell Script Execution

Note: As a security precaution remote PowerShell scripts cannot be executed unless you enable it. This is required before VMware commands can be executed.

i. Type in the Powershell CommandLine: set-ExecutionPolicy RemoteSigned.

The powershell execution policy determines where scripts can be run and if they need to be digitally signed.

powershellremotesingedII. Type Y =(Yes) to allow to change the exution policy to set-ExcutionPolicy Remotesigned.

III. Install the Vmware Powershell ComponentsSnap-Ins and Modules

*Please notes there are 2 main ways to extend the shell,  we will be add snap in method. 

Modules – A package that contains Windows PowerShell commands int he form of functions, cmdlerts and workflows, in addition it may contain variables, aliases and providers. Modules can be written in PowerShell and/or compiled as DLLs.

Snap-Ins – Are compiled cmdlets in to a DLL written in a .Net language are being deprecated and no longer recommended as the way to create and package new cmdlets.

Install the VMware PowerShell Components  by Installing  the Wmware ‘PowerCLI’ onto your local PC that contains the VMware software management objects and Cmdlets

*Reference Install PowerCLI  Section- if have not install PowerCLI before.

IV. Registrered – PSSnapin – Vmware.Automation Cmdlets- Open the VMware (not Windows) PowerShell-  Type- Get-Command -Noun PSSnapin.

a. PS C:\> get-command -noun pssnapin components.
PSSnapinregistred

b. PS C:\> Add-PSSnapin VMware.VimAutomation.Core and  PS C:\> help vm illustrate all Vmware.VIMAutomation.Core CMDLETS

addpsnapinmoduleC: Import-modules:

Copy powershell Modules to local machine  and navgiate to directoy.. Type import-module .\<module file name>

Module comes with Powershell 5.  under C:\Windows\System32\WindowsPowerShell\v1.0\Modules\

powershellmodules

 

PS C:\scripts\InfraTools\PowerCLIBook\PowerCLIBook> Import-Module .\PowerCLIBook.psd1

VERBOSE: Importing function ‘Add-DistributedSwitchVMHost’.
VERBOSE: Importing function ‘Backup-MsSqlDatabase’.
VERBOSE: Importing function ‘Backup-VcConfig’.

For example below modules are made up of four files:

  • PowerCLIBook.psd1 – The PowerShell module manifest file, which describes the contents of the module and controls how the module is processed.
  • PowerCLIBook.psm1 – The PowerShell module file containing the functions which make up the PowerCLIBook module.
  • DistributedSwitch.psm1 – The PowerShell module file containing the functions which make up the DistributedSwitch nested module, if you like a sub-module of the PowerCLIBook module.
  • Test-Version.ps1 – A script which is run prior to the PowerCLIBook module being called which checks to see if PowerCLI 4.1 or newer is installed.

 * will importing all above modules*

  • From your PowerShell session you will now be able to see that the PowerCLIBook Module is available:

Get-Module -ListAvailable

getmoduleliset

  • To use functions from the module it needs to be imported into the current session:

Import-Module PowerCLIBook

  • To view what functions are now available (in total there are 79!):

Get-Command -Module PowerCLIBook

powerclibook1To view the comamnds specific to Distributed Switches:

Get-Command *DistributedSwitch*

getmoduleliset32To get help on one of the functions:

Get-Help Set-DistributedSwitch -Detailed

  • To view the help examples for a function

Get-Help Set-DistributedSwitch -Examples

D. Test Vmware Powershell CMDLET – Open the VMware (not Windows) PowerShell
Command Prompt and type the following:

1. Connection to Vcenter or ESXI Host : Syntax Example

Connect-VIServer -Server
<vCenterIPAddress> -User
<adminUsername> -Password <password>

PS C:\> Connect-VIServer -Server mvp-vs01.mvp.local

connectesxhostviapowershell

2.  User and Password Prompt- Input Vcenter or ESXI User ID and Password. 

powershelluseridpromptesxi

*Unsigned Certificate haven’t been configured then see a warning- This can be ignored. 

3. User Successful Authenticated to ESXI or Vcenter. 

ESXI- User Root 

ESXIrootuserauthenicationsuccesfully

Vcenter-Domain User-

powershellvcenter1

4.  List Vmware CMDLET- Typing help VM:

*Notice there more option; Click Spacebar to more of the Vmware CMDLET.

listvmcmdletpowershellSample of Example Commands:

PowerShell Command= Action
Start-VM <guestVMname> =Power on the VM specified
ShutDown-VMGuest <guestVMname> =Shuts down Guest OS
Get-DRSRule –Cluster <ClusterName>=Displays DRS rules for specified cluster
Note: All commands above are entered as one line

6. Execute  a VMware CMDLET, For example, power up a VM guest on ESXI host/Vcenter.

PS C:\> start-VM mvp-sccm02 on ESXI Host.

startvmhostpowershell2startvmhostpowershell3Start VM on Vcenter Host:

startingvumserver.

Vcenter Rcent Taskstarvumserver01Post Successfully Start VM host

startvmcmdlet

* You will see VM host PowerState, Number CPUs , Memory GB status in Powershell Windows

7.  Disconnect from vCenter or ESX
Once finished, it is good practice to disconnect. To do
this type the following command:
Disconnect-VIServer

C:\WINDOWS\system32\
windowspowershell\v1.0\powershell.
exe -PSConsoleFile “C:\Program
Files\VMware\Infrastructure\VSphere
PowerCLI\vim.psc1” -NoExit –Command

Note: The command above is entered as one line

Don’t forget you still must connect to a vCenter Server
or ESX Host so the first command might look like this:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program
Files\VMware\Infrastructure\VSphere PowerCLI\vim.psc1”

-NoExit –Command Connect-VIServer
-Server <vCenterIPAddress> -User
<adminUsername> -Password <password>
Note: The command above is entered as one line

Likewise a PowerShell script named C:\My1stPSscript.ps1 can be called by typing:

C:\WINDOWS\system32\
windowspowershell\v1.0\powershell.
exe -PSConsoleFile “C:\Program
Files\VMware\Infrastructure\VSphere
PowerCLI\vim.psc1” -NoExit –Command
C:\MyFirstPSscript.ps1
Note: The command above is entered as one line

V. Summary

The basics of VMware PowerShell commands has a wealth of information on
PowerShell scripting between the integration of VMware and Microsoft
tools- with with a number of sample scripts.

listvmcmdletpowershell

For example: Let say you need to a VM guest network configuration

PS C:\> get-VMGuestNetworkInterface mvp-sccm01.mvp.local

References:

1. Microsoft PowerShell How Install.
Microsoft PowerShell How Install

2.VMware PowerShell Components Download and
Documentation.
https://www.vmware.com/support/pubs/ps_pubs.html

3. Vmware Powershell Compoenets Install Guide:
https://www.vmware.com/support/developer/windowstoolkit/wintk40/doc/viwin_install.pdf

4. PowerCLI book author has written and introduction to get you up to speed –
http://www.simple-talk.com/sysadmin/powershell/an-introduction-to-powershell-modules/

VI. NetApp PowerShell cmdlets(coming soon!!)

Download the DataONTAP.zip from NetApp site
http://community.netapp.com/t5/Microsoft-Cloud-and-Virtualization-Discussions/Data-ONTAP-PowerShell-Toolkit-3-2/td-p/93081

2. Extract the content folder DataONTAP.

3. Copy the DataONTAP folder to the following location: C:\Windows\System32\WindowsPowerShell\v1.0\Modules

4. Launch the PowerShell window and type the following command. Confirm ‘Yes’ when prompted.

Set-ExecutionPolicy unrestricted

5. Type another command to import the NetApp DataOntap modules in PowerShell:

image

6. Its done! You can confirm by running below command. It will list all the NetAPP cmdlets available

Get-Command -module DataONTAP

Import Vmware CMDLET Modules Guide

Scope: This posting is show overview import Vmware Powershell CMDLET  to manage Vmware environment.

Introduction:

CMDLET Modules have primarily 2 locations on your system:

Default location
%windir%\system32\WindowsPowerShell\Modules this is the location for system wide modules available to any user in the system.

User Level
%USERPROFILE%\Documents\WindowsPowerShell\Modules

C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShel
l\v1.0\Modules\

Each module is stored in a folder where there is a psd1 file that is known as a Module Manifest,powermaifeastmodulesvsphere6

getmodulelisetmodulesvsphere6aThese manifest has the settings for the module and sets the restrictions for it in terms of .Net Framework version, version of PowerShell, files to load, version, copyright, author and many other settings.

This file can load what is called a main module and sub-modules each can either be a psm1 or dll file, in addition they can also be scripts that gets processes. As it can be seen using modules provide great flexibility in terms of formats and structure.

We can also have modules in other locations that can be accessed by the PowerShell session we run in, the locations are defined in the environment variable :$env:PSModulePath

Example

C:\> $env:PSModulePath

* C:\Users\<Vmware Admin User>\Documents\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\Modules\

C:\Users\richard\Documents\WindowsPowerShell\modules;C:\Windows\system32\WindowsPowerShell\Modules\

If we want to add another path for PowerShell to look at we just add that path to the current environment variable:

By using the following setting:

$env:psmodulepath = $env:psmodulepath + “;”

To list the modules that are available we use the Get-Module cmdlet withe the -listavailable parameter:

Get-Module -ListAvailable

2.  Download Vmware Powershell Modules:

PowerCLIBook.psd1 – The PowerShell module manifest file, which describes the contents of the module and controls how the module is processed.

PowerCLIBook.psm1 – The PowerShell module file containing the functions which make up the PowerCLIBook module.

DistributedSwitch.psm1 – The PowerShell module file containing the functions which make up the DistributedSwitch nested module, if you like a sub-module of the PowerCLIBook module.

Vmpowerclicmdlet.psm1-The Powersehll module manifest file,The PowerShell module file containing the functions which make up the  module within  VMware.VimAutomation.Core.

I. Import Vmware CMDLET Modules Guide

1. Set Default  the CMDLET Modules Path location by typing the environment variable :$env:PSModulePath

A. Copy CMDLET psm1 files to CMDLET Module User Path (C:\Users\richard\Documents\WindowsPowerShell\modules(Reference CMDLET psm1 download)

powermaifeastB. Open Powershell as Administrator-—> Type; $env:PSModulePath check the default module environment Path Location:

environpathSyntax: PS C:\> $env:PSModulePath

Output Default Environment Path: “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShel
l\v1.0\Modules\”

C. Add User Profile Modules to existing Environment Powershell Path:

Syntax: $env:psmodulepath = $env:psmodulepath + “;”

Example adding user path  C:\Users\richard\Documents\WindowsPowerShell\modules to default Powershell Environment Path:

PS C:\> $env:psmodulepath = $env:psmodulepath + “C:\Users\richard\Documents\WindowsPowerShell\modules;C:\Windows\system32\WindowsPowerShell\Modules\”
PS C:\>custompowershelldefaultenvironmentpowershelldefaultenvironmentOutput Default Environment Path with added custom environment path:

PS C:\> $env:PSModulePath
C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShel
l\v1.0\Modules\C:\Users\richard\Documents\WindowsPowerShell\modules;C:\Windows\system32\WindowsPowerShell\Modules\C:\Users\richard\Documents\WindowsPowerShell\modules;C:\Windows\system32\WindowsPowerShell\Modules\

D. List all the Modules—–> Typing:
PS C:\> get-module -ListAvailable

listmodules*Notice you didn’t see none of the below modules there?

PowerCLIBook.psd1 – The PowerShell module manifest file, which describes the contents of the module and controls how the module is processed.

PowerCLIBook.psm1 – The PowerShell module file containing the functions which make up the PowerCLIBook module.

DistributedSwitch.psm1 – The PowerShell module file containing the functions which make up the DistributedSwitch nested module, if you like a sub-module of the PowerCLIBook module.

Vmpowerclicmdlet.psm1-The Powersehll module manifest file,The PowerShell module file containing the functions which make up the  module within  VMware.VimAutomation.Core.

* You much create the exact file path as the psm1 file within the path..

For example—-> All about module must reside under exact folder name :

PowerCLIBook.psd1 – The PowerShell module manifest file, which describes the contents of the module and controls how the module is processed.

  • C:\Users\richard\Documents\WindowsPowerShell\modules\PowerCLIBook<module same as the upper level>

powerclifilepath*Make sure you match module name exact folder structure– module\module folder name\module name(same)

modulespwoershellsCreate folder under Module level —> Module Folder name then put module psd1 under it.

modulesfoldersF: Import Module- Type import-module <Module Folder>

Syntax:
PS C:\> Import-Module PowerCLIBook -Verbose

PowerCLIBook Modules
importmodulepowerclibook
VERBOSE: Importing function ‘Evacuate-VMHost’.
VERBOSE: Importing function ‘Export-Folders’.
VERBOSE: Importing function ‘Export-PermissionsToCSV’.
VERBOSE: Importing function ‘Export-VMLocation’.
VERBOSE: Importing function ‘Export-Xls’.
VERBOSE: Importing function ‘Get-ADUserObject’.
VERBOSE: Importing function ‘Get-AggregationJobs’.
VERBOSE: Importing function ‘Get-ClusterSummary’.
VERBOSE: Importing function ‘Get-DistributedSwitch’.
VERBOSE: Importing function ‘Get-DistributedSwitchCandidate’.
VERBOSE: Importing function ‘Get-DistributedSwitchNetworkAdapter’.
VERBOSE: Importing function ‘Get-DistributedSwitchPortGroup’.
VERBOSE: Importing function ‘Get-DistributedSwitchPrivateVLAN’.
VERBOSE: Importing function ‘Get-FolderPath’.
VERBOSE: Importing function ‘Get-FolderStructure’.
VERBOSE: Importing function ‘Get-IPPool’.
VERBOSE: Importing function ‘Get-LicenseKey’.
VERBOSE: Importing function ‘Get-MissingPortgroups’.
VERBOSE: Importing function ‘Get-Network’.
VERBOSE: Importing function ‘Get-OvfDetails’.
VERBOSE: Importing function ‘Get-SnapshotCreator’.
Vmware VDS Module
importmoduleVDS
PS C:\> Import-Module DistributedSwitch -Verbose
VERBOSE: Importing function ‘Add-DistributedSwitchVMHost’.
VERBOSE: Importing function ‘Get-DistributedSwitch’.
VERBOSE: Importing function ‘Get-DistributedSwitchCandidate’.
VERBOSE: Importing function ‘Get-DistributedSwitchNetworkAdapter’.
VERBOSE: Importing function ‘Get-DistributedSwitchPortGroup’.
VERBOSE: Importing function ‘Get-DistributedSwitchPrivateVLAN’.
VERBOSE: Importing function ‘Get-VMHostPnic’.
VERBOSE: Importing function ‘New-DistributedSwitch’.
VERBOSE: Importing function ‘New-DistributedSwitchNetworkAdapter’.
VERBOSE: Importing function ‘New-DistributedSwitchPortGroup’.
VERBOSE: Importing function ‘New-DistributedSwitchPrivateVLAN’.
VERBOSE: Importing function ‘New-VMHostNetworkAdapter’.
VERBOSE: Importing function ‘Remove-DistributedSwitch’.
VERBOSE: Importing function ‘Remove-DistributedSwitchNetworkAdapter’.
VERBOSE: Importing function ‘Remove-DistributedSwitchPortGroup’.
VERBOSE: Importing function ‘Remove-DistributedSwitchPrivateVLAN’.
VERBOSE: Importing function ‘Remove-DistributedSwitchVMHost’.
VERBOSE: Importing function ‘Set-DistributedSwitch’.
VERBOSE: Importing function ‘Set-DistributedSwitchNetworkAdapter’.
VERBOSE: Importing function ‘Set-DistributedSwitchPortGroup’.
VERBOSE: Importing function ‘Set-DistributedSwitchVMHost’.

InfraTools
importmoduleinfratools
PS C:\> Import-Module infraTools -Verbose
VERBOSE: Importing function ‘Get-SyslogInfo’.
VERBOSE: Importing function ‘Get-VMCreationDate’.
VERBOSE: Importing function ‘Get-VMdisk’.
VERBOSE: Importing function ‘Get-VMEvents’.
VERBOSE: Importing function ‘Get-VMStat’.
VERBOSE: Importing function ‘Set-VMdisk’.

G: List Module Recently Imported:

listednewlyimportedmodulesIII. References:

A. PowerCLIBook
http://www.powerclibook.com/?p=137B. Windows Powershell Moduleshttps://msdn.microsoft.com/en-us/library/dd878324%28v=vs.85%29.aspx

B. Vmware PowerCLI Blog
http://blogs.vmware.com/PowerCLI/tag/powercli-modules

C. Infra Tools Modules
https://psvmware.wordpress.com/

 


Leave a Reply

Your email address will not be published. Required fields are marked *