Update configSource of XML element in web.config using Powershell by passing in Parameters - asp.net

I am trying to figure out a way to update my web.config for different environments by updating the configSource for the appSettings element in the web.config.
Here are the way I know how to do it.
$xml.get_DocumentElement().appSettings.configSource = $replaced_test
The problem is that I want one base script where I can pass in different nodes to the script that I want to change and update but I am not sure how to do it.
For example, I want to be able to call a powershell script like this
changeWebConfig.ps1 nodeToChange newValueofNode
I hope this was clear enough.
This is the code I have now.
$webConfigPath = "C:\web.config"
# Get the content of the config file and cast it to XML
$xml = [xml](get-content $webConfigPath)
#this was the trick I had been looking for
$root = $xml.get_DocumentElement()."system.serviceModel".client.configSource = $replace
# Save it
$xml.Save($webConfigPath)
The problem I was having was the configuration node
I had to change it from
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
this to
<configuration>
I am not sure how to find the node with the configuration node in it's orginal state yet, but I'm getting closer.
function Set-ConfigAppSetting
([string]$PathToConfig=$(throw 'Configuration file is required'),
[string]$Key = $(throw 'No Key Specified'),
[string]$Value = $(throw 'No Value Specified'))
{
if (Test-Path $PathToConfig)
{
$x = [xml] (type $PathToConfig)
$node = $x.SelectSingleNode("//client[#configSource]")
$node.configSource = $Value
$x.Save($PathToConfig)
}
}
set-configappsetting "c:\web.config" CurrentTaxYear ".\private$\dinnernoworders" -confirm

Finally figured it out.
$root = $xml.get_DocumentElement().SelectSingleNode("//client[#configSource]").configSource = "test"
of course, I will replace "//client[#configSource]" with a variable so I can pass in different nodes as parameters to create my base script.

Im looking for a way to modify the code as well.
Here is a way you can view whats the node:
$path = 'c:\site\web.config'
$PublishState = (Select-Xml -Path $path -XPath "configuration/appSettings/add[#key='PublishState']/#value").Node.'#text'
$PublishState

Related

Key binding by file type

Description
I'm trying to create a key binding that behaves differently based on the file type.
Ideally what id like to do is the following:
If the file type is .md then run the command markdown-preview-plus:toggle
else run the command script:run
I know it's something along the lines of:
file init.coffee :
editor.command('custom:command', e => {
if ( of file type .md) {
markdown-preview-plus:toggle
} else {
script:run
}
})
Then in the keymap.cson i have to add something like:
'atom-text-editor':
'cmd-i': 'custom:command'
But obviously this is pseudocode. I've tried reading the documentation specifically this
but there isn't enough information.
I was able to do this by adding the following to the keymap.cson file:
"atom-text-editor[data-grammar='source gfm']":
'cmd-i': 'markdown-preview-plus:toggle'
"atom-text-editor:not([data-grammar='source gfm'])":
'cmd-i': 'script:run'
For anyone trying to do something similar to this, I used this as reference:
Atom grammer syntax

How do I retrieve embedded XML values using MSXML in AutoItV3?

