Powershell:将文件路径指定为变量 [英] Powershell: Specify file path as variable
问题描述
我通过powershell脚本运行以下SQL查询,并需要针对不同的文件多次运行脚本。所以我想弄清楚的是,当我运行脚本时,如何指定一个文件路径作为一个变量?
更新[$ Db_name]。[dbo]。[$ BatchTable]
set [$ Db_name]。[dbo ] [$ BatchTable] .Wave ='Wave1.1'
from [$ Db_name]。[dbo]。[$ BatchTable]
inner join OPENROWSET(BULK'FilePath\file.csv'
FORMATFILE ='E:\import.xml')作为
on([$ Db_name]。[dbo]。[$ BatchTable] .Name = a.Name)和
[$ Db_name]。[dbo]。[$ BatchTable] .Domain = a.Domain)
'FilePath \file.csv'是我需要定义为一个变量的文件路径,使我的代码改为如下:
inner join OPENROWSET(BULK'$ INPUTFILEPATH',
FORMATFILE ='E:\import.xml')AS a
任何帮助或潜在的更好的方法来完成这将非常有帮助。
从命令像我想要能够像这样运行脚本:
CMD:updatescript。 ps1 $ INPUTFILEPATH = C:\Documents\myfile.csv
同样,我不确定这是最好的办法吗?
你就在那里。
您需要在脚本的开头添加一个参数块,例如
Param(
[Parameter(Mandatory = $ true)]
[ValidateScript({Test-Path $ _ -PathType'leaf'})]
[string] $ InputFilePath
)
这将创建一个强制的(不是可选的)字符串参数 InputFilePath
并且 ValidateScript
是用于验证参数的代码,在这种情况下,使用 Test-Path
cmdlet检查文件是否存在和路径类型(如果检查目录使用'container'
)。
下面的语法:
updatescript.ps1 -INPUTFILEPATHC:\Documents\myfile.csv
并且在脚本中使用该变量作为路径完全符合您的问题:
inner join OPENROWSET(BULK'$ INPUTFILEPATH',
FORMATFILE ='E:\import.xml')AS a
注意:在PowerShell中运行脚本时使用参数,只需使用最少的字符从您的param块中的所有其他参数中唯一标识该参数 - 在这种情况下
-I
工作原理以及-InputFilePath
。I am running the following SQL query through a powershell script and need to run the script multiple times against different files. So what I am trying to figure out is how to specify a file path as a variable when I run the script?
update [$Db_name].[dbo].[$BatchTable] set [$Db_name].[dbo].[$BatchTable].Wave = 'Wave1.1' from [$Db_name].[dbo].[$BatchTable] inner join OPENROWSET(BULK 'FilePath\file.csv', FORMATFILE= 'E:\import.xml') AS a on ([$Db_name].[dbo].[$BatchTable].Name= a.Name) and ([$Db_name].[dbo].[$BatchTable].Domain = a.Domain)
The 'FilePath\file.csv' is the file path I need to define as a variable so that my code would instead look like this:
inner join OPENROWSET(BULK '$INPUTFILEPATH', FORMATFILE= 'E:\import.xml') AS a
Any help or potentially better methods to accomplish this would help very much.
From the command like I want to be able to run the script like this:
CMD: updatescript.ps1 $INPUTFILEPATH = C:\Documents\myfile.csv
Again, I'm not sure this is the best way to go about this?
解决方案You're nearly there. You will need to add a parameter block at the very start of your script e.g.
Param( [Parameter(Mandatory=$true)] [ValidateScript({Test-Path $_ -PathType 'leaf'})] [string] $InputFilePath )
This creates a mandatory (not optional) string parameter, called
InputFilePath
, and theValidateScript
is code used to validate the parameter, in this case checking the file exists using theTest-Path
cmdlet and pathtype of leaf (if checking existence of a directory use'container'
).When running your script use the syntax below:
updatescript.ps1 -INPUTFILEPATH "C:\Documents\myfile.csv"
and in the script use the variable as the path exactly as in your question:
inner join OPENROWSET(BULK '$INPUTFILEPATH', FORMATFILE= 'E:\import.xml') AS a
NOTE: in powershell when using parameters when running a script you only need to use the least amount of characters that uniquely identify that parameter from all the others in your param block - in this case
-I
works just as well as-InputFilePath
.这篇关于Powershell:将文件路径指定为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!