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""}"
Related
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?
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
This question already has answers here:
Using a Variable (PowerShell) inside of a command
(2 answers)
Closed 1 year ago.
I have looked at similar questions and not finding answers that apply here. I have also been reading and digging around on learn.microsoft.com. Apologies if this is answered before and I could not locate it.
A geolocation API with a CLI is not working when I feed it a variable in Powershell.
The command: (site, API key and IP are changed for this post)
curl.exe 'https://api.xyz.com/ipgeo?apiKey=XXXX&ip=4.3.2.1'
The script as I have it:
[string]$lookup = $Args[0]
echo $lookup
echo "$lookup"
curl.exe 'https://api.xyz.com/ipgeo?apiKey=XXXX&ip="$lookup"'
I run it from powershell with geo 4.3.2.1
At first I did not use the [string] variable type notation but after it was not working and I did some reading on learn.microsoft.com
I realized I wanted this treated as a string so I added that.
The echo statements are just me checking how the variable is being handled and will be removed from final script. Both of them display the IPv4 address properly.
I have tried the script originally with
curl.exe 'https://api.xyz.com/ipgeo?apiKey=XXXX&ip=$lookup'
then added the double quotes around variable. Neither work.
If I edit the line in the script, and put an IPv4 address in there and run it, it outputs properly, so I know it is not the API or the command for it, it is how the variable is being passed from powershell.
When the variable is used I get an error from the API.
I have tried this in older powershell, and today installed powershell 7.2 and get same result.
'https://api.xyz.com/ipgeo?apiKey=XXXX&ip=$lookup'
'https://api.xyz.com/ipgeo?apiKey=XXXX&ip="$lookup"'
Looks like you are passing it literally. Try double-quotes outside, single inside.
"https://api.xyz.com/ipgeo?apiKey=XXXX&ip='$lookup'"
Edit: Variable expansion in strings and here-strings
https://devblogs.microsoft.com/powershell/variable-expansion-in-strings-and-here-strings/
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
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