powershell修改 '.\'并调用.ps1

标签 powershell path environment-variables

我正在尝试调用一个 powershell 脚本,该脚本依次运行各种 .SQL 文件。 它看起来像这样:

脚本1(Invoke_createdb.ps1):

$scriptPath = "C:\scriptpath"
$dbUser = "dbuser1"
$dbPassword = "dbpassword1"
$dbInstance = "dbserver1"
$dbName = "Database1"
& $scriptPath\createdb.ps1 -dbInstance $dbInstance -dbUser $dbUser -dbPassword $dbPassword -dbName $dbName

脚本2(createdb.ps1):

param
(
$dbInstance = $(Read-Host "database server"),
$dbaUser = $(Read-Host "dba user"),
$dbaPassword = $(Read-Host "password of dba user"),
$dbName = $(Read-Host "Database Name")
)

Invoke-Expression "sqlcmd.exe -S $dbInstance -U $dbaUser -P $dbaPassword -i .\create.sql"
Invoke-Expression "sqlcmd.exe -S $dbInstance -d $dbName -U $dbaUser -P $dbaPassword -i .\MSSQLScripts\Create\TABLES.SQL"

$storedProcedures = Get-ChildItem .\MSSQLScripts\Shared\*.sql
foreach ($storedProcedure in $storedProcedures)
{  
Invoke-Expression "sqlcmd.exe -S $dbInstance -d $dbName -U $dbaUser -P $dbaPassword -i $storedProcedure"
}

这在原理上看起来很简单,并且如果第二个脚本单独运行就可以工作。 当第一个脚本调用第二个脚本时,我遇到的问题就会出现,因为“.\”不再是相对的并且会中断。使用 .\.

无法找到任何内容

无法修改“Script2”,因为它需要在其他场景中相对于自身运行,并且会使用其他过程定期更新,但我需要在第一个脚本中调用它。

我尝试修改 $env:PATH$env:ROOT 变量,但没有成功。

有谁知道一种干净的方法,我可以(1)调用第二个脚本,同时(2)设置相对路径,以便它在调用时成功运行?

感谢您的帮助。

编辑:我通过将第二个脚本中每次提及 .\ 修改为明确的 UNC 路径,暂时测试成功,但如上所述,该脚本在运行时不会运行其他环境。

最佳答案

为什么不在调用脚本之前更改当前目录。

...
Push-Location $scriptPath
$scriptPath\createdb.ps1 -dbInstance $dbInstance -dbUser $dbUser -dbPassword $dbPassword -dbName $dbName
Pop-Location

关于powershell修改 '.\'并调用.ps1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20520315/

相关文章:

powershell - 从 IIS 7.5 中删除多个网站

java - 如何为每个 Windows Azure 虚拟机操作系统设置一些唯一标识符

powershell - 搜索邮箱命令无法识别 Exchange 2010 sp1 powershell

iphone - 在 IOS 中保存应用程序文件的最佳位置

python - 同时连接 linux 和 windows 的路径

python - 如何从 Bottle 中的路径获取参数?

bash - 使用环境变量传递参数的缺点

powershell - 在 Powershell 中设置后机器级环境变量不可用

PowerShell 禁用和启用驱动程序

powershell - Azure Powershell : Get public virtual IP of service