Capture text between two different char. Between the first{ and the last } - powershell

Capture text between two different char using PowerShell. Between the first{ and the last } . Basically there is text with Json in it and I want to capture the json from it. I have looked for examples but so far no luck.
PROJECT Description: Azure Test Project Description
PROJECT ADMINISTRATORS: jjohnson
CONTRIBUTORS: jdoe
BOARD PROCESS: Agile
SPECIAL INSTRUCTIONS:
{
"organization": "https://dev.azure.com/cloudops",
"projectName": "Test Project",
"projectDescription": "Azure Test Project Description",
"projectProcessType": "Agile",
"specialInstructions": "",
"adminMembers": [
{
"userSamAccountName": "jjohnson",
"userEmailAddress": "jjohnson#test.com",
"userPrincipalName": "jjohnson#test.com",
"projectGroupType": "projectAdministrator"
}
],
"contribMembers": [
{
"userSamAccountName": "jdoe",
"userEmailAddress": "jdoe#test.com",
"userPrincipalName": "jdoe#test.com",
"projectGroupType": "projectContributor"
}
]
}

Is this what you were looking for?
[Regex]::Match((Get-Content "sampleinputfile.txt" -Raw),
'^{.+}',
[Text.RegularExpressions.RegexOptions]::Multiline -bor
[Text.RegularExpressions.RegexOptions]::Singleline).Value
Basically this converts the input file to a single (newline-delimited) string (Get-Content -Raw), and then uses the .NET Framework's Match method to do a regular expression match for the lines of text between the { and } characters (inclusive).

Related

Images are not displayed after publishing the bot with continuous deployment

