- 1 year ago
extract PSComputerName for invoke command
To extract the PSComputerName
property from the output of the Invoke-Command
cmdlet in PowerShell, you can use the Select-Object
cmdlet to retrieve only the desired property. Here's an example:
powershell$session = New-PSSession -ComputerName <RemoteComputerName>
$result = Invoke-Command -Session $session -ScriptBlock {
# Your script or command goes here
# ...
# Return the desired result
$PSComputerName
}
$computerName = $result | Select-Object -ExpandProperty PSComputerName
In the above example, the Invoke-Command
cmdlet is used to run a script or command on the remote computer specified by <RemoteComputerName>
. The output of the Invoke-Command
is stored in the $result
variable. By using Select-Object
with the -ExpandProperty
parameter, we extract the value of the PSComputerName
property from the $result
object and assign it to the $computerName
variable.
Make sure to replace <RemoteComputerName>
with the actual name or IP address of the remote computer you want to execute the command on.
By extracting the PSComputerName
property in this way, you can retrieve the computer name associated with the output of the Invoke-Command
cmdlet.