Unable to replace space with comma in powershell script - powershell

In database, I have stored value like: a,b,c
When I fetch and pass it to powershell script as parameter and print, it changes to: a b c
I have tried replacing string using $param1.replace(' ',',') and $param1 -replace '\s',',', but it is not working.
Please help me resolve this.

I've found somethinw that would help:
PS > ("this is test").Replace(" ", "-")
this-is-test
source: https://www.itechguides.com/powershell-replace/

Related

Execute a bat file with a variable name in the path

I am having a problem executing a powershell script where I want to point to different versions of a bat file based on some arguments
Hard coding the path as follows works with no issues:
& 'C:\Program Files\MATLAB\R2022a\bin\mcc.bat' -W $command -T link:exe ...
but replacing a folder name with a variable causes it to fail:
$Matlab_version = "R2022a"
& 'C:\Program Files\MATLAB\$Matlab_version\bin\mcc.bat' -W $command -T link:exe
with the following output:
& : The term 'C:\Program Files\MATLAB\%$Matlab_version%\bin\mcc.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
+ & 'C:\Program Files\MATLAB\%$Matlab_version%\bin\mcc.bat' -W $comman ...
I've tried even creating a variable with the path name
$mcc_loc = & 'C:\Program Files\MATLAB\$Matlab_version\bin\mcc.bat'
Write-Host $mcc_loc
which produces a string with the correct path. Any help is greatly appreciated. My dumb solution is to have an if statement for each $Matlab_version and hard code it in there but I'm sure there has to be a more elegant solution
It's because you are using single quotation marks.
variables will not be interpreted within single marks, everything will be taken as a literal string.
use double quotes instead when passing in variables mid string.
example:
You need to change the single quote ' to double quote "
Double-quoted strings
A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.
$Matlab_version = "R2022a"
& "C:\Program Files\MATLAB\$Matlab_version\bin\mcc.bat" -W $command -T link:exe
See about_Quoting_Rules

Output message with variable part to TeamCity build log from PowerShell

I know that one can output messages to TeamCity build logs via specially decorated Write-Host:
write-host "##teamcity[message text='Prepearing backup folder']"
What if I need the message to contain a value of a variable as well.
I've tried the following:
$myFullMessage = "Perpearing backup folder at: " + $path
write-host "##teamcity[message text=$myFullMessage]"
But I get an error in an output stating that the message parameter provided should start with ' character.
Please let me know if I can output messages with variable value part in message body.
The easiest way is a string formatter. Otherwise you get in escape-character hell. Note how in Powershell that you must place two consecutive single-quote characters to put a literal one in the string.
$myFullMessage = "Perpearing backup folder at: " + $path
write-host $( '##teamcity[message text=''{0}'']' -f $myFullMessage )

How do I pass raw strings to commands through the command line in PowerShell?

Our IT department is moving to Windows 8 and everyone is starting to use PowerShell as their default windows command line environment rather than the cmd.exe.
Unfortunately PowerShell evaluates the things you type in it and it's quite hard to pass raw command line arguments to programs.
For example I've a program inventory.exe that takes a special string that formats its output. I can't pass this special string in PowerShell as I get a mysterious error:
PS C:\Users\Administrator> inventory.exe 'inventory "," date "," owner'
inventory.exe error: No comma allowed
I assume that PowerShell evaluated the 'inventory "," date "," owner' string somehow and something else was passed to inventory.exe program as a result it printed an error.
This program works ok in cmd.exe:
C:\Documents and Settings\boda> inventory.exe 'inventory "," date "," owner'
... (the output that I expect) ...
It even gets worse if I type characters such as %, $ in PowerShell.
Does anyone know how to pass raw strings to commands in PowerShell?
If you are using Powershell v3.0, there is a new syntax to make Powershell avoid any extra parsing for arguments. Something like:
inventory.exe --% 'inventory', 'date', 'owner'
There are other approaches as mentioned here: http://blogs.technet.com/b/josebda/archive/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters.aspx

How do I reference variables when executing a shell command in PowerShell?

I'm a newbie to PowerShell. What's wrong with my script below? It's not wanting to emit the value of $config. However, when I wrap that command in double quotes, everything looks okay.
param($config, $logfolder)
# Must run log analysis in chronological order.
ls $logfolder | Sort-Object LastWriteTime | % {
perl D:\Websites\_awstats\wwwroot\cgi-bin\awstats.pl -LogFile="$($_.FullName)" -config=$config update
}
# Execute with - .\regen-logs.ps1 webgenesis "C:\inetpub\logs\LogFiles\W3SVC5"
# Returns for each file - Error: Couldn't open config file "awstats.config.conf" nor "awstats.conf" after searching in path "D:\Websites\_awstats\wwwroot\cgi-bin,/etc/awstats,/usr/local/etc/awstats,/etc,/etc/opt/awstats": No such file or directory
As-is, what gets emitted and executed seems to have "-config=$config" passed as an argument. At least, that's my best guess. I don't know if $_ is working correctly either.
If I put quotes around the perl command like so, I get the command I do want to execute.
ls $logfolder | Sort-Object LastWriteTime | % {
"perl D:\Websites\_awstats\wwwroot\cgi-bin\awstats.pl -LogFile=`"$($_.FullName)`" -config=$config update"
}
# Outputs for each log file something like - perl D:\Websites\_awstats\wwwroot\cgi-bin\awstats.pl -LogFile="C:\inetpub\logs\LogFiles\W3SVC5\u_ex110602.log" -config=webgenesis update
If putting quotes around it produces the correct commandline, one way to execute the contents of a string is with Invoke-Expression (alias iex):
$v = "myexe -myarg1 -myarg2=$someVar"
iex $v
Put double quotes around "-config=$config". Without this, PowerShell will interpret -config=$config as one string argument that just happens to contain a $ sign in it.
I think you need to start your perl command out with & so that PowerShell interprets things as a command and not a string.
& perl D:\Websites\_awstats\wwwroot\cgi-bin\awstats.pl -LogFile=`"$($_.FullName)`" -config=$config update
Also, see: Run a program in a foreach

How to pass a variable as an argument to a command with quotes in powershell

My powershell script takes the following parameter:
Param($BackedUpFilePath)
The value that is getting passed into my script is:
"\123.123.123.123\Backups\Website.7z"
I have another variable which is the location I want to extract the file:
$WebsiteDeploymentFolder = "C:\example"
I am trying to extract the archive with the following command:
`7z x $BackedUpFilePath -o$WebsiteDeploymentFolder -aoa
I keep getting the following error:
Error:
cannot find archive
The following works but I need $BackedUpFilePath to be dynamic:
`7z x '\123.123.123.123\Backups\Website.7z' -o$WebsiteDeploymentFolder -aoa
I think I need to pass $BackedUpFilePath to 7z with quotes but they seem to get stripped out no matter what I try. I am in quote hell.
Thanks.
EDIT: It turns out the problem was I was passing in "'\123.123.123.123\Backups\Website.7z'". (extra single quotes)
The easiest way to work with external command line applications in PowerShell (in my opinion) is to use aliases. For example, the following works fine for me.
Set-Alias Szip C:\Utilities\7zip\7za.exe
$Archive = 'C:\Temp\New Folder\archive.7z'
$Filename = 'C:\Temp\New Folder\file.txt'
SZip a $Archive $Filename
PowerShell takes care of delimiting the parameters correctly.