Automation

 View Only
  • 1.  Powershell Function

    Posted Feb 09, 2011 09:05 PM

    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?



  • 2.  RE: Powershell Function

    Posted Feb 09, 2011 09:10 PM

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

    Did you try without the ampersand ?



  • 3.  RE: Powershell Function

    Posted Feb 09, 2011 09:21 PM

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



  • 4.  RE: Powershell Function

    Posted Feb 09, 2011 09:41 PM

    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



  • 5.  RE: Powershell Function

    Posted Feb 09, 2011 09:58 PM

    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.



  • 6.  RE: Powershell Function

    Posted Feb 09, 2011 10:25 PM

    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.



  • 7.  RE: Powershell Function

    Posted Feb 09, 2011 10:36 PM

    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



  • 8.  RE: Powershell Function
    Best Answer

    Posted Feb 09, 2011 10:53 PM

    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.



  • 9.  RE: Powershell Function

    Posted Feb 09, 2011 11:52 PM

    LucD,

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

    Thank you