I am writing a powershell script which reads a CSV which includes path where Web.config/App.config resides applications. The script simple tries to encrypt the configuration files. A snippet of the code is as:
foreach ($config in $configs) {
$rootPath = Get-Location
$directory = Join-Path -Path $rootPath -ChildPath $config.GetPath()
if (Test-Path -Path $directory) {
$configPath = Join-Path $directory -ChildPath $config.GetOriginalConfig()
if (![System.IO.File]::Exists($configPath)) {
Write-Host "$configPath was not found."
return
}
# A set of helper codes
Try {
cd $directory
# Invoke-Command $moveToDirectory
aspnet_regiis -pef connectionStrings . -prov CustomProvider
}
Catch {
Write-Host $$_.Exception.Message
}
}
}
The problem here is that I have 5 configuration path but only the first one runs and the application exists. It seems that aspnet_regiis
exists the program in both success or failure case. What can I do to make it run in a loop?
Well through some hit and trials, I was able to breakdown the problem. The loop was stopped to run because of: cd $directory
command. My guess is that as the problem is commanded to change the directory, the script is not in its current scope. I am a newbie so this is just my guess. So I changed a line of code to:
aspnet_regiis -pef connectionStrings "$directory" -prov CustomProvider
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.