I want to pass the variable as an argument for the variable that runs the script in powershell.

Asked 2 years ago, Updated 2 years ago, 280 views

I am trying to do the following with powershell, but I will fail with で

ホスト Read the json file containing the hostname and IP
②Put the read value into the variable
③The variable that runs the script file that passes the value as an argument in the script file. ④Use the variables created in にて with other variables

# ホストRead the json file with the host name and IP
#CorrespondenceTable.json
{
 "Host1": {
           "IP": "x.x.x.x",
           "Hostname": "Host1"
          },
 "Host2": {
           "IP": "y.y.y.y.y",
           "Hostname": "Host2"
           },
}
# 読みInsert the read value into the variable
$data=Get-content-Path "C:\CorrespondenceTable.json" | ConvertFrom-Json

# CONVERSION CONVERSION
echo$data.Host1.IP
 → x.x.x.x

echo$data.Host1.Hostname
 → Host 1

echo$data.Host2.IP
 → y.y.y.y

echo$data.Host2.Hostname
 → Host 2
#3 Pass the value of the variable that runs the script file as an argument in the script file
$1 = "hoge.ps1$data.Host1.Hostname$data.Host1.IP$data.Host2.Hostname$data.Host2.IP"

# When I check with echo, it looks like the following.
echo $1
 → hoge.ps1@{Host1=;Host2=;};Host1.Hostname@{Host1=;Host2=;};Host1.IP@{Host1=;Host2=;};$data.Host2.Hostname@{Host1=;Host2=;};$data.Host2.IP"

# Try to enclose the variable with {}
$1 = "hoge.ps1${data.Host1.Hostname}${data.Host1.IP}${data.Host2.Hostname}${data.Host2.IP}

# When I checked with echo, the argument part disappeared.
echo $1
 → hoge.ps1

In にて, when I checked with echo, I expected it to be returned as follows.
hoge.ps1Host1 x.x.x.x Host2 y.y.y
How do I describe a variable as an argument, as in the expected form?

powershell

2022-09-30 21:57

1 Answers

I couldn't read it well, but I used variables to read it

 hoge.ps1 Host 1 x.x.x.x Host 2 y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.y.

Do you mean that you want to execute ?
If so, use Invoke-Command to

Invoke-Command-FilePathhoge.ps1-ArgumentList$data.Host1.Hostname,$data.Host1.IP,$data.Host2.Hostname,$data.Host2.IP

and so on.


2022-09-30 21:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.