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.
Related
I'm still kind of new to windows administration and very new to powershell so please forgive my ignorance. I wrote the following one-liner and I'm attempting to adapt it so that it can modify every existing user's FTRSettings.xml file on a PC. I'm worried that using the following path with a wildcard in place of the username would only replace the first file it finds.
C:\users\*\appdata\Roaming\FTR\GoldMain\FTRSettings.xml
Tested and working one liner executed from the appdata\Roaming\FTR\GoldMain\ directory.
((Get-Content -Path .\FTRSettings.xml -Raw) -replace '<Setting name="audioLevelsVisible" type="11">0</Setting>','<Setting name="audioLevelsVisible" type="11">-1</Setting>' -replace '<Setting name="inputLevelsVisible" type="11">0</Setting>','<Setting name="inputLevelsVisible" type="11">-1</Setting>' -replace '<Setting name="logSheetPaneViewState" type="11">0</Setting>','<Setting name="logSheetPaneViewState" type="11">-1</Setting>')
I was wondering if anyone could point me in the right direction.
To process each file individually:
Use Get-ChildItem with your wildcard-based path to find all matching files.
Loop over all matching files with ForEach-Object and perform the string replacement and updating for each file.
Note: I'm using a single -replace operation for brevity.
Get-ChildItem -Path C:\users\*\appdata\Roaming\FTR\GoldMain\FTRSettings.xml |
ForEach-Object {
$file = $_
($file | Get-Content -Raw) -replace 'foo', 'bar' |
Set-Content -Encoding utf8 -LiteralPath $file.FullName -WhatIf
}
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
Note the use of -Encoding to control the character encoding explicitly, because the original encoding is never preserved in PowerShell; instead, the cmdlet's own default applies - adjust as needed.
Is it possible to copy files using bash script (.sh) on powershell?
Tried using cp and copy, but got command not found error. However, if use cp or copy in the powershell command line, it does work.
I tried
Copy-Item -path "$file_path" -Destination "C:\destination\"
where $file_path is a variable with the source file This resulted in syntax errors-
Unexpected EOF while looking for matching ' " '
syntax error: unexpected end of file
The exact same copy-item command works when executed in powershell command line.
You don't need double quotes for simple string text or a simple variable.
Copy-Item -path $file_path -Destination 'C:\destination'
Double quotes are for variable expansion use cases where it is needed and in some formatting use cases. For example, combining a variable with something else, so say a file path.
Get-ChildItem -Path 'D:\Temp' |
ForEach {
$PSItem
$PSitem.BaseName
'Processing ' + $PSItem.FullName
"Processing $($PSItem.FullName)"
} |
Select -First 4 |
Format-Table -AutoSize
# Results
<#
Directory: D:\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 06-May-20 20:30 AddressFiles
AddressFiles
Processing D:\Temp\AddressFiles
Processing D:\Temp\AddressFiles
#>
So, if I took your sample and refactor a bit so you can see what I mean:
$File_path = 'D:\Temp'
Get-ChildItem -Path $File_path -File |
Select -First 4 |
ForEach {
Copy-Item -Path $PSItem.FullName -Destination 'D:\Temp\est' -WhatIf
}
# Results
<#
What if: Performing the operation "Copy File" on target "Item: D:\Temp\(MSINFO32) command-line tool switches.pdf Destination: D:\Temp\est\(MSINFO32) comm
and-line tool switches.pdf".
What if: Performing the operation "Copy File" on target "Item: D:\Temp\23694d1213305764-revision-number-in-excel-book1.xls Destination: D:\Temp\est\23694
d1213305764-revision-number-in-excel-book1.xls".
What if: Performing the operation "Copy File" on target "Item: D:\Temp\5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url Destination: D:\T
emp\est\5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url".
What if: Performing the operation "Copy File" on target "Item: D:\Temp\abc.bat Destination: D:\Temp\est\abc.bat".
#>
Yet again, as [David C. Rankin], unless your environment is properly configured, you can only run PowerShell commands in the PowerShell consolehost, ISE or VSCode.
You can run external executables in PowerShell, but you must call it properly, especially if you are using the PowerShell ISE.
• PowerShell: Running Executables
Table of Contents
Direct - Using the environment path or local folder
Invoke-Expression (IEX)
Invoke-Command (ICM)
Invoke-Item (II)
The Call Operator &
cmd /c - Using the old cmd shell
Start-Process (start/saps)
[Diagnostics.Process] Start()
WMI Win32_Process Create() Method
Stop-Parsing Symbol --%
How can I create a protocol handler for a powershell script and make the target powershell script receive command line arguments?
And what are the security concerns in doing so?
I thought I write up a decent guide on doing so since the information I found online was lacking some details.
https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767914(v=vs.85)
First off the security concerns
Any program, website, script etc. that is running on your computer can set off the protocol. There are no authorization checks.
You should NOT create a universal protocol handler. That would be a massive security issue concern. I mean that would enable a program, website, script etc. to run any powershell script or command on your computer.
Creating the protocol handler in Windows registry
The protocol must be registered in Windows Registry. It's a simple task.
I'm calling my powershell protocol handler for pwsh
Step 1: Open the Registry Editor and navigate to
Computer\HKEY_CLASSES_ROOT
For inspiration you can look at Computer\HKEY_CLASSES_ROOT\http to look at how that protocol handler is made.
Step 2: Create the following hierarchy:
Create the key pwsh: [Computer\HKEY_CLASSES_ROOT\pwsh]
Edit the default value of (Default) to URL:pwsh. Remember I call my protocol handler for pwsh, write whatever your is called.
Add a string value with the name URL Protocol and empty data.
It should look like this now:
Create a new key under pwsh, DefaultIcon: Computer\HKEY_CLASSES_ROOT\pwsh\DefaultIcon.
Set the (Default) data field to a filepath that leads to an icon or image. I used the powershell icon for Powershell 7 C:\Program Files (x86)\PowerShell\7-preview\assets\ps_black_32x32.ico.
Then create the keys shell -> open -> command like shown on the image above.
In the key command change the (Default) data value to where powershell is installed and then the powershell script to be run.
When testing I do this: "C:\Program Files\PowerShell\6\pwsh.exe" -noexit -executionpolicy bypass -Command {Write-Host %1}
Note I am using powershell core 6 and your path to powershell is probably different.
You can test to check if it works by opening the run program in Windows(Windows+R).
Expected behavior is the powershell window to open with the text pwsh:Hello Stackoverflow printed.
Step 3: Create a powershell script to handle incoming actions on the protocol.
The production ready data value for the command key: "C:\Program Files\PowerShell\6\pwsh.exe" -noexit -File C:\handleActions.ps1 %1
Param($Argument="") # If the protocol is ran you always at least get the protocol name as an argument. (if using the %1)
[String]
$Argument
function Handle-Actions { # The cmdlet 'Handle-Actions' uses an unapproved verb.
[cmdletBinding()]
param(
[Parameter(Mandatory=$false, Position=0)]
[String]
$Argument
)
$Argumuments = $Argument.Split([Char]0x003F) # Splits by `?`
#Argumnets is now in an array, do whatever you need to next.
$Argumuments | %{
Write-Host $_ # Writes each argument that was seperated by ? to a line
}
}
Handle-Actions -Argument $Argument
Given the run command pwsh:?firstArgument?SecondArgument the script will output:
pwsh:
firstArgument
SecondArgument
To complement your helpful guide with sample code that automates creation of a custom protocol handler:
The following:
Creates a custom URI protocol custom: (rather than pwsh:, given that PowerShell is simply used to implement the protocol) to which an open-ended number of arguments can be passed.
Does so for the current user only (HKEY_CURRENT_USER\Software\Classes) by default; however, it's easy to tweak the code to implement the custom protocol for all users instead (HKEY_LOCAL_MACHINE\Software\Classes), though you'll need to run the code with elevation (as administrator) then.
A handler *.ps1 script is automatically created:
At $env:USERPROFILE\customUriHandler.ps1 in the current-user scenario.
At $env:ALLUSERPROFILE\customUriHandler.ps1 in the all-users scenario.
The handler script simply echoes the arguments passed to it, and it is invoked in a PowerShell script window that is kept open after script execution (-NoExit); tweak the PowerShell command as needed.
The protocol expects its arguments as if it were a shell command, i.e., as a space-separated list of arguments, with argument-individual "..." quoting, if necessary.
The sample command at the end uses Start-Process to invoke the following URI, which you could also submit from the Run dialog (WinKey-R), which passes arguments one, two & three, four:
URI: custom:one "two & three" four
Invocation via Start-Process: Start-Process 'custom:one "two & three" four'
Caveat: If you submit this URI via a web browser's address bar (note: doesn't seem to work with Microsoft Edge), it is URI-escaped, and a single one%20%22two%20&%20three%22%20four argument is passed instead, which would require custom parsing; similarly, submitting from File Explorer's address bar passes one%20two%20&%20three%20four, though note that the " chars. are - curiously - lost in the process.
# Determine the scope:
# Set to $false to install machine-wide (for all users)
# Note: Doing so then requires running with ELEVATION.
$currentUserOnly = $true
if (-not $currentUserOnly) {
net session *>$null
if ($LASTEXITCODE) { Throw "You must run this script as administrator (elevated)." }
}
$ErrorActionPreference = 'Stop'
# The name of the new protocol scheme
$schemeName = 'custom'
$pwshPathEscaped = (Get-Process -Id $PID).Path -replace '\\', '\\'
$handlerScript = ($env:ALLUSERSPROFILE, $env:USERPROFILE)[$currentUserOnly] + "\${schemeName}UriHandler.ps1"
$handlerScriptEscaped = $handlerScript -replace '\\', '\\'
# Create the protocol handler script.
#'
# Remove the protocol scheme name from the 1st argument.
$argArray = $args.Clone()
$argArray[0] = $argArray[0] -replace '^[^:]+:'
# If the 1st argument is now empty, remove it.
if ('' -eq $argArray[0]) { $argArray = $argArray[1..($argArray.Count-1)] }
"Received $($argArray.Count) argument(s)."
$i = 0
foreach ($arg in $argArray) {
"#$((++$i)): [$arg]"
}
'# > $handlerScript
# Construct a temp. *.reg file.
# Target the scope-appropriate root registrykey.
$rootKey = ('HKEY_LOCAL_MACHINE\Software\Classes', 'HKEY_CURRENT_USER\Software\Classes')[$currentUserOnly]
# Determine a temp. file path.
$tempFile = [IO.Path]::GetTempPath() + [IO.Path]::GetRandomFileName() + '.reg'
#"
Windows Registry Editor Version 5.00
[$rootKey\$schemeName]
#="URL:$schemeName"
"URL Protocol"=""
[$rootKey\$schemeName\DefaultIcon]
#="$pwshPathEscaped"
[$rootKey\$schemeName\shell]
#="open"
[$rootKey\$schemeName\shell\open\command]
; === Tweak the PowerShell command line here: ===
#="\"$pwshPathEscaped\" -ExecutionPolicy Bypass -NoProfile -NoExit -File \"$handlerScriptEscaped\" %1"
"# > $tempFile
# Import the *.reg file into the registry.
& {
$ErrorActionPreference = 'Continue'
reg.exe import $tempFile 2>$null
if ($LASTEXITCODE) { Throw "Importing with reg.exe failed: $tempFile"}
}
# Remove the temp. *.reg file.
Remove-Item -ErrorAction Ignore -LiteralPath $tempFile
# ---
# Sample invocation of the new protocol with 3 arguments:
$uri = "$schemeName`:one `"two & three`" four"
Write-Verbose -Verbose "Invoking the following URI: $uri"
Start-Process $uri
I'm replacing parts of a .bat script with PowerShell. Configuration for the batch files is done via files that set appropriate environment variables. I'm looking for a way to load those variable values into the .ps1 script, without modifying the .bat files (as they are also used in other places.
An example .bat looks as follows:
set VAR_ONE=some_value
set VAR_TWO=/other-value
In a batch script, I'd just CALL the configuration file and the variables would be available. I've tried both dot-sourcing (. filename.bat) and calling (& filename.bat) the configuration files from PowerShell, neither of those makes the variables visible. Tried accessing them with both with $VAR_ONE and $env:VAR_ONE syntax.
What would be a good way to load such configuration file without modifying it's format on disk?
If you are using the PowerShell Community Extensions, it has a Invoke-BatchFile that does this. I use with the Visual Studio vcvarsall.bat file to configure my PowerShell session to use the Visual Studio tools.
I'd parse them (just skip all lines that don't start with set and split them with first = character. You can do it from o small C# cmdlet or directly with a small PowerShell script:
CMD /c "batchFile.bat && set" | .{process{
if ($_ -match '^([^=]+)=(.*)') {
Set-Variable $matches[1] $matches[2]
}
}}
I have this code and I'm sure it comes from somewhere but credits have been lost, I suppose it comes from Power Shell Community Extensions for an Invoke-Batch script.
The preferred option would be to change the configuration to a .ps1 file and change the variable definitions to PowerShell syntax:
$VAR_ONE = 'some_value'
$VAR_TWO = '/other-value'
Then you'll be able to dot-source the file:
. filename.ps1
If you want to stick with the format you currently have, you'll have to parse the values, e.g. like this:
Select-String '^set ([^=]*)=(.*)' .\filename.bat | ForEach-Object {
Set-Variable $_.Matches.Groups[1].Value $_.Matches.Groups[2].Value
}
Note: The above won't work in PowerShell versions prior to v3. A v2-compatible version would look like this:
Select-String '^set ([^=]*)=(.*)' .\filename.bat | ForEach-Object {
$_.Matches
} | ForEach-Object {
Set-Variable $_.Groups[1].Value $_.Groups[2].Value
}
You can do that via a Batch file that first call the configuration file and then execute the PowerShell script.
Assuming the .bat is called test.bat, define testps.ps1:
$lines = cat "test.bat"
$output = #{};
foreach ($line in $lines) {
$bits = $line.Split("=");
$name = $bits[0].Split(" ")[1];
$val = $bits[1];
$output[$name] = $val
}
return $output
Then the result is something like:
C:\temp> .\testps.ps1
Name Value
---- -----
VAR_TWO /other-value
VAR_ONE some_value
C:\temp> $x = .\testps.ps1
C:\temp> $x
Name Value
---- -----
VAR_TWO /other-value
VAR_ONE some_value
C:\temp> $x["VAR_ONE"]
some_value
There is probably a nicer way of doing the splits (will edit if I find it)
In my PowerShell script I am getting an error that I dont understand.
The error is:
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
Invalid regular expression pattern:
Menu "User" {
Button "EXDS" {
Walk_Right "EXDS"
}
}
.
At C:\test.ps1:7 char:18
+ ($output -replace <<<< $target) | Set-Content "usermenuTest2.4d.new"
+ CategoryInfo : InvalidOperation: (
Menu "User" {...do"
}
}
:String) [], RuntimeException
+ FullyQualifiedErrorId : InvalidRegularExpression
My script reads a file into a string (string A) then attempts to remove String A from another file. What does this error mean and how I can I fix it?
My code:
#set-executionpolicy Unrestricted -Force
#set-executionpolicy -scope LocalMachine -executionPolicy Unrestricted -force
$target=[IO.File]::ReadAllText(".\usermenuTest1.4d")
$output=[IO.File]::ReadAllText(".\usermenuTest2.4d")
($output -replace $target) | Set-Content "usermenuTest2.4d.new"
Try:
($output -replace [regex]::escape($target))
in the -replace $target is always evaluated as a regular expression.
In you case $target contains some regex special character and can't be parsed correctly then you need to escape all special characters. The [regex]::escape() .net method helps doing this job.
That's probably because $target is null (and so is $output).
.NET replaces the dot with the initial working directory in which PowerShell was started (usually either your home directory or systemroot). I'm guessing that usermenuTest1.4d is in a different directory, and you're running this script from that directory. ReadAllText is looking for the file in the initial directory and not finding it.
If you run $target=[IO.File]::ReadAllText(".\usermenuTest1.4d") at the command prompt in the directory in which usermenuTest1.4d is located, you'll see an error telling you that it couldn't find the file and showing you the full path it was looking for, which will be different than what you expected. Or, you could add the following line to your script to see which directory it will replace the dot with:
[environment]::currentdirectory
Any of the following should work:
$target = Get-Content .\usermenuTest1.4d | Out-String
$target = [IO.File]::ReadAllText("$pwd\usermenuTest1.4d")
$target = [IO.File]::ReadAllText((Resolve-Path usermenuTest1.4d))
[environment]::currentdirectory = $pwd
$target=[IO.File]::ReadAllText('.\usermenuTest1.4d')
The last one is unnecessarily cumbersome, but I put it in to help make it clear what's going on.
Of course, you should do the same when setting $output.