Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Wednesday, June 12, 2013

Search and replace using PowerShell

 

Last week we were faced to a problem: there are numerous files distributed in hierarchy of folders on the disk and we are required to replace a pattern string in each of these files to a specified replacement string. (In fact, we had to add quickly hardwired location parameters to all the AJAX ModalPopupExtender controls in an ASP.NET solution).

The challenge is: the files cannot be copied outside of the target machine, processed and copied back – all the processing MUST occur on the target box using only on-board tools. And there aren’t any built-in software in Windows (Server 2008 R2) providing such functionality like S&R, Visual Studio or other similar tools, enabling recursive search and replace operations.

Some of us started to wipe dust off their DOS command line skills and write some batch files. But the better solution is in use of built-in PowerShell.

Start PowerShell and launch following command:

dir –r {fileseach pattern}| %{ $x = get-content $_ ; $x = $x -replace ({search pattern}, {replacement pattern}) ; set-content $_ $x }

For example:

dir -r *.aspx| %{ $x = get-content $_ ; $x = $x -replace ("ModalPopupExtender ID", "ModalPopupExtender X=""100"" Y=""100"" ID") ; set-content $_ $x }

replaces string

ModalPopupExtender ID

with

ModalPopupExtender X="100" Y="100" ID

in all .aspx files in current folder and below

This makes from

<ajax:ModalPopupExtender ID=”mpe1” …/>

the string

<ajax:ModalPopupExtender X="100" Y="100" ID=”mpe1” …/>

So all the processing occurred on the target box without any need to copy the files to equipped machine, process them and copy back. Also without any need to install/deploy additional software to the target machine specially to perform desired processing.

Next time you need to replace some string with another one in a number of files distributed in recursive subfolders using only built-in software of Windows OS – do not panic: use pattern PowerShell command from above and enjoy!

Sunday, January 20, 2013

Using CoreConfig on Windows Hyper-V Server 2012

 

Microsoft Hyper-V Server 2012 is a great opportunity to virtualize the whole IT environment for low cost (free download, free-of-charge activation – as for today).

As for core server there aren’t any window based management tools, the administrator may want to learn and exercise console and powershell commands. Alternatively, there’s an administration package named CoreConfig at codeplex: http://coreconfig.codeplex.com/

After copy to the target box one may encounter problems starting the configuration tools. The reason is based on changed command set for Windows Server 2012 and described here:

http://coreconfig.codeplex.com/discussions/397446

So, once you downloaded the content of the package to your fresh installed Hyper-V Server 2012 box, the next step is: find “oclist” string and replace it by “dism” in following files:

  • Start_CoreConfig.wsf
  • CoreConfig.ps1
  • Networking.ps1
  • Roles.ps1

After the files are changed you can start the Start_CoreConfig.wsf as usual (as on Hyper-V Server 2008 R2) – or launch powershell and start single modules.

If you get an error message

"File cannot be loaded because the execution of scripts is disabled on this system"

you’ll need to set execution policy to unrestricted with following powershell command:

Set-ExecutionPolicy Unrestricted

After the steps are done you should be able to start the configuration modules as expected:

image

Enjoy!