我有以下任务,我想知道最好和最快的方法是什么。我在考虑编写脚本而不是C#应用程序,但在DOS下苦苦于编写脚本。我想使用powershell但不确定所有机器是否都安装了PowerShell:
我有大约15台Windows机器,在每台机器上,我想找到所有* .config文件(比如说C:\和D :)并替换一个特定的单词(db name)。
我可以在本地执行此操作或远程执行此操作,因为我可以登录计算机。但困难的部分是脚本。
任何指针都很棒。
感谢。
答案 0 :(得分:1)
Wikipedia has some info on what versions of Windows have PowerShell.
PowerShell 1.0版于2006年针对Windows XP SP2 / SP3,Windows Server 2003和Windows Vista发布.... 2.0版与Windows 7和Windows Server 2008 R2集成,适用于Windows XP Service Pack 3 ,带有Service Pack 2的Windows Server 2003和带有Service Pack 1的Windows Vista。版本3.0与Windows 8和Windows Server 2012集成在一起.Microsoft还为适用于Windows Server 2008的Windows 7 Service Pack 1提供了PowerShell 3 Service Pack 1和带有Service Pack 1的Windows Server 2008 R2。
要在Win 7上安装第3版,您需要WMF 3.0 package
答案 1 :(得分:0)
这是未经测试和破坏性的,所以首先测试它。它旨在搜索C:和D:驱动器和所有子目录并修改所有* .config文件。
它设置为查看c:\ abc和d:\ abc trees atm,因此将一些文件和文件夹放在那里并进行测试。
@echo off
for /f "delims=" %%a in ('dir c:\abc\*.config d:\abc\*.config /b /s /a-d') do (
call :sar "%%a" "%%a.tmp" "db_name" "new_db_name"
move /y "%%a.tmp" "%%a" >nul
)
goto :EOF
:sar
:: inputfile outputfile regexp_search replacement
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
>_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs
goto :EOF