LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Issues translating PowerShell to LabVIEW (finding Assembly & Error 3008)

I'm having trouble translating two PowerShell scripts into LabVIEW.

 

The first worked fine in Windows 7, but is broken in Windows 10.

] $driveEject = New-Object -comObject Shell.Application
] $driveEject.Namespace(17).ParseName("X:").InvokeVerb("Eject")

 

For Windows 7, I used ActiveX "Automation Open", passing a static reference to "Microsoft Shell Controls And Automation Version 1.0/Shell (Shell.Application.1)" (aka Shell32.IShellDispatch6?).  Then I invoke NameSpace(17) on it, then "ParseName('C:'), and finally InvokeVerb('Eject'), all using LabVIEW Invoke nodes.  That worked great, as long as the drive was ready to eject (and popped up a dialog if it wasn't).

 

Now, in Windows 10, it started as a broken VI.  I had to re-select the exact same object for the static reference, and it was able to compile.  But it doesn't run.  Invoke:NameSpace(17) returns error 3008, "Automation Interface for the specified class cannot be obtained..."  It seems that isn't an unusual error when calling less standard .dll files, but shell32.dll???  (or is that what I'm really invoking?).  And the solution seems different each time.  For grins, I called every NameSpace(i) from 0...100; all are the same.  The Invoke Node does offer 'NameSpace' as a method I can call, so I know it is reading the .dll & getting the interface, even though the error says it can't.

 

 

I'm also hoping to translate:

] $o = new-object -com shell.application
] $o.Namespace('c:\My Folder').Self.InvokeVerb("pintohome")

 

I get the same error message (Windows 10 only this time).

 

 

I'd be happy to switch to .NET, and use a Constructor Node, but I can't find "shell.application" anywhere.  Which Assembly should I be looking inside?  (I tried browsing directly to System32/shell32.dll and SysWOW64/shell32.dll, but they wouldn't read).

 

[I've hit this issue, trying to find Assembly/Object, before.  Is there a searchable list anywhere?]

 

I tried LabVIEW 2017 for the first program, and 2015 for the second (both SP1).  I'm on Windows 10 Pro.

 

Thanks.

___________________
CLD, CPI; User since rev 8.6.
0 Kudos
Message 1 of 19
(4,170 Views)

Thanks, wiebe.  Unfortunately, I have a few customers that either won't accept 3rd-party programs, even if I can supply source code or won't want to have to use yet another language.  I think I could get away with a PowerShell script, especially one that is only 2 lines.

 

I'm still hoping to find some way to instantiate a Shell.Application with .NET.

___________________
CLD, CPI; User since rev 8.6.
0 Kudos
Message 3 of 19
(4,117 Views)

The problem is invoking a verb on a folder. Invoking a verb (open, print) on a file is no problem at all (ShellExecuteEx, or .net Process.start). There just doesn't seem to be a decent way to do it, except with powershell. I do think .net has a powershell class, but I haven't looked in to it.

0 Kudos
Message 4 of 19
(4,111 Views)

Thanks.  I did like you suggested, and took a step backward to Win32 DLL style.  It does work for some verbs (Open, PrintAt, PinToHome), but not others (Eject, OpenWith, PinToStart, Delete).  So that's what I'll use for one problem.

CreateQuickAccess.png

I'm not certain I got the parameters 100% right.  But if I change the verb to "Open" (case insensitive), the last parameter does affect the window it pops up (5=norm; 3=max), so I'm sure I got it close enough.

 

I also ran across a blog on a Microsoft forum that shows how to run a PowerShell script from C#, which might help, but I run into the same problem of trying to find which Assembly various constructors are hidden in.  It says to "Runspace runspace = RunspaceFactory.CreateRunspace()".  I'm guessing that is in System.Management.Automation.Runspaces, but I've run out of time, so need to set this bug aside for later.  Somehow, it doesn't seem to even mention PowerShell anywhere.

 

I think a .VI that runs PowerShell would be very helpful again & again, so I plan to come back to this, and post a result.

 

___________________
CLD, CPI; User since rev 8.6.
0 Kudos
Message 5 of 19
(4,105 Views)

I think the PowerShell .NET implementation should look something like this:PowerShell dotNET.png

But I get a AmbiguousMatchException....

0 Kudos
Message 6 of 19
(4,093 Views)

From your example, I can see that I was getting too wrapped up in looking for constructors to use the LabVIEW Constructor node.  I looked for a class-method for "New-Object -comObject Shell.Application", but didn't find one.

 

I've tried one variation after another on the code above (from wiebe), but I can't seem to get anything except Error #1172 (System.InvalidOperationException: This operation is only valid on generic types.) when the Invoke[PowerShell.Invoke] executes.

 

I tried creating a Runspace for it, and setting the ps.Runspace to that.  I tried changing to script to something trivial ("$a = 1").  I tried using PowerShell.AddScript vs. PowerShell.AddCommand.  And I tried translating the following (from Microsoft documentation) into LabVIEW.  I tried wiring the 'ref' into the [Invoke] from the ref output of the [AddScript] instead of the 'AddScript' output from the [AddScript] (they appear to be 2 different refnums to the same object).  Every case, I get the same error.

]  Runspace rs = RunspaceFactory.CreateRunspace();
]  PowerShell ps = PowerShell.Create();

]  ps.Runspace = rs;
]  ps.AddCommand("Get-Process").AddArgument("idle");
]  ps.AddStatement().AddCommand("Get-Service").AddArgument("audiosrv");
]  ps.Invoke();

 

The Microsoft help on PowerShell:Invoke says that it can only throw the 'InvalidOperationException" if either the PowerShell is already running (I don't believe it is; I tried adding ps.Stop), or if it has no commands (I believe it does).  The "only valid on generic types" in the error doesn't make sense to me, unless it's just the error message generator that gets confused between .NET and LabVIEW.

 

It could be my Windows 10 Pro that's the problem (I'm sure that's the main cause), but of course, asking Microsoft to fix something is a waste of time.

 

___________________
CLD, CPI; User since rev 8.6.
0 Kudos
Message 7 of 19
(4,086 Views)

Yes I know this is from 2018.

 

Do you have the Widows 10 policy set correctly so that you can execute scripts? Can you execute this script in the powershell ISE? Perhaps you need to change your policy. Run PowerShell ISE as admin and run the command > get-executionPolicy

 

if it is not unrestricted run command> set-ExecutionPolicy unrestricted

 

Norm

 

 

 

0 Kudos
Message 8 of 19
(2,953 Views)

Thanks.  I think I'll have time to try that out today.  The job where I needed 'Eject' is closed out.  But the 'PinToHome' would still be good.  And I really want a general solution for all future jobs, too.

___________________
CLD, CPI; User since rev 8.6.
0 Kudos
Message 9 of 19
(2,910 Views)

hello

    i want to calculate the sha1 by powershell in vi,how can i get the feedback; i have achieved it by cmd,but the time by this method is too long;

thanks

0 Kudos
Message 10 of 19
(2,577 Views)