0

我正在编写一个 powershell 脚本,它读取一个 CSV,其中包括 Web.config/App.config 驻留应用程序的路径。脚本 simple 尝试加密配置文件。代码片段如下:

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
    }
  }
}

这里的问题是我有 5 个配置路径,但只有第一个运行并且应用程序存在。似乎aspnet_regiis在成功或失败的情况下都存在程序。我该怎么做才能让它循环运行?

4

1 回答 1

0

好吧,通过一些打击和试验,我能够解决问题。循环因以下原因停止运行:cd $directory命令。我的猜测是,由于命令问题更改目录,因此脚本不在其当前范围内。我是新手,所以这只是我的猜测。所以我将一行代码更改为:

aspnet_regiis -pef connectionStrings "$directory" -prov CustomProvider

于 2019-01-07T08:31:31.317 回答