none
powershell RRS feed

  • Question

  • Hi There,

    I am working on a Visual Studio external tool that would allow me to programmatically update .rdl (Reporting Services) files by calling a PowerShell function. Once Ready/working, I should be able to click on the Options menu in VS and see my tool ready for use. The PowerShell function relied upon accepts a mandatory argument (fully qualified file & path name).

    Being new to PowerShell, I am testing the function but it does not appear to work (returning error messages). Can someone look at it and tell me what I am doing wrong ? Could it be the way I am calling from the command line ? (see below):

    Powershell.exe -ExecutionPolicy Bypass C:\Users\fnlm1234\Documents\Personal Project\FixRDLReferences.ps1 "C:\Users\fnlm1234\Desktop\PS rdl test files\ssrssubreportname.rdl"

    Could it be the script ? (see below)

    Function FixRDLReferences {
        
        Param(
                [parameter(Mandatory=$true)]
                [String[]]
                $rdlFileFQN
             )
    
        $oldfile = $rdlFileFQN
        $newfile = $oldfile
        $patternttofind = "<Reportname>"
        $replacementpattern = "<Reportname>./"
    
        $text = (Get-Content -Path $oldfile -ReadCount 0) -join "`n"
        $clean = $text -match $replacementpattern
    
        if ($clean -eq "True"){
        cls
        Write-Host "clean value is: " $clean
        Write-Host "Operation not needed"
        }
    
        else{
        $text -replace $patternttofind, $replacementpattern | Set-Content -Path $newfile
        cls
        Write-Host "clean value is: " $clean
        Write-Host "Operation complete! References were successfully updated."
        }
    }

    I have also included the error log:

    Powershell.exe : C:\Users\fnlm1234\Documents\Personal : The term 'C:\Users\mm3429\Documents\Personal' is not recognized as the name of a 
    At line:1 char:1
    + Powershell.exe -ExecutionPolicy Bypass C:\Users\fnlm1234\Documents\Pers ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (C:\Users\fnlm1234... the name of a :String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     
    cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify 
    that the path is correct and try again.
    At line:1 char:1
    + C:\Users\mm3429\Documents\Personal Project\FixRDLReferences.ps1 C:\Us ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\Users\fnlm1234\Documents\Personal:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    When not running it as a function, the logic does work.

    Thank you for any feedback on this.



    • Moved by Reed KimbleMVP Friday, May 11, 2018 7:03 PM powershell in vb.net
    • Edited by Tshindaye Friday, May 11, 2018 7:15 PM
    Friday, May 11, 2018 5:00 PM

Answers

  • This command won't work:


    Powershell.exe -ExecutionPolicy Bypass C:\Users\fnlm1234\Documents\Personal Project\FixRDLReferences.ps1 "C:\Users\fnlm1234\Desktop\PS rdl test files\ssrssubreportname.rdl"

    You need to use this instead:


    Powershell.exe -ExecutionPolicy Bypass -File "C:\Users\fnlm1234\Documents\Personal Project\FixRDLReferences.ps1" "C:\Users\fnlm1234\Desktop\PS rdl test files\ssrssubreportname.rdl"

    That is, you were missing the -File parameter and the " marks around the script file's name (because it contains one or more spaces).


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Tshindaye Thursday, May 17, 2018 3:03 PM
    Friday, May 11, 2018 9:03 PM
    Moderator
  • You cannot execute a file that contains only a function.  Remove the function wrapper and it will work.  Bill's code is correct and nothing happens because the function is never being called.


    \_(ツ)_/

    • Marked as answer by Tshindaye Thursday, May 17, 2018 2:52 PM
    Thursday, May 17, 2018 1:45 PM

All replies

  • This command won't work:


    Powershell.exe -ExecutionPolicy Bypass C:\Users\fnlm1234\Documents\Personal Project\FixRDLReferences.ps1 "C:\Users\fnlm1234\Desktop\PS rdl test files\ssrssubreportname.rdl"

    You need to use this instead:


    Powershell.exe -ExecutionPolicy Bypass -File "C:\Users\fnlm1234\Documents\Personal Project\FixRDLReferences.ps1" "C:\Users\fnlm1234\Desktop\PS rdl test files\ssrssubreportname.rdl"

    That is, you were missing the -File parameter and the " marks around the script file's name (because it contains one or more spaces).


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Tshindaye Thursday, May 17, 2018 3:03 PM
    Friday, May 11, 2018 9:03 PM
    Moderator
  • Thanks for your feedback Bill.

    Unfortunately, running the command you've suggested does not seem to make it work.

    The strange thing is that running the command below from the PowerShell IDE does work everything. What am I missing here:

    PS C:\Users\fnln1234\documents\PersonalProject> FixRDLReferences("C:\Users\fnlm1234\Desktop\TestFiles\ssrssubreportname.rdl")

    In the snippet above, FixRDLReferences is the name of the PowerShell function and the stuff in quote is the argument.

    The whole point of add this script as an external tool within Visual Studio. Once the user clicks on the tool, it should invoke the PowerShell script, that in turn will wait for the user to input the argument (file path).

    Thank you.

    M. Tshindaye

    Tuesday, May 15, 2018 3:57 PM
  • 1) We don't use ( ) to call a function in PowerShell. The command would be FixRDLReferences "filename".

    2) This question isn't about scripting in general but rather about how to do something in Visual Studio. This is not a Visual Studio forum.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 15, 2018 4:17 PM
    Moderator
  • Good Morning Bill,

    You definitely have a point. Help from Visual Studio forum could certainly come in handy when I get there but for now, the issue still stands: I am not able to get the PowerShell function to run from either the powershell.exe utility or from within a .batch file.

    To make it easier, I have included in the function a prompt to get the user to pass in the full path and name to the file needing processing.

    Thank you for looking into it if availability allows.

    Function FixRDLReferences {
        
        Param(
                [parameter(Mandatory=$true, HelpMessage="Enter path of the .rdl file to process")]
                [ValidateNotNullorEmpty()]
                [ValidateScript({ Test-Path $_ -PathType 'Leaf' })]
                [ValidateScript({ (Get-Item $_ | select -Expand Extension) -eq ".rdl" })]
                [string] $rdlFileFQN
             )
    
        $oldfile = $rdlFileFQN
        $newfile = $oldfile
        $patternttofind = "<Reportname>"
        $replacementpattern = "<Reportname>./"
    
        $text = (Get-Content -Path $oldfile -ReadCount 0) -join "`n"
        $clean = $text -match $replacementpattern
    
        if ($clean -eq "True"){
        cls
        Write-Host "clean value is: " $clean
        Write-Host "Operation not needed"
        }
    
        else{
        $text -replace $patternttofind, $replacementpattern | Set-Content -Path $newfile
        cls
        Write-Host "clean value is: " $clean
        Write-Host "Operation complete! References were successfully updated."
        }
        #pause

    Thursday, May 17, 2018 1:31 PM
  • You cannot execute a file that contains only a function.  Remove the function wrapper and it will work.  Bill's code is correct and nothing happens because the function is never being called.


    \_(ツ)_/

    • Marked as answer by Tshindaye Thursday, May 17, 2018 2:52 PM
    Thursday, May 17, 2018 1:45 PM
  • Great catch!

    You're a life saver. It did it !

    Thank you for your input here.

    Thursday, May 17, 2018 2:53 PM
  • Thanks for your feedback Bill.
    Thursday, May 17, 2018 3:03 PM