I am trying to use AutoItV3 to automate the insertion of some Entities into a piece of software.
It will be far easier if my automation can read information from an xml file and use this to generate my entities, as I can then parse in different xml files for different tests.
I am using a popular extension MSXML to try and do this. This can be found here:
https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=44418
My XML is a relatively simply structure where I will have various fields under each 'Entity' within all of my 'Entities'
<?xml version="1.0" encoding="UTF-8"?>
<entities>
<entity>
<name>
Mation Jr, Mr Auto
</name>
<legalname>
Mr Auto Mation Jr
</legalname>
</entity>
<entity>
<name>
Mation Sr, Mr Auto
</name>
<legalname>
Mr Auto Mation Sr
</legalname>
</entity>
</entities>
In my script header I am importing the MSXML au3 file and setting the XML path
#include <_MSXML.au3>
; Set the XML file
$xmlpath = #ScriptDir & "\Entity.xml"
My Question is, how can I iterate through the attributes of each Entity within all Entities?
This is what I have so far, but i am not understanding how I would retrieve values from an individual entity listed under the Entities node:
; Fetch All Entities from XAML
$ENTITIES = _MSXML_SelectNodes($oXml, "entities/entity")
If ($ENTITIES[0] > 0) Then
; This part works and will iterate for x amount of entities provided
; Fetch Entity as pos $i
For $i = 1 To $ENTITIES[0] Step 1
; How can I iterate through attributes from ENTITIES[$i] ??
Next
Else
MsgBox(4096, 'Error', 'No entity was provided')
EndIf
I understand my question is quite broad but I think there should be enough information to start with
This issue with that UDF is that it seems to want to return strings for everything instead of xml objects which are more useful. I would avoid it and instead just just use the com object yourself with $oXml = ObjCreate("Msxml2.DOMDocument") and then have a look at the documentation here.
But anyways, I think this code will get you what you want:
; Set the XML file
$xmlpath = #ScriptDir & "\Entity.xml"
$oXml = ObjCreate("Msxml2.DOMDocument")
$oXml.load($xmlpath)
; Fetch All Entities from XAML
$objNodeList = $oXml.selectNodes("entities/entity")
For $node in $objNodeList
ConsoleWrite($node.nodename & #CRLF)
$objChildNodeList = $node.selectNodes("*")
For $ChildNode in $objChildNodeList
ConsoleWrite(#TAB & $ChildNode.nodename & ' = ' & $ChildNode.text & #CRLF)
Next
Next
Notice how there is really no need to use a UDF and you can just use the com object's built in methods. In my opinion, this is simpler than using the UDF.
Another thing in general that is worth mentioning is that if you ever have trouble figuring out how to do anything in autoit you can try searching for how to do that same thing in vba or vbs since the languages are pretty similar and autoit can use all the com objects that are used in vba/vbs. When vba/vbs does something like this Set oXml = CreateObject("Msxml2.DOMDocument") just do this in autoit: $oXml = ObjCreate("Msxml2.DOMDocument").

SQLite: isolating the file extension from a path

I need to isolate the file extension from a path in SQLite. I've read the post here (SQLite: How to select part of string?), which gets 99% there.
However, the solution:
select distinct replace(column_name, rtrim(column_name, replace(column_name, '.', '' ) ), '') from table_name;
fails if a file has no extension (i.e. no '.' in the filename), for which it should return an empty string. Is there any way to trap this please?
Note the filename in this context is the bit after the final '\'- it shouldn't be searching for'.'s in the full path, as it does at moment too.
I think it should be possible to do it using further nested rtrims and replaces.
Thanks. Yes, you can do it like this:
1) create a scalar function called "extension" in QtScript in SQLiteStudio
2) The code is as follows:
if ( arguments[0].substring(arguments[0].lastIndexOf('\u005C')).lastIndexOf('.') == -1 )
{
return ("");
}
else
{
return arguments[0].substring(arguments[0].lastIndexOf('.'));
}
3) Then, in the SQL query editor you can use
select distinct extension(PATH) from DATA
... to itemise the distinct file extensions from the column called PATH in the table called DATA.
Note that the PATH field must contain a backslash ('\') in this implementation - i.e. it must be a full path.

How can i replace url from css

I'm trying to combine multiple css files into a single file (from many different folder in my host, and including external css files). I use the following short code:
Declare array:
$styles=array(
array(
'name'=>'style1.css',
'path'=>'http://mydomain.com/css/'
),
array(
'name'=>'camera.css',
'path'=>'http://mydomain.com/js/camera/'
),
array(
'name'=>'style3.css',
'path'=>'http://external.com/assets/css/'
));
Get content and replace url:
foreach ($styles as $style) {
echo preg_replace('/url\(\s*[\'"]?\/?(.+?)[\'"]?\s*\)/i', 'url('.$style['path'].'$1)', file_get_contents($style['path'].$style['name']));
}
After combined into one css file, i have some css background image url as follows:
url(ttp://mydomain.com/css/../image/background.png) //Internal path - Case 1A
url(http://mydomain.com/js/camera/../../image/background.png) //Internal path - Case 1B
url(http://external.com/assets/css/../../image/background.png) //External path - Case 2
Actual, the internal path (Case 1A, 1B) can display the background image (despite the lack of professionalism), but in the external path (Case 2) cannot display the background image, my question is:
How can i replace wrong path with correct path (REPAIR BASE ON CURRENT RESULTS) as:
url(http://mydomain.com/image/background.png) //Correct internal path
url(http://external.com/image/background.png) //Correct external path
(I understand the problem is if find a keyword containing '../' will remove keyword and remove 1 string before the keyword contain '*/', , but I can't figure out how to do this).
Thanks in advance.
(I have tried find out and tried searching for 1 week before ask new question).
additionally replace /up-level-directory/../ with /
that is '/([^\/]+)/../' with / globally
$correct_url = preg_replace('|/\w+/\.\./|', '/', $url);

Remove .js file works but a warning persist

i would like to remove a .js file from a drupal module to override this .js one in my theme.
So, in my MYTHEME_preprocess_page() function I added the following code:
//Delete logintoboggan.js and replace by our own in .info file
$tmpJs = drupal_add_js();
if (isset($tmpJs['module']['sites/all/modules/logintoboggan/logintoboggan.js'])) unset($tmpJs['module']['sites/all/modules/logintoboggan/logintoboggan.js']);
$vars['scripts'] = drupal_get_js($tmpJs);
This for, but a warning message appear on the website:
warning: Illegal offset type in isset or empty in /home/mykitxen/public_html/example.com/includes/common.inc on line 2210.
What is wrong?
You're missing the first parameter of drupal_get_js() ($scope), which should be 'header' or NULL in your case (with NULL just triggering the default 'header'). So the last line of your code should be:
$vars['scripts'] = drupal_get_js('header', $tmpJs);
or
$vars['scripts'] = drupal_get_js(NULL, $tmpJs);
NOTE: This assumes Drupal 6. If you are using Drupal 7, you might want to take a look at hook_js_alter().

Resources