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

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?

Related

How can I use dot sourcing to run code from a variable in the same instance? [duplicate]

This question already has answers here:
Are these alternatives to Invoke-Expression really any safer? And why?
(1 answer)
Powershell reevaluate string to array
(1 answer)
Closed 4 months ago.
I have a variable that stores a command that I would like to execute.
ex:
$C = "echo 'test'"
I have one way to execute this by using the following:
powershell .($C)
however this opens a new instance of powershell. How can I go about executing that code in the same instance? I can't seem to find a way to use dot sourcing without opening that second instance.
EDIT: I can not use IEX

Concat variable's name in powershell [duplicate]

This question already has answers here:
Dynamically create variables in PowerShell
(1 answer)
Create an incrementing variable from 2 variables in PowerShell
(2 answers)
Closed 10 months ago.
I looking for this in powershell
$VAR0=args[0]
$VAR1=args[1]
$VAR2=args[2]
With a construction like this
$a=0
$VAR+$a=args[$a]
but I cannot concatenate this way

powershell, how to expand variable inside array # [duplicate]

This question already has answers here:
Using a Variable (PowerShell) inside of a command
(2 answers)
Closed 1 year ago.
I have trouble with following piece of code:
$result = "F651F545A059588ABFDCE7EE3EEB27EED8CED28D"
winrm create winrm/config/Listener?Address=*+Transport=HTTPS '#{Hostname="my.host.winagent"; CertificateThumbprint="$result"}'
it yields:
Message = The WS-Management service cannot process the configuration request because the certificate thumbprint in the request is not a valid hex string: $result.
it looks like variable result is not being expanded, probably because it is inside array? How to get the variable value expanded there?
You need to use " (not ') to create an expandable string literal - escape the literal "'s by doubling them:
winrm create winrm/config/Listener?Address=*+Transport=HTTPS "#{Hostname=""my.host.winagent""; CertificateThumbprint=""$result""}"

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

Powershell function call in verbatim string [duplicate]

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