我无法弄清楚我做错了什么。我希望它读取 web.config 文件查找(< serverName >、ServerName 或 localhost)并替换为计算机名,然后再次保存文件。我还想将驱动程序字母更改为脚本所在的当前驱动器。我知道我的代码很乱,但我根本没有脚本编写技巧。因此,如果您对清理它有任何建议,那将有所帮助。
$env:CD = (get-location).drive.root;
$env:CL = get-location
Get-Content ($env:cl\wwwroot\GBMain\Web.config) | ForEach-Object { $_ -replace "<serverName>", "$env:COMPUTERNAME" } | Set-Content ($env:cl\wwwroot\GBMain\Web.config)
Get-Content ($env:cl\wwwroot\GBMain\Web.config) | ForEach-Object { $_ -replace "ServerName", "$env:COMPUTERNAME" } | Set-Content ($env:cl\wwwroot\GBMain\Web.config)
Get-Content ($env:cl\wwwroot\GBMain\Web.config) | ForEach-Object { $_ -replace "localhost", "$env:COMPUTERNAME" } | Set-Content ($env:cl\wwwroot\GBMain\Web.config)
Get-Content ($env:cl\wwwroot\GBMain\Web.config) | ForEach-Object { $_ -replace "D:\", "$env:cd" } | Set-Content ($env:cl\wwwroot\GBMain\Web.config)
Get-Content ($env:cl\wwwroot\GBMain\Web.config) | ForEach-Object { $_ -replace "C:\", "$env:cd" } | Set-Content ($env:cl\wwwroot\GBMain\Web.config)
Get-Content ($env:cl\wwwroot\GBReporting\Web.config) | ForEach-Object { $_ -replace "<serverName>", "$env:COMPUTERNAME" } | Set-Content ($env:cl\wwwroot\GBReporting\Web.config)
Get-Content ($env:cl\wwwroot\GBReporting\Web.config) | ForEach-Object { $_ -replace "ServerName", "$env:COMPUTERNAME" } | Set-Content ($env:cl\wwwroot\GBReporting\Web.config)
Get-Content ($env:cl\wwwroot\GBReporting\Web.config) | ForEach-Object { $_ -replace "localhost", "$env:COMPUTERNAME" } | Set-Content ($env:cl\wwwroot\GBReporting\Web.config)
Get-Content ($env:cl\wwwroot\GBReporting\Web.config) | ForEach-Object { $_ -replace "D:\", "$env:cd" } | Set-Content ($env:cl\wwwroot\GBReporting\Web.config)
Get-Content ($env:cl\wwwroot\GBReporting\Web.config) | ForEach-Object { $_ -replace "C:\", "$env:cd" } | Set-Content ($env:cl\wwwroot\GBReporting\Web.config)
Exit