CloudFormation Userdata to Base64 Decoding Incorrectly - powershell

I've created a CloudFormation template that creates an instance. I want to pass a PowerShell script into a windows instance via the user data; however, when it's encoded with Base64, the Cloudformation template does not decode it correctly.
I want to pass in this:
Set-DNSClientServerAddress -InterfaceIndex (Get-NetAdapter).InterfaceIndex -ServerAddresses ("172.31.15.30")
When I check the template in the CloudFormation it got decoded as:
Set-DNSClientServerAddress /u2013InterfaceIndex (Get-NetAdapter).InterfaceIndex /u2013ServerAddresses ("172.31.15.30")
When this is passed into the Windows instance, it doesn't recognize /u2013, and errors out.
How do I ensure the line I pass in keeps the dash, rather than decode it into Unicode.

Could you try this?
"Set-DNSClientServerAddress \-InterfaceIndex (Get-NetAdapter).InterfaceIndex \-ServerAddresses ("172.31.15.30")"
Based on others needing to escape other characters: AWS Cloudformation output double quotes in a file using Fn::Join

\u2013 is a different type of unicode dash character. Your editor probably used it because it looks nicer. Try editing your source code with notepad or a simple editor and replace that unicode dash with a proper dash. If you are having a hard time typing it, you can copy it from Set-DNSClientServerAddress. That one seems fine.

Related

How to pass non-english chars in an api post command

I am writing a powershell script and trying to run a post command with an Azure DevOps api, but one of my values contains non-english characters and the Invoke-RestMethod always fails with:
{"$id":"1","innerException":null,"message":"You must pass a valid patch document in the body of the request.","typeName":"Microsoft.VisualStudio.Services.Common.VssPropertyValidationException, Microsoft.VisualStudio.Services.Common","typeKey":"VssPropertyValidationException","errorCode":0,"eventId":3000}
If I remove the non-english chars, it works as expected.
What do I need to do, to get the non-english chars to be accepted in the json body.
Figured it out. Just needed to add ";charset=UTF-8" to my content type.

Encoding issue with Powershell `Get-Clipboard`

I would like to retrieve HTML from the clipboard via the command line and am struggling to get the encoding right.
For instance, if you open a command prompt/WSL, copy the following ⇧Shift+⭾TAB and run:
powershell.exe Get-Clipboard
The correct text is retrieved (⇧Shift+⭾TAB).
But if you then try to retrieve the clipboard as html:
powershell.exe "Get-Clipboard -TextFormatType html"
The following text is retrieved
...⇧Shift+⭾TAB...
This seems to be an encoding confusion on part of the Get-Clipboard commandlet. How to work around this?
Edit: As #Zilog80 indicates in the comments, indeed the encoding of the text does not match the encoding which is assumed the text has. I can rectify in Ruby for instance using:
out = `powershell.exe Get-Clipboard -TextFormatType html`
puts out.encode('cp1252').force_encoding('utf-8')
Any idea for how to achieve the same on the command line?
This is indeed a shortcoming of Get-Clipboard. The HTML format is documented to support only UTF-8, regardless of the source encoding of the page, so the cmdlet should interpret it as such, but it doesn't.
I'm speculating as to the encoding PowerShell is going to be using when decoding the data, but it's probably whatever the system default ANSI encoding is. In that case
[Text.Encoding]::UTF8.GetString([Text.Encoding]::Default.GetBytes( `
(Get-Clipboard -TextFormatType Html -Raw) `
))
will recode the text, but with the caveat that if the default ANSI encoding does not cover all code points from 0-255, some characters might get lost. Fortunately Windows-1252 (the most common default) does cover all code points.

Converting Danish special characters

I'm trying to read a .csv file for my script, but the script contains the special danish characters "æ","ø" and "å". So far my idea was to define it like this:
CellM = readtable(csvdata,'Format','%æ%ø%å%s');
but it's not working.
You can specify the file encoding when you call readtable:
readtable(..., 'Encoding', 'UTF-8');
This should solve your problem, assuming you didn't use another function on csvdata, earlier, which already messed your data up.

formatting text in a csv export

I'm having trouble with a .csv export which is being uploaded to a website. There are must be some hidden or illegal characters in a description field I have in the database. I'm having a tough time getting the text to format correctly and not break a php script.
If I use the GetAs(css) function in a calculation, the text works fine. Obviously this won't work as a working file but it at least validates there's something in the formatting of the description field that's breaking the export. I did use the excel clean(text) calculation and that fixes the issue as well. Just need to find a way in Filemaker to do this.
Any suggestions?? Maybe a custom function that strips out bad characters?
You can filter invalid characters out of text using the filter function. If you only want a minimal set of ASCII characters, use it like
filter(mytable::myfield; "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789.!?")

Apostrophe issue in RTF

I have a function within a custom CRM web application (old VB.Net circa 2003) that takes a set of fields from a database and merges them with palceholders in a set of RTF based template documents. These generate merged letters and documentation. The code essentially loops through each line of the RTF template file and replaces any instances of the placeholder values with text from a database record. The issue I'm having is that users have pasted a certain type of apostrophe into the web app (and therefore into the database) that is not rendering correctly in the resulting RTF file. It is rendering like this - ’.
I need a way to spot this invalid apostrophe in the code and replace it with a valid one. Unfortunately when I paste the invalid apostrophe into the Visual Studio editor it gets converted into the correct one. So I need another way to express this invalid apostrophe's value. Unfortunately I do not know a great deal about unicode and other encodings so I'm calling out for help with this.
Any ideas?
If you really just want to figure out what the character is you might want to try and paste it into a text editor like ultraedit. It has a hex mode that you can flip to to see the actual underlying bytes.
In order to do the replace once you've figured out the character you'd do something like this in Vb,
text.Replace(ChrW(2001), "'")
Note that you might not be able to figure it out easily using the text editor because it might also get mangled by paste from the clipboard. You might want to either print some debug of the ascii values from code. You can use the AscW function to do that.
I can't help but think that it may actually simply be a case of specifying the correct encoding to use when you write out the stream though. Assuming you're using a StreamWriter you can specify it on the constructor. I'm guessing you actually want ASCII given your requirement.
oWriter = New System.IO.StreamWriter(path, False, System.Text.Encoding.ASCII)
It looks like you probably want to encode characters out of the 8 bit range (>255).
You can do that using \uNNNN according to the wikipedia article.