Category Archives: Line

Powershell to insert at start of line and end of line

When I was creating the post Facebook Domain Name Sets for ISA I needed to insert data at the start and end of a line.  I used powershell to do this, and this is what I came up with:
Get-Content DNSList.txt | ForEach-Object {“%PREPEND%”+$_+”%APPEND%”}
where:
DNSList.txt contains a list of text;
%PREPEND% is the data to add at the start of the line;
%APPEND% is the data to add to the end of the line.
In the Facebook ISA example, the actual code was (one line):
Get-Content | list.txt | ForEach-Obect {“<fpc4:Str dt:dt=`”string`”>*.”+$_”</fpc4:Str> “}
Notice the backticks infront of the “string” component.  This is to escape the quote marks to allow them to be included.
The output can be pasted into the relevant XML file and imported into ISA.

Run a Powershell script (.ps1) from command line


To run a Powershell script from the command line, batch file or task scheduler, use the following:
powershell -command “. ‘%SCRIPT%'”
Note the double quotes (“) followed by single quotes (‘), and the reverse on the end of the line.
eg:
powershell –command  “. ‘c:maintenancetuesday.ps1′”
To load a custom powershell console, for example the Exchange Management Shell, use the following:
powershell –PSConsoleFile %PSC1% –command “. ‘%SCRIPT%'”
eg:
powershell –PSConsoleFile “c:program filesmicrosoftexchange serverv14binExSchell.psc1” -Command “. ‘c:maintenanceReportMailboxSizeExchange2010.ps1′”

Server 2003 Volume Shadow Copy Command Line (previous versions)

Using Server 2003, with the Server 2003 Resource Kit  (opens new window) installed, you can restore Shadow Copies from the command line with the command volrest.exe

For example, the file in question is:

 \svr2003serverdatasales2013Sales report sep 2013.doc”

Open a command prompt and type:

volrest “\svr2003serverdatasales2013sales report sep 2013.doc”

This gives us a list of the shadow copies available for that document.  Wildcards can also be used. 
EG: we could also have used:

volrest “\svr2003serverdatasales2013*report sep 2013.doc”
volrest “\svr2003serverdatasales2013*report*”
volrest “\svr2003serverdatasales2013*”

The switch /s  searches subfolders as well.  The command below will list all shadow copy files for the share serverdata.

volrest /s “\svr2003serverdata*”

We can see the date time the file was modified, size, and the time the shadow copy was created.

To restore, use the command:

volrest “\svr2003serverdatasales2013sales report sep 2013.doc” /r:\svr2003serverdatasales2013restored”

This will restore all the sales report sep 2013.doc copies available to a subfolder called restored.  If the restored folder doesn’t exist, it will be created.

The files are restored in reverse time order.  Eg: the newest files are created first, and subsequent files are renamed (1), (2), etc.

In the example above, the 287Kb file is called Sales report sep 2013.doc, and the 6Kb file is called Sales report sep 2013(1).doc

Reference: http://technet.microsoft.com/en-us/library/cc780410(v=ws.10).aspx

http://www.windowsdevcenter.com/pub/a/windows/2004/07/20/shadow_copies.html