How do I pass double quotes as a parameter in powershell? For example, I need to execute this line but K="Key words" has to be in double quote
$Ie.Navigate2("http://inside.nv.com/demo/Search/Pages/results_Table.aspx?k="Boiling Point"(CreatedBy:Broussard AND Write>=6/1/2015 AND Write<=6/30/2015)", 0x10000)
Try to escape the " which is part of the parameter string with
`
so, in your case it will be like below -
$Ie.Navigate2("http://inside.nv.com/demo/Search/Pages/results_Table.aspx?k=`"Boiling Point`"(CreatedBy:Broussard AND Write>=6/1/2015 AND Write<=6/30/2015)", 0x10000)
Use encoding. The (") character would correspond to %22.
https://en.wikipedia.org/wiki/Percent-encoding
Related
I have a powershell script which calls another powershell file passing a string argument.
param (
[string]$strVal = "Hello World"
)
$params = #{
message = "$strVal"
}
$sb = [scriptblock]::create(".{$(get-content $ps1file -Raw)} $(&{$args} #params)")
Somehow the script passes message variable without double quotes so the powershell file receives only the first part of the message variable (before space) i.e. "Hello".
How can I pass the strVal variable with space (i.e. "Hello World")?
A double quote pair signals PowerShell to perform string expansion. If you want double quotes to output literally, you need to escape them (prevent expansion) or surround them with single quotes. However, a single quote pair signals PowerShell to treat everything inside literally so your variables will not interpolate. Given the situation, you want string expansion, variable interpolation, and literal double quotes.
You can do the following:
# Double Double Quote Escape
#{message = """$strVal"""}
Name Value
---- -----
message "Hello World"
# Backtick Escape
#{message = "`"$strVal`""}
Name Value
---- -----
message "Hello World"
Quote resolution works from left to right, which means the leftmost quote type takes precedence. So '"$strVal"' will just print everything literally due to the outside single quote pair. "'$strVal'" will print single quotes and the value of $strVal.
I want to run the following PowerShell script file from Jenkins Pipeline:
".\Folder With Spaces\script.ps1"
I have been able to do it with the following step definition:
powershell(script: '.\\Folder` With` Spaces\\script.ps1')
So I have to remember to:
escape the backslash with a double backslash (Groovy syntax)
escape the space with backtick (PowerShell syntax)
I would prefer to avoid at least some of this. Is it possible to avoid using the backtick escaping, for example? (Putting it between "" does not seem to work, for some reason.)
I found that it's possible to use the ampersand, or invoke, operator, like this:
powershell(script: "& '.\\Folder With Spaces\\script.ps1'")
That gets rid of the backtick escaping, and should make life a tiny bit easier.
To avoid escaping the backslashes you could use slashy strings or dollar slashy strings as follows. However you cannot use a backslash as the very last character in slashy strings as it would escape the /. Of course slashes as well would have to be escaped when using slashy strings.
String slashy = /String with \ /
echo slashy
assert slashy == 'String with \\ '
// won't work
// String slashy = /String with \/
String dollarSlashy = $/String with / and \/$
echo dollarSlashy
assert dollarSlashy == 'String with / and \\'
And of course you'll lose the possibility to include newlines \n and other special characters in the string using the \. However as both slashy and dollar slashy strings have multi line support at least newlines can be included like:
String slashyWithNewline = /String with \/ and \
with newline/
echo slashyWithNewline
assert slashyWithNewline == 'String with / and \\ \nwith newline'
String dollarSlashyWithNewline = $/String with / and \
with newline/$
echo dollarSlashyWithNewline
assert dollarSlashyWithNewline == 'String with / and \\ \nwith newline'
If you combine that with your very own answer you won't need both of the escaping.
I need to get some double quotations around the GUID=$ntds output. I have tried encompassing the entire string in double quotes to no avail. Single quotes won't work because of the variable.
$Site=Read-Host "ENTER SITE NAME"
$Server=Read-Host "ENTER SERVER NAME"
$NTDS=Get-ADObject -Identity "CN=NTDS Settings,CN=$server,CN=Servers,CN=$site,CN=Sites,CN=Configuration,$((Get-ADDomain).DistinguishedName)" |foreach {$_.objectguid}
write-host "Repadmin /showobjmeta" * "<GUID=$ntds>"
You can use another pair of double quotes to escape like
Write-Host "hello ""200mg"""
Which will output hello "200mg"
Your case it would be
write-host "Repadmin /showobjmeta""<GUID=$ntds>"""
I do not know powershell, but from other languages, you can try:
you can try using double-double quote "" where you want to have ".
"<GUID=""$ntds"">"
or you can try single quotes if permitted on the outside
'<GUID="$ntds">'
or you can try the backslash as escape character before the " making it
"<GUID=\"$ntds\">"
Just try and let me know if you succeeded.
I have
myVar.value = 123521#machine OK
now I'm using this variable with system command as it's an argument passed to a binary .exe
so I have to add quotes to myVar.value as it caontains spaces
I tried :
'''myVar.value''' but this will give 'myVar.value', whereas I just want to have the result equal to "123521#machine OK"
how could I use the quotes in this case ?
Try this:
x = ['"' myVar.value '"']
I think you can use double quote characters within strings demarcated by single quotes. Within a string demarcated by single quotes characters by doubling up:
x = ['''' myVar.value '''']
I can't get to include quotation marks in the following string interpolator:
f"foo ${math.abs(-0.9876f)*100}%1.1f%%"
The output is
foo 98.8%
Now the desired output is
foo "98.8%"
Inserting \" doesn't work, only produces "unclosed string literal" errors.
Seems that this problem wouldn't be fixed. You can use one of the following workarounds:
multi-line strings:
f"""foo "${math.abs(-0.9876f)*100}%1.1f%""""
\042:
f"foo \042${math.abs(-0.9876f)*100}%1.1f%\042"
${'"'}:
f"foo ${'"'}${math.abs(-0.9876f)*100}%1.1f%${'"'}"