I am running the following command:
([xml](new-object net.webclient).DownloadString(
"http://blogs.msdn.com/powershell/rss.aspx"
)).rss.channel.item | format-table title,link
The output for one of the RSS items contains this weird text:
You Don’t Have to Be An Administrator to Run Remote PowerShell Commands
So, the question is:
Why the mix up in characters? What happened to the apostrophe? Why is the output rendered as Don’t when it should just render as Don't?
How would I get the correct character in the PowerShell standard output?
You need to set the encoding property of the webclient:
$wc = New-Object System.Net.WebClient
$wc.Encoding = [System.Text.Encoding]::UTF8
([xml]$wc.DownloadString( "http://blogs.msdn.com/powershell/rss.aspx" )).rss.channel.item | format-table title,link
Related
I need to get the path or name of a directory, the things I know about it:
- the folder the directory is on
- its name follows a given regex
How can I accomplish this?
Working on Jenkins pipeline (groovy)
You can apply the pwd pipeline step to return the current directory as String and fetch the name from it by using regex groups.
See the following example to extract values from regex using groovy: https://gist.github.com/EwanDawson/2407215
I ended up assigning the result of powershell to the variable, I did this by telling the powershell step that it has a stdout return like:
def folder = powershell (returnStdout: true, script: """
Get-ChildItem -directory | Select-String -pattern '<pattern>'
""")
That combined with pwd() gives me the full path, although you can also get full path using a pipeline in the command, something like Get-ChildItem -directory | Select-Object Fullname | Select-String -pattern '<pattern>'
Good day Stackoverflow.
As the title says, I have an issue with Doxygen.
Description
A PowerShell script modify the PROJECT_NUMBER variable of my Doxyfile.
Then it runs Doxygen, but it generates the documentation in HTML and LaTeX like it's reading a Default generated Doxyfile.
If I manually modify the Doxyfile before running this script, via Notepad++, Doxygen works perfectly, but once the script is ran, the issue appears.
I would also mention that my Doxyfile has:
GENERATE_HTML = YES
GENERATE_LATEX = NO
GENERATE_MAN = YES
In practice Doxygen behave like this:
.\doxygen.exe -g
\doxygen.exe .\Doxyfile
The bizzarre behaviour begins now!
Let's call my actual Doxyfile CustomConfig and the default generated DefaultConfig.
If I generate a DefaultConfig through .\doxygen.exe -g and then I overwrite its content with the text of CustomConfig via Notepad++, doxygen accepts the Doxyfile, as it should, and generates a correct output!
So the problem is not the Doxyfile content but PowerShell that modifies the file.
I've verified this by doing a simple copy&paste of the entire content:
Copy&Paste through Notepad++: WORK
Copy&Paste through PowerShell: DOESN'T WORK
PowerShell Script
# Replace the old PROJECT_NUMBER with the new one
$DOXY_PATH = $env:FS_OS + "\doc"
$CONFIG_PATH = $DOXY_PATH + "\bin\Doxyfile"
$BIN_PATH = $DOXY_PATH + "\bin\doxygen.exe"
$GIT_PATH = $env:FS_OS
$GIT_BRANCH = "Development"
# Get git commit number on the specified branch
$GIT_HASH = git log $GIT_BRANCH -1 --pretty=format:%H
$PRJ_CONTENT = Get-Content $CONFIG_PATH
$PRJ_NUM = "PROJECT_NUMBER = " + $GIT_HASH
$PRJ_CONTENT = $PRJ_CONTENT -replace "PROJECT_NUMBER\s*=\s*[A-z0-9]{40}",$PRJ_NUM
$PRJ_CONTENT | Out-File -FilePath $CONFIG_PATH
Start-Process -FilePath $BIN_PATH -ArgumentList "$CONFIG_PATH" -WorkingDirectory ($DOXY_PATH + "\bin")
Copy&Paste Script
$var = Get-Content "./doc/bin/Doxyfile.bak"
$var | Out-File -FilePath "./doc/bin/Doxyfile"
Thanks to #BenH for the comment, I've found the solution.
It looks like PowerShell writes to files automatically with BOM.
I've found a solution with the Accepted Answer from this question:
Using PowerShell to write a file in UTF-8 without the BOM
I have post build events set up already for copying files for me dependent on ConfigurationName and want to be able to set an "environment variable" in a config (js) file on client side of an angular application to allow debug info to be visible or not dependent upon environment running application in.
To this end I've created a powershell script (ReplaceText.ps1):
function replace-file-content([String] $path, [String] $replace, [String] $replaceWith)
{
(Get-Content $path) | Foreach-Object {$_ -replace $replace,$replaceWith} | Out-File $path
}
and added this line to the post build event of my web project.
if "$(ConfigurationName)"=="LIVE" (powershell -File "$(ProjectDir)Tools\ReplaceText.ps1" "$(ProjectDir)app\application.config.js" "DEBUG" "LIVE")
which I was hoping to change the word "DEBUG" to "LIVE" when built against LIVE build configuration in my application.config.js file which contains this line:
$provide.constant('currentEnv', 'DEBUG');
Build succeeds but no changes occur on my file. Can anyone identify where I'm going wrong?
I do know I could do this sort of stuff with Gulp or another task runner BTW, but was trying to do it without bringing in another dependency and just use VS post build events & PS. :)
Cheers
Your PS code only defines a function but there's nothing that invokes it.
Use param as the first statement in the script to convert command line into the script's parameters:
param([String] $path, [String] $replace, [String] $replaceWith)
(Get-Content -literalPath $path -raw) -replace $replace, $replaceWith |
Out-File $path -encoding UTF8
-literalPath - correctly handles paths with [] symbols otherwise interpreted as a wildcard;
-raw - reads the entire file as one string for speedup, available since PowerShell 3.
Yesterday I ran the following script on some batch files on our server to replace an individual's email address as she is no longer with the company. When examining the text it worked perfectly and the log file wrote correctly as well. However, now the batch file no longer executes when I double click on it. I see a quick flash of the console window but there does not appear to be any text in it. Adding PAUSE statements is not helpful as it does not seem to execute any of the text in the file.
I copied and pasted the text to a new batch file and it works fine. I noticed that the powershell-edited file is 6KB and the new copied-and-pasted file is 3KB so clearly the script has done something unexpected to the file. Copying and pasting each file obviously defeats the purpose of using a script to batch process things. Any ideas where I'm going wrong?
I've been running the script from my development machine and I have full administrator permissions to everything on our network.
If it matters we are running Windows Server 2008 R2 Enterprise on the server.
# Finds string in batch files recursively and replaces text with other text
#
#get command line arguments
param (
[string]$Text = $( Read-Host "Input the text to search for"),
[string]$Replacement = $( Read-Host "Input the replacement text")
)
$Today=get-date -format yyyymmdd
$Results = "{server name}\c$\Batch Jobs\Find Text In Batch Jobs\ReplaceTextInBatchJobs-" + $Text + "-" + $Today + ".txt"
$Path = "{server name}\c$\Batch Jobs"
# get all the files in $Path that end in ".bat".
Get-ChildItem $Path -Filter "*.bat" -Recurse |
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object {
#Find whether there is a matching string
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
#Replace the text in this file
(Get-Content $_.FullName) | Foreach-Object {$_ -replace $Text, $Replacement} |
Out-File $_.FullName #write the new results back to the file
#write the file name to a log file
$_.FullName >> $Results
}
}
Out-File defaults to Unicode encoding (which is why the file doubles in size; each character is 16 bits). You can either use Out-File -Encoding ASCII or Set-Content which defaults to ASCII.
I would like to load a file, contains vars' statements.
For example, VLANS.conf will contain $VLANS = "VLAN1500", "VLAN877"
How do I load it into powershell?
Read the file content and use the Invoke-Expression cmdlet to evaluate each line as an expression:
PS > Get-Content .\VLANS.conf | Foreach-Object {Invoke-Expression $_}
PS >$VLANS
VLAN1500
VLAN877
Alternative is to have a VLANS.ps1 or VLANS.conf.ps1 or something and "dot source the file"?
. .\VLANS.ps1
You will have the advantage of having here-strings, script blocks ( and of course anything you can have in a powershell script)