powershell - sqlcmd. How to pass in a variable that includes a colon and a space? -


i've been struggling while now. i'm trying invoke sql script , pass 2 variables using sqlcmd. i'm doing in powershell.

here's i've got:

$time = '12:00 am' $date = '06/20/2014' $result = sqlcmd -u username -p password -i "c:\path\to\script.sql" -v date=$date -v time=$time 

this fails following error message:

sqlcmd : sqlcmd: 'time=12:00 am': invalid argument. enter -? help. 

after experimentation, i've discovered problem colon , space in $time. if remove colon , space $time = '1200am', command executes without error.

unfortunately, script i'm executing wants exact format "12:00 am".

things i've tried didn't work:

$time="12\:00\ am" $time="12\\:00\\ am" $time="12"+":00"+" am" $time="12"+":00" $time="12"+":"+"00" 

these respond similar invalid argument failures. last few attempts solution this similar post. don't work.

i have tried placing string values directly in sqlcmd invocation, so:

$result = sqlcmd -u username -p password -i "c:\path\to\script.sql" -v date=$date -v time="12\:00\ am".  

no dice, , anyways, need read time in somewhere else, need $time variable.

i (finally) found solution worked sqlcmd powershell script. (using invoke-sqlcmd not option me)

i needed pass absolute path containing colon in variable (e.g., c:\rootdir\subdir). worked regular command prompt, couldn't work powershell script. came ugly kludge, passing parts before , after colon in 2 variables, reassembling in sql script.

but failed when path contained space (e.g., c:\root dir\subdir).

so found solution fixed both colons , spaces. involved enclosing path text in double quotes, enclosing double-quoted path text in outer set of single quotes. after building full sqlcmd in variable, looked this:

sqlcmd <other args>  -v rootpath='"c:\root dir\subdir"' 

(that's outer set of single quotes (') , inner set of double quotes (")).

this worked if path didn't have colon, e.g., \\nodename\root dir\subdir. had been problem when tried split path around assumed colon. i'm still not sure why both outer single quotes , inner double quotes necessary, version worked me.

addendum: worked powershell 5, , broke when script run powershell 4. make work on both, found needed enclose internal spaces in single quotes, e.g.,

sqlcmd <other args>  -v rootpath='"c:\root' 'dir\subdir"' 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -