Hello,
I'm trying to get all VMs in server, getting their metadata and svaing them to csv, but I'm getting error calling cmdlets
My code is :
ConnectVIServer cs = new ConnectVIServer();
cs.Server = new string[1] { ViServer};
cs.User = ViUsername;
cs.Password = ViPassword;
cs.Invoke();
GetVM getVM = new GetVM();
foreach (VirtualMachine vm in getVM.Invoke<VirtualMachine>())
{
var name = vm.Name;
}
with error message: System.InvalidOperationException: 'Cmdlets derived from PSCmdlet cannot be invoked directly. ' in step getVM.Invoke<VirtualMachine>()
The whole idea behind this is conversion of PS script into simple C# application to avoid the need of installation all PS modules as users don't have install permission, but need to export the metadata.
Edit 1: I' using these libraries
\VMware.VimAutomation.Sdk\13.1.0.21605170\net472\VMware.VimAutomation.Sdk.Util10Ps.dll
\VMware.VimAutomation.Core\13.1.0.21606170\net472\VMware.VimAutomation.ViCore.Cmdlets.dll
\VMware.VimAutomation.Core\13.1.0.21606170\net472\VMware.VimAutomation.ViCore.Impl.dll
\VMware.VimAutomation.Core\13.1.0.21606170\net472\VMware.VimAutomation.ViCore.Interop.dll
\VMware.VimAutomation.Core\13.1.0.21606170\net472\VMware.VimAutomation.ViCore.Types.dll
\VMware.VimAutomation.Core\13.1.0.21606170\net472\VMware.VimAutomation.ViCore.Util10.dll
\VMware.VimAutomation.Core\13.1.0.21606170\net472\VMware.VimAutomation.ViCore.Util10Ps.dll
You still need to install the dll files - what is the reason you are trying to do that? If the user does not have permissions to install PowerShell modules to a system folder, you can extract the modules to any folder and update the $env:PSModulePath to contain that folder for this PowerShell session.
E.g.:
$env:PSModulePath = "C:\Users\myUser\Downloads\VMware-PowerCLI-13.0.0-20829139"
import-module "VMware.VimAutomation.Core"
See PowerCLI Installation Guide for the steps for offline installation.
Did you try with a helper class as in c# - Invoke a PSCmdlet from within a cmdlet - Stack Overflow
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, I tried, but with no success, it gave me error message:
The term 'Get-VM' is not recognized as the name of a cmdlet, function, script file, or operable program.
I even tried to include the library into SessionState with no success
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModulesFromPath("C:\\Temp\\powerCLI\\VMware.VimAutomation.Core\\13.1.0.21606170\\VMware.VimAutomation.Core.psd1");
Runspace rs = RunspaceFactory.CreateRunspace(iss);
System.Management.Automation.Runspaces.Runspace.DefaultRunspace=rs;
rs.Open();
ConnectVIServer cs = new ConnectVIServer();
cs.Server = new string[1] { ViServer };
cs.User = ViUsername;
cs.Password = ViPassword;
cs.Invoke();
GetVM getVm = new GetVM();
PsInvoker psi = PsInvoker.Create("Get-VM");
var obj = psi.Invoke(); //<-- this is what makes the error message from helper class
foreach (VirtualMachine vm in obj)
{
var name = vm.Name;
}
Did you stop/start your session?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, even restarted PC, the issue presists
Did you already try with
iss.ImportPSModulesFromPath("C:\\Temp\\powerCLI\\VMware.VimAutomation.Core\\13.1.0.21606170");
The Import
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, I already tried with the same issue
Let me try to ping someone with more dev experience
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You still need to install the dll files - what is the reason you are trying to do that? If the user does not have permissions to install PowerShell modules to a system folder, you can extract the modules to any folder and update the $env:PSModulePath to contain that folder for this PowerShell session.
E.g.:
$env:PSModulePath = "C:\Users\myUser\Downloads\VMware-PowerCLI-13.0.0-20829139"
import-module "VMware.VimAutomation.Core"
See PowerCLI Installation Guide for the steps for offline installation.
I will try to propose this solution to the person who wanted the conversion
@AtanasAtanasov That is a good workaround I assume.
But I'm also interested in how would actually call a PowerCLI cmdlet from C#.
Is that possible?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD As far as I know, the PowerShell API allows you to create essentially your own PowerShell host (using Runspaces etc.) where you load modules and can issue commands as text. I am not sure if calling the cmdlet class directly will work though as that will probably skip some logic from the interpreter and miss some functionality.
Hello I got answer from the person and solution to extract the modules and include them in the script is possible.
Thank you for answer