0

In PowerShell, when I created an instance from a PSCmdLet via $cmdlet = [Namespace.Type]::new(), how would I invoke that instance in the current or a new PowerShell session?
Calling Invoke() itself will end in a Cmdlets derived from PSCmdlet cannot be invoked directly. error-message.

TGlatzer
  • 5,815
  • 2
  • 25
  • 46
  • An instance of a class contains properties and methods. You can call run any method by simple use the instance of the variable and the method name: $cmdlet.Method(). You do not need invoke. – jdweng Apr 05 '23 at 08:48
  • @jdweng I'd agree with you, if the process I'd like to run wasn't encapsulated in an PSCmdLet and some global state that's created during (protected) BeginProcess, ProcessRecord and so on... – TGlatzer Apr 05 '23 at 09:24
  • Do you need to run the code as a separate process? Most cases you only need a separate process if you are running code in parallel. – jdweng Apr 05 '23 at 10:08
  • The process is the PowerShell installation module for ADFS. It has an error during a check of a acl. So I went ahead and tried to instantiate the PsCmdLet (InstallFarmCommand), since it has switches, which allow to skip those checks, that are NOT surfaced as parameters. And that specific instance of the PSCmdLet is what I'd like to execute in my current PowerShell session (or in a new one - I don't really care) – TGlatzer Apr 05 '23 at 10:29
  • Is failure due to not running As Admin? Most users would not have permission to read the ACL and would get an exception. You may need to start by right click PS shortcut and select Run As Admin. PsCmdLet does not have option to Run As Admin. – jdweng Apr 05 '23 at 11:38
  • The module's check for the rights is not aligned with the installation requirements provided by Microsoft and so, we're trying to work around the problem. Invoking the PSCmdLet instance would instantanously solve the problem for us, since it skips the uneccessary check. There's no problem with my local rights, there's no problem with the service accounts rights on the ADObject or anything in that direction. Without the check, the code would just work - and there's a SwitchParamter, that would allow for that to be set, it's public, but not [Parameter] – TGlatzer Apr 05 '23 at 14:18
  • The PSCmdlet command must hae a base class PSCmdlet. See : https://stackoverflow.com/questions/46957866/invoke-a-pscmdlet-from-within-a-cmdlet?force_isolation=true – jdweng Apr 05 '23 at 14:39
  • I already found that specific question, but could not distill an answer to my question from it, since I can't provide the command as string – TGlatzer Apr 08 '23 at 07:29
  • Did you look at all the answers including one with check mark? – jdweng Apr 08 '23 at 07:43

0 Answers0