Powershell function call in verbatim string [duplicate] - powershell

This question already has answers here:
PowerShell outputting array items when interpolating within double quotes
(3 answers)
Closed 6 years ago.
Is a function call in a verbatim PowerShell string a thing?
CSharp example:
$"this my { GetSomeValue() }";
I would rather not extract a variable if I don't have to.

I always do it this way:
$verbatimString = 'something' + (GetSomeValue) + 'end'
Since it's verbatim string, you cannot do any trick, to put value directly into it, like you showed in your example.
Remember to call functions without (), you can read about it here: 4988226

Related

Powershell how to replace multiple commands with output of single variable [duplicate]

This question already has answers here:
Access PSObject property indirectly with variable
(3 answers)
Set Value of Nested Object Property by Name in PowerShell
(3 answers)
Closed 2 months ago.
I am traversing an object and would like to replace several path parts with a string stored in another variable. For example:
# a is a complex ps object, the path is valid
$a.b.c
myvalue
$replace = "b.c"
$a.$($replace)
# here I expect 'myvalue', however nothing is returned, as I would like to execute the equivalent of $a.b.c
Is there some way to do this replacement?

Why does order of operands matter when adding powershell strings and arrays? [duplicate]

This question already has answers here:
Why do integers in PowerShell compare by digits?
(4 answers)
Closed 4 years ago.
In powershell, when I add string + array the result is a string, but when I add array + string the result is an array? Why is that?
PowerShell converts the second operand to the type of the first operand (if it can).

How can I trim the process path from output? [duplicate]

This question already has answers here:
Get only filename from full path of a file
(2 answers)
Closed 5 years ago.
I'm writing a PowerShell script to output the process,
$Process=chknull $_.Properties[0].Value
$Process_new=$Process -replace '(?<! .*) ','_'
The above code outputs the result as entire path of process, Eg:
C:\Windows\System32\Notepad.exe.
Is there a way, where I can get just "Notepad.exe"
If you're getting a full path return like that as a string you can set the value into a variable and then use split on it and grab the last entry in the resulting array.
Example:
("C:\Windows\System32\Notepad.exe." -split "\\")[-1]
Remember to use two "\" as it is also an escape character in regex.
([IO.FileInfo]"C:\Windows\System32\Notepad.exe").Name

Read Array of NSDictionaries With String Interpolation in Swift [duplicate]

This question already has answers here:
escape dictionary key double quotes when doing println dictionary item in Swift
(5 answers)
Closed 8 years ago.
This freaks out because it finds an unexpected " in the string interpolation:
let temp = "\(catalogueRows[0]["person"])"
Tried using single quotes but that didn't work. Seems like a pretty simple step and I'm confused why it doesn't work.
You can't use quotes in a string interpolation. That's just the way the syntax is.
You can't "escape" or single-quote your way out. You'll need two lines of code to do what you're trying to do:
let temptemp = catalogueRows[0]["person"]
let temp = "\(temptemp)"

Need help understanding perl code [duplicate]

This question already has answers here:
What does the '`' character do in Perl?
(3 answers)
Closed 8 years ago.
I am a beginner in perl.
Just wanted to understand the following code.
sub get_files {
foreach my $customer (keys %customers){
lg("Getting files from ftp for customer $customer", "LOG");
my $ftp_server = $customers{$customer}{'FtpServer'};
my $ftp_user = $customers{$customer}{'FtpUser'};
my $ftp_pass = $customers{$customer}{'FtpPass'};
my $datadir = $datafiles.$customer."/";
`$get_files $ftp_server $ftp_user $ftp_pass $datadir`;
}
}
What does last line in the above subroutine tells?
It invokes the command that's in the string $get_files, passing the command the remaining strings as parameters.
Usually it's used if you want to capture the resulting output and store it in a variable. In this case where the result is being discarded it would be more usual to use system instead:
system $get_files, $ftp_server, $ftp_user, $ftp_pass, $datadir;
although if the command does then produce any output it'll appear on-screen instead of being absorbed by the back-ticks operator.