[英]Path SQLSERVER:\7za.exe does not exist?
我在powershell腳本中有以下代碼。 我在腳本的同一目錄中有7za.exe
文件。
param($sql)
$temp = [System.IO.Path]::GetTempFileName()
Invoke-Sqlcmd -ServerInstance sqlserver1 $sql | ConvertTo-Csv > $temp
.\7za.exe a '$temp.zip' '$temp'
但是,它出現以下錯誤。 在invoke-sqlcmd
之后立即在同一文件夾中執行可執行文件的正確方法是什么?
SQL Server PowerShell provider error: Path SQLSERVER:\7za.exe does not exist. Please specify a valid path. + CategoryInfo : OperationStopped: (:) [], GenericProviderException + FullyQualifiedErrorId : Microsoft.SqlServer.Management.PowerShell.GenericProviderException + PSComputerName : localhost
如果您查看錯誤消息中的路徑,它是“SQLSERVER:\\ 7za.exe” - 所以運行此腳本的PowerShell實例的當前工作目錄(您可以使用自動變量$ pwd獲取)是SQLSERVER :\\,這是由SQL模塊\\ snap-in創建的PSDrive,必須加載它才能運行Invoke-SqlCmd cmdlet。
默認情況下,PowerShell腳本不會將其當前位置用作當前工作目錄。 但是,在PowerShell版本3及更高版本中,您可以使用自動變量$ PSScriptRoot獲取腳本的目錄。 在早期版本中,您可以自己生成$ PSScriptRoot:
$PSScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
然后您可以在腳本中使用此變量,如下所示:
& (Join-Path $PSScriptRoot '7za.exe') "$temp.zip" "$temp"
請注意,我在$ temp變量周圍使用了雙引號,因此PowerShell會自動將變量擴展為正確的名稱。
@jbsmith關於使用什么路徑是正確的。 導致此問題的原因是SQLPS模塊將當前目錄更改為SQLSERVER:導入時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.