Getting Double Quotation Marks in Write-Host Output - powershell

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.

Related

Pass string value having space to another powershell file

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.

remove or ignore double quotes in a string - powershell

I think I have a simple problem but I can't seem to find the answer.
This is a part of the string I am working with
$text = "INDEPENDENT ELECTORAL AND BOUNDARIES COMMISSION
POLLING STATION: "ABC DEF GHIJKL (001)"
STREAM:123"
When I try to work with this, I get an error because of the double quotes in $text. I know I can escape the double quotes using a back tick, but the entire string is too big for me to go through it all.
I wonder if there is a simple way to ignore or remove all double (and single, too) quotes except for the first and last.
You can solve this by using here-strings:
$Text = #'
All the symbols I/ can ` hope for
between " the *>$ opening $() and closing ${}
of the symbols
'#
Use this: #' ... '#
Your string would look like this:
$text = #'
"INDEPENDENT ELECTORAL AND BOUNDARIES COMMISSION
POLLING STATION: "ABC DEF GHIJKL (001)"
STREAM:123"
'#

Break a replace string in powershell

I am using powershell to automaticly add lines of code to certain scripts. See example below:
$a -replace '<div class="ef-column1 bodyContent" id="column1">', '<div class="ef-column1 body-content" id="column1"> #RenderSection("ColumnMainHeader", false)'
The #RenderSection part should be on a new line. So i tried to add 'n in front of #RenderSection but this will create 'n#RenderSection instead of putting #RenderSection on a new line.
I also tried 'r'n#RenderSection, but this has the same effect. putting 'n between " " will work neither.
The issue is with the single quote at the front making the grave a literal one rather than escaping the 'n.
Try using double quotes and then escaping all of the double quotes in the expression:
$a -replace '<div class="ef-column1 bodyContent" id="column1">', "<div class=`"ef-column1 body-content`" id=`"column1`"> `n#RenderSection(`"ColumnMainHeader`", false)"

Use variable with quotes with system in MATLAB

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 '''']

Powershell syntax - Passing value with double quote

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