VMware Cloud Community
admin
Immortal
Immortal
Jump to solution

Powershell Function

I am trying to write a function that calls an exe. The function is as follows:

function ShowView {
param([string]$name) #Name Parameter

& E:/scripts/clientaccess.exe -sid 000000001234 show view $name

}

It is called later i the script thru this:

foreach ($i in $get_view){
            ShowView "$i"
        }

And I get the error:

At Z:\script_v2.ps1:21 char:2
+ & <<<<  clientaccess.exe -sid 000000001234 show view $name
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

For some reason it will not interpolat $name properly if it is placed after view, however if I put $name on it's own line and comment out the exe line then it will interpolate $name properly. Also if I type a valid value in place of the $name parameter the function runs fine. I cannot understand why putting $name at the end of the exe line does not work.

Any ideas?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can you try to use the Invoke-Expression cmdlet to run the external command instead of the ampersand ?

The difference is that Invoke-Expression first parses the text and will resolve your $name variable.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Why do you have the ampersand in front of the command ? Afaik this is not needed.

Did you try without the ampersand ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

Yes I have tried it both ways and it has the same behavior.  With or without the &.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

From where do you run your script ?

From the PowerShell ISE, from PowerGui, from the PowerCLI prompt ?

There seem to be some problems when the EXE writes to stderr and you're executing the script in ISE.

See the StackOverflow post callled Ignoring an errorlevel != 0 in Windows Powershell


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

I receive the error from my original post thru PowerGUI and I receive:

No view could be found for the specified input parameters

from the PowerCLI prompt. So i get the error in both places.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think the message you get when you run from the PowerCLI prompt is a message from clientaccess.exe.

Apparently the parameters you passed are not the ones the command expects.

At least from the PowerCLI prompt you get the correct error message and not the NativeCommandError you get when you run the script from PowerGUI.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

LucD,

My mistake I did not include the entire error when run in PowerGUI:

<

At Z:\script_v2.ps1:21 char:2
+ & <<<<  clientaccess.exe -sid 000000001234 show view $name
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError


No Views could be found for the specified input parameters

>

So the PowerGui does state the same error as cli as well as additional information. It is definatley chocking on the $name variable which should be a legitimate view name e.g.; My_View

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try to use the Invoke-Expression cmdlet to run the external command instead of the ampersand ?

The difference is that Invoke-Expression first parses the text and will resolve your $name variable.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

LucD,

As usual you come thru again. Worked just like I wanted. I wish I could afford to put you on retainer.

Thank you 

Reply
0 Kudos