Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 168400

Re: Trouble with psdrive when the powershell script is run from c#.

$
0
0

After a couple of frustrating days, the solution, or workaround to this, is NOT to load the VMware.VimAutomation.Core snapin in C#. Just create the PowerShell Runspace and use the Add-PSSnapin within the Powershell/PowerCLI scripts.

 

Once the Snapin has loaded, it should remain cached in memory for the length of time the C# application remains open/does not destroy the Runspace. Therefore the delay of up to a minute, or longer, while loading is only on first run. Running the Get-PSDrives cmdlet should now show the vi(s) and vmstore(s) drives.

 

Obviously if you are calling a C# application every time you want to run a script then there will be a delay whilst the script adds the snapin each time.

 

Because you were loading in to the C# runspace, attempting to re-add the snapin for use by the script in that Runspace will not work. Prove this by adding the following to the start of your script whilst still loading the snapin within C#:


Remove-PSSnapin VMware.VimAutomation.Core -confirm:$false

Add-PSSnapin VMware.VimAutomation.Core

Get-PSDrives

 

You will notice the VimDatastore/VimInventory Provider Drives are still not there

 

Also, the InitializePowerCLIEnvironment.ps1 isn't really needed - You're already loading all of the VMware snapins... And you are running this from C#, that script is for setting up the environment when loading the PowerCLI command prompt, WindowTitle change, yellow font etc...

 

Hope this helps


Viewing all articles
Browse latest Browse all 168400

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>