PSProvider and VMware datastore – PowerCLI

Hello everyone,

I am writing this short blog after a long time. While explaining in-out of PowerShell to some of my friends in person, I discussed about PSProviders. Most of the knowledge about PSProvider is information only and as a script writer we dont really bother about how powershell is playing with different resources (Variable/function/Filesystem/Registery Keys etc) which are used in PowerShell Session or a Script.

However as a VMware Admin I do use PSProvider in background alot in order to move the datastore item from,

  1. datastore to datastore
  2. Datastore to local drive (Windows Drive or Shared Drive) or vice versa

In this Post we will learn about Copy-DatastoreItem cmdlet and PSProviders.

What is PSProvider?

In Simple Term, PSProviders are special repository(data stored within Powershell)  to process the data as it recieves during PowerShell execution. This data is presented in their respective PowerShell drives which are known as PSDrive.

For Ex. See the below command output from Get-PSProvider

Get-Provider.PNG

by default, you get above psproviders which are registry, Alias, Environment, Filesystem, Function and variable. You can also see the respective drives associated to its PSProvider. This means that if you are creating any variable it will be stored in variable: , If you are creating a function then it will be stored in Function: 

check the below image, where i am going into respective drive and able to see the variable which i have created. 

CDVariable

In conclusion, whatever variable/function/etc which I create in powershell gets stored in their respective drives.

vimstore PSProvider. 

vimstore is a one of the PSProvider which you get after connecting to VMware vCenter via PowerCLI. Do this, Connect-VIServer vCenter-ip and then run get-PSProvider cmdlet and you will see additional PSProviders are available to you. These providers are something which provides VMware Inventory and datastore resources to the POwerCLI or PowerShell.
POwerCLIProviders

So, After connecting to vCenter via PowerCli you can see additional PSDrives are available to you, provided by 2 additional PSProviders. I can do cd vmstore: and can actually list the available datastore in the datastore inventory (Simillar to how we list the directories and files in a path) or can list the host inventory.

Once you are connected you can follow below commands to create a New-PSDrvive with ‘Vimdatastore’ PSProvider.

DatastoreDS

Now you have DS: drive which is available to you and you can basically navigate through the same way you do it for any other drive.

Use below command to move data from your local drive to the VMware datastore using PowerCLI. Please note that i am already in DS: , If you are in any other drive then give proper path using vimdatastore drive

copydatastore.PNG

Note: This method is quite helpful in case you are trying to move things around from datastore and you can automate the move operation. also this is an alternate to certificate error which you may receive while moving data from Web Client. For ex, Operation failed when I tried to upload the same ISO using web client.

CertError

Use PowerCLI vimdatastore Psprovider and copy-datastoreitem cmdlet to work around this.

 

Thanks

Jatin

 

 

 

 

 

 

 

 

 

My Failed Attempt with PowerShell DSC + VMware !

The Idea: 

  1. Have a DSC Configuration for vSphere which get status of  vSwitch Security Policies like Forged transmit, Promiscuous mode and MAC address change.
  2. All 3 security settings must be set to Reject.
  3. DSC Configuration should set above 3 settings to Reject if it is not.
  4. And finally apply this configuration to all ESXi host in vCenter. 

In recent days I am learning PowerShell DSC. No doubt it is a great tool but need more DSC resources for various technologies. I am sure that PowerShell community will rise above all and work toward making DSC more powerful.

My initial idea was to create a vSphere Security Configuration for vSwitch Security policies using PowerShell DSC. However, It didn’t work out for me as expected. Initially i struggled with Script resource and variable scope in GetScript, TestScript and SetScript block. Basically I was not able to pass variable in Script Block. This issue is documented here. Thankfully with the help of my colleague Rohit Sharma, We were able to resolve variable scope issue. Bottom line of that issue is, we have to use $using to pass the data in script block(Get,Set and Test).

It was a great relief for me to resolve above issue but…. Problems didn’t stop here. GetScript Block returns hash table values. So, If you try to return any PowerShell Object, Variable or any other object then this script block will throw an error. Ultimately you have to return hash table.

Well, That was sorted out. GetScript and TestScript looked to be working fine but another problem occurs at SetScript block. This time we figured out that session which was connected with Connect-VIServer is no longer available in SetScript block, which means that it will not execute any PowerCLI commands and will throw an error

