2

I am using a PowerShell file. I have a file inside the same folder, path can change but folder remains same.

How to use relative path, so there is no change in script?

Param([string]$servername, [string]$path)
#Param([string]$instance)

#$path1="d:\sysdba\"
#$path2= $path1 "+" $path

try {
    Invoke-Expression -Command "d:\sysdba\File_Creation.ps1 $servername"
} catch {
    Write-Host -BackgroundColor Red -ForegroundColor White "Fail"

    $errText =  $Error[0].ToString() 

    if ($errText.Contains("network-related")) {
        Write-Host "Connection Error. Check server name, port, firewall."
    }
    Write-Host $errText 
    continue 
}

instead of

Invoke-Expression -Command "d:\sysdba\File_Creation.ps1 $servername"

I want to use like .\file_creation.ps1 but it's not working.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
deepti
  • 729
  • 4
  • 17
  • 38
  • 1
    As a side-note: avoid `Invoke-Expression`. It's almost always the wrong tool. In your case you should use the call operator instead: `& "$PSScriptRoot\File_Createion.ps1" $servername`. – Ansgar Wiechers Aug 14 '17 at 08:40

0 Answers0