How to escape # character in AHK? - autohotkey

I am trying to output some text with hot string. All work well except when using the # character. It produces unexpected results.
:*:.ef::C# Entity framework{Space}
I tried to escape it with ` char, with no success.

!, {, }, # etc, are special keys that must be enclosed in braces to be sent:
:*:.ef::C{#} Entity framework{Space}
or
If you have AHK v1.1.27+ use the T option for the hotstring:
:*T:.ef::C# Entity framework ` ; to send a space at the end add an ` char after it

Related

Powershell replace command not removing newline

I have text that prints out like this:
mdbAppText_Arr: [0]: The cover is open. {goes to next line here}
Please close the cover. and [1] Backprinter cover open
46
I tried getting rid of the newline after open., and it's still there. Any idea of a better way or fix for what I'm doing? I need to get rid of the newline because it's going to a csv file, and messing up formatting (going to newline there).
This is my code:
$mdbAppText_Arr = $mdbAppText.Split("|")
$mdbAppText_Arr[0].replace("`r",";").replace("`n",";").replace("`t",";").replace("&",";")
#replace newline/carriage return/tab with semicolon
if($alarmIdDef -eq "12-7")
{
Write-Host "mdbAppText_Arr: [0]: $($mdbAppText_Arr[0]) and [1] $($mdbAppText_Arr[1]) "
[byte] $mdbAppText_Arr[0][31]
}
I've been looking at:
replace
replace - this one has a link reference to lookup in the asci table, but it's unclear to me what column the byte equivalent is in the table/link.
I'm using PowerShell 5.1.
-replace is a regex operator, so you need to supply a valid regular expression pattern as the right-hand side operand.
You can replace most newline sequences with a pattern describing a substring consisting of:
an optional carriage return (\r? in regex), followed by
a (non-optional) newline character (\n in regex):
$mdbAppText_Arr = $mdbAppText_Arr -replace '\r?\n'

Unrecognized escape sequence \P while parsing the data using powershell

