none
Relative path with debugging in ISE RRS feed

  • Question

  • If I put a script and a text file in the same directory the following works

    MyPSScript.ps1:

    Get-Content "Mytextfile.txt" 

    But if I run is in ISE I get an error : Cannot retrieve the dynamic parameters for the cmdlet. SQL Server PowerShell provider error: Path {..} does not exist. Please specify a valid path. This is even though I have saved the .ps1 file in the same directory as the file.

    If I go to a PS prompt in the folder the script "MyPSScript.ps1" work as expected. I can cook the file like this:

    Get-Content "$(split-path $psISE.CurrentFile.FullPath)\Mytextfile.txt"

    It now works in ISE but not normal powershell.

    I want to write a script in ISE, test and then run from the prompt when I've ironed out the bugs. Is that so much to ask? 

    Monday, October 26, 2015 11:10 AM

Answers

  • If you are loading SQLPS then it will change your path.

    You can do this in the script:

    Before loading SQLPS:

    pushd

    load module

    popd

    pushd/popd will save and restore the current location.

    Note how the following works:

    PS C:\scripts> pushd
    PS C:\scripts> import-module sqlps
    PS SQLSERVER:\> popd
    PS C:\scripts>
    


    \_(ツ)_/

    • Marked as answer by ArmZox Tuesday, October 27, 2015 1:37 PM
    Tuesday, October 27, 2015 10:44 AM

All replies

  • You have to start PowerShell ISE from the directory the script is in.

    In ISE type at the prompt "$pwd"

    Look at the current folder.

    Start ISE in the script folder.


    \_(ツ)_/

    • Proposed as answer by Ruud BorstMVP Monday, October 26, 2015 1:13 PM
    Monday, October 26, 2015 1:04 PM
  • I can see where your coming from. I guessed it had something to do with where powershell.exe was located. I've changed the "start in" property to the folder with the code in,  but the $pwd is still "SQLSERVER:\" in ISE.

    I like your solution - if it worked for me - but it means any shortcut need altering for wherever your code is - thanks again Microsoft.

    I've also ran powershell_ise.exe from the dos command (oh the irony) from my code path and the ISE $PWD still insists on being "SQLServer:\"

    Tuesday, October 27, 2015 8:56 AM
  • If you are loading SQLPS then it will change your path.

    You can do this in the script:

    Before loading SQLPS:

    pushd

    load module

    popd

    pushd/popd will save and restore the current location.

    Note how the following works:

    PS C:\scripts> pushd
    PS C:\scripts> import-module sqlps
    PS SQLSERVER:\> popd
    PS C:\scripts>
    


    \_(ツ)_/

    • Marked as answer by ArmZox Tuesday, October 27, 2015 1:37 PM
    Tuesday, October 27, 2015 10:44 AM