I have developed a bot using Bot Framework. It shows images when I run it locally on the Bot Framework Emulator. But after publishing it on Azure by creating a private GitHub repository, none of the images are displayed. I'm using Direct-Line API 3.0. What could be the reason?
Like, here's the tree-view of my project. The images folder resides in Content Root.
I'm using the following static method to retrieve an image path.
public static string CreateImagePath(string filename)
{
return Path.Combine(TempData.HostingEnvironment?.ContentRootPath, "images", $"{filename}");
}
And then converting it into a Uri, and assigning it to an Adaptive Card's image. Here's the code snippet.
(AdaptiveCardFactory.CreateAdaptiveElement(card, "image") as AdaptiveImage).Url =
new Uri(PathFactory.CreateImagePath("welcome_card_image.png"));
CreateAdaptiveElement is defined as:
public static AdaptiveElement CreateAdaptiveElement(AdaptiveCard card, string adaptiveElementId)
{
return card.Body.Find(ae => ae.Id == adaptiveElementId);
}
This is the Uri that is returned when I run the bot locally. it works fine.
file:///C:/Users/Husai/source/repos/PanjaSahibBot/images/welcome_card_image.png
Here's what is returned after publishing.
file:///D:/home/site/wwwroot/images/welcome_card_image.png
And, it's not working.
Here's the latest command log.
Command: "D:\home\site\deployments\tools\deploy.cmd"
Handling ASP.NET Core Web Application deployment.
Restoring packages for D:\home\site\repository\GurdwaraBot.csproj...
Generating MSBuild file D:\home\site\repository\obj\GurdwaraBot.csproj.nuget.g.props.
Generating MSBuild file D:\home\site\repository\obj\GurdwaraBot.csproj.nuget.g.targets.
Restore completed in 5.02 sec for D:\home\site\repository\GurdwaraBot.csproj.
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 1.41 sec for D:\home\site\repository\GurdwaraBot.csproj.
GurdwaraBot -> D:\home\site\repository\bin\Release\netcoreapp2.2\GurdwaraBot.dll
GurdwaraBot -> D:\local\Temp\8d6da8df6d390f3\
Creating app_offline.htm
KuduSync.NET from: 'D:\local\Temp\8d6da8df6d390f3' to: 'D:\home\site\wwwroot'
Copying file: 'GurdwaraBot.deps.json'
Copying file: 'GurdwaraBot.dll'
Copying file: 'GurdwaraBot.pdb'
Copying file: 'GurdwaraBot.runtimeconfig.json'
Copying file: 'images\01d.png'
Copying file: 'images\01n.png'
Copying file: 'images\02d.png'
Copying file: 'images\02n.png'
Copying file: 'images\03d.png'
Copying file: 'images\03n.png'
Copying file: 'images\04d.png'
Copying file: 'images\04n.png'
Copying file: 'images\09d.png'
Copying file: 'images\09n.png'
Copying file: 'images\1.jpg'
Copying file: 'images\10.jpg'
Copying file: 'images\10d.png'
Copying file: 'images\10n.png'
Copying file: 'images\11d.png'
Copying file: 'images\11n.png'
Copying file: 'images\13d.png'
Copying file: 'images\13n.png'
Copying file: 'images\2.jpg'
Copying file: 'images\3.jpg'
Copying file: 'images\4.jpg'
Copying file: 'images\5.jpg'
Copying file: 'images\50d.png'
Copying file: 'images\50n.png'
Copying file: 'images\6.jpg'
Copying file: 'images\7.jpg'
Copying file: 'images\8.jpg'
Copying file: 'images\9.jpg'
Copying file: 'images\bill.png'
Copying file: 'images\feedback.png'
Copying file: 'images\Festivals.jpg'
Copying file: 'images\festivals_bandi_chhor_divas.jpg'
Copying file: 'images\festivals_gurpurab.jpeg'
Copying file: 'images\festivals_hola_mohalla.jpg'
Copying file: 'images\festivals_maghi.jpg'
Copying file: 'images\festivals_martyrdom_of_guru_arjan.jpg'
Copying file: 'images\festivals_parkash_utsav_dasveh_patshah.jpg'
Copying file: 'images\festivals_vaisakhi.jpg'
Copying file: 'images\menu_card_image.png'
Copying file: 'images\panja_thumbnail_1.png'
Copying file: 'images\panja_thumbnail_2.png'
Copying file: 'images\panja_thumbnail_3.png'
Copying file: 'images\pof_guest_hotel.jpg'
Copying file: 'images\question.png'
Omitting next output lines...
Finished successfully.
This is what I am expecting.
Locally executed
And, this is what I'm getting via DirectLine!!
Actual result
Here's the content of my welcome_card.json
{
"type": "AdaptiveCard",
"body": [
{
"type": "Image",
"id": "image",
"horizontalAlignment": "Center",
"style": "Person",
"url": "",
"size": "Medium"
},
{
"type": "TextBlock",
"horizontalAlignment": "Center",
"size": "Medium",
"weight": "Bolder",
"color": "Accent",
"text": "Welcome to Gurdwara Bot",
"wrap": true
},
{
"type": "TextBlock",
"horizontalAlignment": "Left",
"text": "Hi there! I'm the Sri Panja Sahib's support bot. What can I help you with today? Just so you know, you can enter **MENU** anytime you want to go back to the options below, or just go ahead type your query.",
"wrap": true
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Get an Overview",
"data": {
"dataId": "overview"
}
},
{
"type": "Action.Submit",
"title": "Ask a Question",
"data": {
"dataId": "question"
}
},
{
"type": "Action.Submit",
"title": "Book a Room",
"data": {
"dataId": "room"
}
},
{
"type": "Action.Submit",
"title": "Give a Feedback",
"data": {
"dataId": "feedback"
}
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0",
"speak": "Hi there! I'm the Sri Panja Sahib's support bot. What can I help you with today? Just so you know, you can enter **MENU** anytime you want to go back to the options below, or just go ahead type your query."
}
I've defined AdaptiveCardFactory to manipulate with cards.
public class AdaptiveCardFactory
{
public static AdaptiveCard CreateAdaptiveCard(string path)
{
try
{
var adaptiveCardJson = File.ReadAllText(path);
AdaptiveCardParseResult result = AdaptiveCard.FromJson(adaptiveCardJson);
return result.Card;
}
catch (AdaptiveSerializationException)
{
throw;
}
}
public static Attachment CreateAdaptiveCardAttachment(AdaptiveCard card)
{
return new Attachment
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = card,
};
}
public static AdaptiveElement CreateAdaptiveElement(AdaptiveCard card, string adaptiveElementId)
{
return card.Body.Find(ae => ae.Id == adaptiveElementId);
}
}
TL;DR;
You must provide public URLs for items displayed in AdaptiveCards, not local ones. It is working in local with the emulator because... you are in local, and the emulator has access to your local files, so it can understand files with local location (C:\... or D:\... files).
So change your path so that it uses your sub-domain: you can do it here, for example by using a configuration variable instead of TempData.HostingEnvironment?.ContentRootPath
public static string CreateImagePath(string filename)
{
return Path.Combine(TempData.HostingEnvironment?.ContentRootPath, "images", $"{filename}");
}
Details:
AdaptiveCard item is passed as an attachment in the bot message, everything that is inside is rendered by the client (channel), so it can only render things that are "visible" from the outside.
See my demo here: I used a Card with an ImageSet of 2 images: one with an URL over the web, the other with an URL build like yours.
Local: both images are OK.
Published: only 1st image is displayed
If you look at the Inspector on the right-side, it becomes obvious that you must change the way you build your image Url.
You can also have a look to your deployed webchat by right-clicking the zone where the image should be, and you will find a link with a D:\... path
Code Fix
1st, move your images folder inside wwwroot folder of your projet. Then you can do the following:
public string CreateImagePath(string filename)
{
if (_hostingEnvironment.IsDevelopment())
{
return Path.Combine(TempData.HostingEnvironment?.ContentRootPath, "wwwroot", "images", $"{filename}");
}
else
{
var targetUri = new Uri(new Uri($"https://{Environment.ExpandEnvironmentVariables("%WEBSITE_SITE_NAME%")}.azurewebsites.net"), Path.Combine("images", $"{filename}"));
return targetUri.ToString();
}
}

PropertyParams when deploying VM from OVF

I am using the VMWare vCenter REST API to deploy new Virtual Machines from OVF library items. Part of the API allows for additional_paramaters but I am unable to get it to function properly. Specifically, I would like to set the PropertyParams for custom OVF template properties.
When deploying VM from OVF, I am using the following REST API:
POST https://{server}/rest/com/vmware/vcenter/ovf/library-item/id:{ovf_library_item_id}?~action=deploy
I have tried many structures and either end up with the POST succeeding but the parameters completely ignored, or with a 500 Internal Server error with a message about failing to convert the properties structure:
Could not convert field 'properties' of structure 'com.vmware.vcenter.ovf.property_params'
The payload that seems correct from the documentation (but fails with the error above):
deployment_spec : {
/* ... */
additional_parameters : [
{
type : 'PropertyParams',
properties : [
{
id : 'my_property_name',
value : 'foo',
}
]
}
]
}
Given an OVF that contains the following:
<ProductSection>
<Info>Information about the installed software</Info>
<Product>MyProduct</Product>
<Vendor>MyCompany</Vendor>
<Version>1.0</Version>
<Category>Config</Category>
<Property ovf:userConfigurable="true" ovf:type="string" ovf:key="my_property_name" ovf:value="">
<Label>My Property</Label>
<Description>A custom property</Description>
</Property>
</ProductSection>
This also fails for other property types such as boolean.
Note that I have posted on the vCenter forums as well.
I had the same issue, i success to solve it by browsing the vapi structure /com/vmware/vapi/metadata/metamodel/structure/id:<idstructure>
Here is my finding :
firstly, get your properties structure by using the filter api :
https://{{vc}}/rest/com/vmware/vcenter/ovf/library-item/id:300401a5-4561-4c3d-ac67-67bc7a1a6
Then, to deploy, use the class com.vmware.vcenter.ovh.property_params. It will be more clear with the exemple :
{
"deployment_spec": {
"accept_all_EULA": true,
"name": "clientok",
"default_datastore_id": "datastore-10",
"additional_parameters": [
{
"#class": "com.vmware.vcenter.ovf.property_params",
"properties":
[
{
"instance_id": "",
"class_id": "",
"description": "The gateway IP for this virtual appliance.",
"id": "gateway",
"label": "Default Gateway Address",
"category": "LAN",
"type": "ip",
"value": "10.1.2.1",
"ui_optional": true
}
],
"type": "PropertyParams"
}
]
}

Escape $ in snippet

I'm trying to escape the $ character in my snippets but I cannot seem to get it correct.
If I use \$ as it looks like I should be from the documentation, I get the error:
file: path/to/snippets/php.json'
severity: 'Error'
message: 'Invalid escape character in string'
And no $ appear in my snippet.
If I use $$ found from this answer, I get a $ to show up but it thinks that the text immediately following is a tabstop.
If I use $\ it works but I have to have a character that is part of an escape sequence immediately following. So if I wanted $factory, I would need to do $\ffactory. \f seems to be the best as it does not effect the layout of my snippet.
I'm pretty sure that I am missing what needs to be done here.
My snippet for reference:
"factory" :{
"prefix": "factory",
"body": [
"\$factory->define($1, function (Faker\\Generator \$faker){",
"\treturn [",
"\t\t$2,",
"\t];",
"}"
],
"description": "Creates Model factory"
},
After much trial and error, I found that using \\$ will give me the desired results. So if I wanted $test in my snippet output, with test not being a tabstop, I would need \\$test snippet definition body:
"factory" :{
"prefix": "factory",
"body": [
"\\$factory->define($1, function (Faker\\Generator \\$faker){",
"\treturn [",
"\t\t$2,",
"\t];",
"});"
],
"description": "Creates Model factory"
},

Azure: Build templates: how to extract defaultStorageAccount

I am trying to create a custom image in my Azure DevTestLab, from a vhd image stored in the default storage account.
I started modifying the file at the GitHub repo for ARM templates for creating a custom image from a vhd.
I have already created the DevTest lab, and I can run the following Powershell command to get its data:
PS C:\git> $rid = Get-AzureRmResource -ResourceGroupName jgTl9-rg -ResourceType Microsoft.DevTestLab/labs -ResourceName jgTl9 -ApiVersion 2016-05-15
This yields a bunch of information. If I want to extract the default storage account name, I do the following:
PS C:\git> $rid.properties.defaultStorageAccount
So I know it's possible to get it. Now I have modified the variables section of the Azure build template (a JSON file) to look as follows:
"variables": {
"resourceName": "[concat(variables('existingLabName'), '/', variables('imageName'))]",
"resourceType": "Microsoft.DevTestLab/labs/customimages",
"existingLabName": "[replace(resourceGroup().name,'-rg','')]",
"storageAccountID": "/subscriptions/xxxxxxxxxxxx/resourceGroups/jgTl9-rg/providers/Microsoft.Storage/storageAccounts/djgtl91795",
"saidFrags": "[skip(split(variables('storageAccountID'), '/' ), add(length(split(variables('storageAccountID'), '/' )),-1) ) ]",
"storageAccountSuffix": "[take(variables('saidFrags'),1)[0]]",
"existingVhdUri": "[concat('https://',variables('storageAccountSuffix'),'.blob.core.windows.net/uploads/',parameters('imageFileName'))]",
"imageName": "JaysImage"
},
This code actually works when I call the New-AzureRmResourceGroupDeployment command with the azuredeploy.json and azuredeploy.parameters.json.
However, instead of hardcoding the storageAccountID variable, I'd like to extract the defaultStorageAccount from the environment. I tried something like this:
"storageAccountID": "[string( resourceId(resourceGroup().name,'MicrosoftDevTestLab/labs','jgTl9').properties.defaultStorageAccount ))",
I tried a few variations on this but nothing worked. What is the syntax I need to use to extract the same information as I have hardcoded for the storageAccountID?
(I could not find anything in Microsoft's description of template functions).
Couple of issues with your following expression
"storageAccountID": "[string( resourceId(resourceGroup().name,'MicrosoftDevTestLab/labs','jgTl9').properties.defaultStorageAccount ))",
There is syntax issue ie. an extra ) at the end.
The resource provider MicrosoftDevTestlab/labs should be Microsoft.DevTestlab/labs
When resourceId is used it will not give you the entire object. So when you do properties.defaultStorageAccount it will not work. Because it gives you just the string of your dev test lab and nothing else and there is no property on a string of defaultStorageAccount.
Now what you need is reference function like below
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
},
"variables": {
"vDefaultStorageAccount": "[string(resourceId('yourResourceGroupName','Microsoft.DevTestLab/labs','yourTestLabName'))]"
},
"resources": [],
"outputs": {
"defaultStorageAccount":{
"value": "[reference(variables('vDefaultStorageAccount'),'2016-05-15').defaultStorageAccount]",
"type":"string"
}
}
}
In the variable, vDefaultStorageAccount we get the ID of the DevTestLab and then we use reference function to get the object. Then you can do . to get properties. You don't need to do properties.defaultStorageAccount for example.

GCS Transfer via Source: URL list "errorCode": "UNKNOWN"

I'm trying to transfer 7,860,379 files, using the transfer system via URL list, however always encounter the same error:
{ //...
"errorBreakdowns": [
{
"errorCode": "UNKNOWN",
"errorCount": "1",
"errorLogEntries": [
{
"url": " or ",
"errorDetails": [
""
]
}
]
}
]
// ...
}
All the my URLs are valid and the file format as documented:
TsvHttpData-1.0
^([^ ]+)\t([0-9]+)\t([a-f0-9]{32})$
The error I find the API is very generic, someone went through the same problem?
Since, I thank you.
Based on your regex, I suspect you are not providing a base-64 encoded MD5, as it often contains '=' characters. To do this, you need to compute the binary version of your MD5 and then convert it to base64.
Example: Hk2gdsIpWTDz3kQssoTqKg==