header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Relative paths?
Last Post 26 Jul 2010 01:08 PM by 0ptikGhost. 6 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
jamietUser is Offline
New Member
New Member
Posts:12
Avatar

--
22 Jul 2010 03:55 AM
    Hi,
    A powershell noob here...

    I have a Powershell script that is calling Invoke-Sqlcmd and passing in a file to be executed using its -InputFile property. i.e.:
    Invoke-Sqlcmd -ServerInstance . -database MyDB -InputFile ".\Script.sql"

    Script.sql is stored in the same place as my Powershell script.

    When I run the Powershell script I get an error:
    Invoke-Sqlcmd : Could not find file 'C:\Windows\System323\Script.sql'

    Clearly the relative path ".\Script.sql" is not behaving like I thought it would.

    How do I reference a path stored in the same folder using a relative path? Surely this can be done, can't it?

    Is there a solution to this? Something that works in POSH v1 would be preferable.

    thanks
    Jamie
    jamietUser is Offline
    New Member
    New Member
    Posts:12
    Avatar

    --
    22 Jul 2010 04:02 AM
    Ah, looks like
    Get-Location
    does what I need!
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    22 Jul 2010 04:31 AM
    Get-Location is [System.Environment]::CurrentDirectory, which is what you don't want. You want this:

    $scriptDirectory = Split-Path -Path $script:MyInvocation.MyCommand.Path -Parent
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    22 Jul 2010 04:39 AM
    You can use the resolve-path cmdlet:

    -InputFile (resolve-path .\Script.sql).Path

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    jamietUser is Offline
    New Member
    New Member
    Posts:12
    Avatar

    --
    22 Jul 2010 04:48 AM
    nice, thank you guys.

    And out of interest, what's the problem with [System.Environment]::CurrentDirectory? Its doing what I expect it to.
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    22 Jul 2010 04:52 AM
    Its fine as long as you use Set-Location to set the correct path, otherwise its gets the location of the current working directory, which could be anything.
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    26 Jul 2010 01:08 PM

    PowerShell has a current location for each PSDrive it knows about. Some of these are not even filesystems. [System.Environment]::CurrentDirectory gets the current directory for the powershell.exe process rather than the current PSDrive's location.

    Using Get-Location only fixes part of the problem because it depends on the currently selected PSDrive. What is the path to your script if by some reason you are called from HKLM:\Software\Microsoft? Using Get-Location or Resolve-Path will likely result in something similar to HKLM:\Software\Microsoft\Script.sql which is clearly wrong. If your powershell script and script.sql exist in C:\Temp\Scripts but they user is currently in C:\Users\username, then using Get-Location will still not find the script because it results in C:\Users\username\script.sql.

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer