Pergunta

I'm trying to find resources to help me write a powershell script to add text to an existing aspx file but can't seem to find anything. Does anybody have any suggestions or is there anybody that can help me some other way?

Update: So it's out there, I'm using version 1.0.

I have:

$lines = Get-Content foo.aspx

$lines = "<head>asfdsfsafd</head>" + $lines

$lines | Out-File "c:\documents and settings\...\foo.aspx" -Encoding utf8 

but it wipes out everything originally in foo.aspx and creates <head>asfdsfsafd</head><head>asfdsfsafd</head>. How do I fix it so it keeps the original stuff in foo and add stuff to the beginning of the file rather than end?

Update:

I've figured out how to add text with:

$lines = add-content -path "C:\Documents and Settings\..\foo.aspx" -value "Warning...."

but want the text to go at the beginning of the aspx file

Update: I found a function that does what I want and I'm all set.

Foi útil?

Solução

You can get the content (array of strings) of the ASPX file using Get-Content:

$lines = Get-Content foo.aspx

Or you can get the content as a single string which is sometimes more useful if you want to use a regex that spans lines:

$content = Get-Content foo.aspx -raw

As far as changing the content, you have all sorts of options:

$content = "text before " + $content
$content += "text after"
$content = $content -replace 'regex pattern','replacement text'

And then to write back out to the file:

$content | Out-File foo.aspx -Encoding <UTF8 or ASCII or UNICODE>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top