I am using below properties and powershell script to parse the data from properties file. But it is failing while trying to read backslash character "\" in the datasource string. I tried to fix it by adding additional backslash in the properties file. But I am looking for the solution to handle it in powershell instead of making any changes in the properties file.
#env.properties value
datasource=SERVER.SUBDOMAIN.SUBDOMAIN2.DOMAIN.TLD\PWDFC1;initialcatalog=SAA;Trusted_Connection=True;MultiSubnetFailover=True
#Powershell Script
$EnvProps='C:\Apps\SAA\config\env.properties'
$AppConfig= 'C:\Apps\SAA\config\app.config'
$ReadProps = convertfrom-stringdata ([IO.File]::ReadAllText($EnvProps))
foreach($key in $ReadProps.Keys)
{
$value = $ReadProps[$key]
(Get-Content $AppConfig) -replace "##$key##", $value | Set-Content $AppConfig
}
Error while running above script
INFO 2021-08-30 14:22:37 Executing command ...
INFO 2021-08-30 14:22:37
ERROR 2021-08-30 14:22:53 convertfrom-stringdata : parsing "data
source=SERVER.SUBDOMAIN.SUBDOMAIN2.DOMAIN.TLD\PWDFC1;initial
ERROR 2021-08-30 14:22:53 catalog=SAA;Trusted_Connection=True;MultiSubnetFailover=True" -
Unrecognized escape sequence \P.
You need to escape backslashes in the data string. Per the documentation:
ConvertFrom-StringData supports escape character sequences that are allowed by conventional machine translation tools.... the cmdlet can interpret backslashes (\) as escape characters.... instead of the PowerShell backtick character (`) that would normally signal the end of a line in a script. Inside the here-string, the backtick character does not work.
Further mentioned in the link above, it supports escape sequences which are used by regular expressions. You can escape your string prior to putting it through ConvertFrom-StringData by using the [regex]::Escape(string) static method. This will automatically escape any expression tokens in your data so they are parsed literally.
You also need separate each key and value so they fit on their own line, though this is something you will run into once you resolve the error above. Alternatively, you can split on the semi-colon ; prior to running the string through ConvertFrom-StringData. This is also mentioned in the cmdlet documentation.
In the end, the data string you pass to ConvertFrom-StringData should look like this:
datasource=SERVER.SUBDOMAIN.SUBDOMAIN2.DOMAIN.TLD\\PWDFC1
initialcatalog=SAA
Trusted_Connection=True
MultiSubnetFailover=True
Edit
I missed this on the first go, but you also need to remove the semi-colons ; from the string before using ConvertFrom-StringData. This won't cause any errors, but the ; will be included as part of the value, which is likely undesirable. I have updated the sample data above to reflect this.

Can't set extra empty lines for prompt in PowerShell

I've read in the docs for PSReadLineOption that I can amend empty lines below the prompt to separate output from the next input. So I've tried the following.
Set-PSReadLineOption -ExtraPromptLineCount 3
As far I can tell, there's not empty lines appearing and I'm uncertain if I'm doing it wrong, if I'm imagining the result differently than intended or whatever is up with this.
I believe what you're trying to accomplish can be done in a simpler way. At the end of your output, just write a newline "`n" to stdout.
Write-Host "`n"
Sequences such as `n which use the back tick ` which is the PowerShell escape character, and a letter to make an escape sequence. These are called special characters. In the specific case of `n, it represents a newline. In the docs I linked, it lists the escape sequences that you can use within PowerShell, to implement these special characters.

Escape $ in powershell

I use the following to quickly login to RDP:
echo "Connecting to 192.168.1.100"
$Server="192.168.1.100"
$User="Administrator"
$Password="Admin$Password"
cmdkey /generic:TERMSRV/$Server /user:$User /pass:$Password
mstsc /v:$Server
Unfortunately AdminPassword does contain '$' character which breaks the thing. Is there a way to make it work?
Instead of using " " who take $x for a variable. Use ' ' and everything in it will be a character.
You can use the backtick character ` (to the left of the 1 key on US keyboards) to escape special characters - see About Special Characters.
This escapes the dollar sign as well, so your code would become:
$Password="Admin`$Password"

How to get around using apostrophe in double quotes with Powershell

I am working with Powershell. My issue is that my file path (which does not exist on a local computer) has an apostrophe in it. Powershell is seeing this as a single quote, so it is giving me the following error: The string is missing the terminator: '. I thought that I could escape the single quote using a backtick, but that gave me the same error.
The error does not occur when I am doing the first line of code, and I don't even need the backtick for that part. I can even see that the contents of the variable matches up with the file path that I am using. It is only when I am doing the invoke-expression part that it is giving me the error.
I am using https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-expression?view=powershell-7, so I don't think the second line of the code is the problem.
My code is listed below:
$code = "\\example\example\John_Doe`'s_Folder\example.ps1"
invoke-expression -command $code
I have also tried wrapping the entire file path in double-quotes and single-quotes, but my program did not like that either. I can't remove the apostrophe as we have over a hundred of systems that are directing to John_Doe's_Folder.
Invoke-Expression should generally be avoided; definitely don't use it to invoke a script or external program.
In your case, simply use &, the call operator to invoke your script via the path stored in variable $code (see this answer for background information), in which case the embedded ' needs no escaping at all:
$code = "\\example\example\John_Doe's_Folder\example.ps1"
& $code
As for what you tried:
"\\example\example\John_Doe`'s_Folder\example.ps1" turns into the following verbatim string content:
\\example\example\John_Doe's_Folder\example.ps1
That is, the ` was removed by PowerShell's parsing of the "..." string literal itself, inside of which ` acts as the escape character; since escape sequence `' has no special meaning, the ` is simply removed.
For the ` to "survive", you need to escape the ` char. itself, which you can do with ``:
"\\example\example\John_Doe``'s_Folder\example.ps1"