DSC

“You are not currently connected to any servers. Please connect first using a Connect cmdlet.”

Why is it throwing this error? I figured out that DSC Script resource has 3 script block; GetScript, TestScript and SetScript. These script block does not execute anything but pass its value to Get-TargetResource(), Test-TargetResource() and Set-targetResource as a parameter respectively. These functions uses Invoke-command cmdlet on remote computer or localhost. That was the reason for sing $using: for local scope variables and also reason for not passing connected VMware sessions to SetScript block.

Below is the Code for reference. Feel free to test this in POC/Test environment. 

 

As of now i am still struggling to fix these issues. Looks like DSC Script resource has some limitations.

What Next?

  1. Will Understand “How to create custom DSC Resources using PowerShell Classes and Object?”
  2. Will try to build a custom resource by my own.

Hopefully I will be able to resolve this issue and will come out with a DSC Configuration for vSwitch Security policy.

Thanks

 

 

ESXCLI+PowerCLI: Perfect fusion for ESXi

powershell-cim_1

Hello Everyone,

In this post I will discuss about managing ESXi hosts using ESXCLI and PowerCLI. Yes, You got it right. ESXCLI is one of the widely used command line interface for ESXi whereas PowerCLI is a PowerShell based snap-in for VMware.

ESXCLI is intended to provide a single set of commands to perform host based administrative tasks. ESXCLI has set of namespace for various components like network, storage, device etc. Refer below pic to see available namespaces.

How to run ESXCLI?

Well working with ESXCLI is very simple. Take a putty session to host and type esxcliOnce you run this command then it will show you all the available namespace under ESXCLI. Further more you can type child namespace and it will show you available namespace and commands under that.

Ex. 1

~ # esxcli network nic list

The above command will list NIC details (MAC, Adapter type, Link status etc.)

ESXCLI
ESXCLI Namespace

When you have very small environment or performing specific tasks on 1-2 esxi then taking a putty session and running ESXCLI command is not a big pain. However there are scenarios where you want to perform management tasks on large number of ESXi hosts then above approach may not be a feasible one. Such scenarios could be-

  1. Getting certain reports across all the hosts (Firmware, driver version or specific configuration detail)
  2. Setting up standard configuration across multiple hosts or entire environment.

etc.

So in such type of scenario you would like to automate the task. For that obvious choice would be PowerCLI.

Tip: PowerCLI is a PowerShell snap-in for VMware. There is no specific training required for PowerCLI. 

How to execute ESXCLI Commands using PowerCLI?

PowerCLI has a cmdlet called Get-ESXCLI, This cmdlet can be executed against remote ESXi hosts. This can be done by passing a value (hostname) to -VMhost parameter. Refer below command-

PowerCLI C:\> Get-Esxcli -VMhost TestEsxi.poc

The above command will give you available ESXCLI namespace on TestEsxi.poc host.

For a moment let us consider Ex 1. ~ # esxcli network nic list

Same result can be achieved via executing below command in PowerCLI.

PowerCLI C:> (Get-Esxcli -VMhost "TestEsxi.poc").network.nic.list()

How to automate same task for multiple hosts?

Below is the PS script which will automate same task for multiple host. I don’t think that below script needs any explanation, It is very simple and straight forward script. First, You Get content from a .txt file. This file contains ESXi Host names. In later part you are just taking hostname one by one and dynamically passing it to cmdlet.

Please refer below screenshot for same, Though it does not harm anything to your environment but still i would highly recommend you to run this in POC first. Also please note that you have to connect vCenter first.

PowerCLI C:> Connect-VIServer -Server vCenterName

 

ESXCLItest.PNG
ESXCLI Test

Conclusion:

ESXCLI is a great tool for ESXi management and capability to integrate with PowerShell is just awesome. Next time if you have to collect few reports from multiple ESXi hosts then try to automate those reports using PowerCLI and ESXCLI.

FAQs:

  1. How to Learn PowerShell?
  2. How to install PowerCLI within PowerShell Console?
    • Check out this blog for your reference

Hope you liked this post, Please comment and share your feedback in comment section.

Thanks,