Create JIT config file using PowerShell – Connect to UAT database from DevBox

5
(3)
# Get the path to the folder where the script is located
$scriptDirectory = $PSScriptRoot

# Define the path to the XML file (assumes the file is in the same folder as the script)
$sourceXmlFilePath = Join-Path -Path $scriptDirectory -ChildPath "web.config"
$devXmlFilePath = Join-Path -Path $scriptDirectory -ChildPath "web_dev $(get-date -f yyyy-MM-dd).config"

# Load the XML file
if (-Not (Test-Path $sourceXmlFilePath)) {
    Write-Host "Error: File 'web.config' not found in the script directory." -ForegroundColor Red
    exit
}




# Prompt for new values
$serverAndDatabase = Read-Host "Enter the JIT server\database"
if ($serverAndDatabase -notmatch "\\") {
    Write-Host "Error: Please provide the input in the format 'ServerName\DatabaseName'." -ForegroundColor Red
    exit
}

# Prompt for other values
$userName = Read-Host "Enter the new JIT user name"
$password = Read-Host "Enter the JIT password"


# Split server/database string
$serverName, $databaseName = $serverAndDatabase -split "\\"


[xml]$xmlContent = Get-Content $sourceXmlFilePath
# Save a copy of the current dev
$xmlContent.Save($devXmlFilePath)
Write-Host "web_dev $(get-date -f yyyy-MM-dd).config created successfully!"


# Update the XML values
$xmlContent.configuration.appSettings.add | Where-Object {
    $_.key -eq "DataAccess.SqlUser"
} | ForEach-Object {
    $_.value = "$userName"
}

$xmlContent.configuration.appSettings.add | Where-Object {
    $_.key -eq "DataAccess.AxAdminSqlUser"
} | ForEach-Object {
    $_.value = "$userName"
}

$xmlContent.configuration.appSettings.add | Where-Object {
    $_.key -eq "DataAccess.Database"
} | ForEach-Object {
    $_.value = "$databaseName"
}

$xmlContent.configuration.appSettings.add | Where-Object {
    $_.key -eq "DataAccess.DbServer"
} | ForEach-Object {
    $_.value = "$serverName"
}

$xmlContent.configuration.appSettings.add | Where-Object {
    $_.key -eq "DataAccess.AxAdminSqlPwd"
} | ForEach-Object {
    $_.value = "$password"
}

$xmlContent.configuration.appSettings.add | Where-Object {
    $_.key -eq "DataAccess.SqlPwd"
} | ForEach-Object {
    $_.value = "$password"
}

# Save the changes back to the original XML file
$xmlContent.Save($sourceXmlFilePath)
Write-Host "web.config updated successfully!"

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 3

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Scroll to Top