在powershell中调用前面没有空格的变量?
我试图使用导入的CSV文件中的变量替换字符串中的变量 这是密码在powershell中调用前面没有空格的变量?,powershell,Powershell,我试图使用导入的CSV文件中的变量替换字符串中的变量 这是密码 param([string]$CSV) $VMs=导入Csv$Csv Foreach($VM中的VM){ psexec\\$VM.VM_Name-h netsh interface ip set address Name=''Local Area Connection''static$VM.ip_address 255.255.255.0$VM.Gateway 1 } 这就是它的回报: psexec\{VM_Name=TESTCSV
param([string]$CSV)
$VMs=导入Csv$Csv
Foreach($VM中的VM){
psexec\\$VM.VM_Name-h netsh interface ip set address Name=''Local Area Connection''static$VM.ip_address 255.255.255.0$VM.Gateway 1
}
这就是它的回报:
psexec\{VM_Name=TESTCSVVM;IP_地址=10.12.81.82;网关=10.12.81.1;VLAN=H
Q_VM_81}.VM_Name-h netsh interface ip set address Name=“局域网连接”
静态10.12.81.82 255.255.255.0 10.12.81.1 1
以下是我希望它看起来的样子:
C:\Windows\myFile.ext
psexec\TESTCSVVM-h netsh interface ip set address name=“局域网连接”
静态10.12.81.82 255.255.255.0 10.12.81.1 1
以下是CSV文件:
“虚拟机名称”、“IP地址”、“网关”、“VLAN”
“TESTCSVVM”、“10.12.81.82”、“10.12.81.1”、“HQ_VM_81”
如何确保\和第一个变量$VM.VM\u名称之间没有空格
谢谢试试这个:
Foreach ($VM in $VMs) {
psexec \\\\$($VM.VM_Name) -h netsh interface ip set address `
name='"Local Area Connection"' `
static $VM.IP_Address 255.255.255.0 $VM.Gateway 1
}
此外,通常用引号括起变量也可以 因此,如果:
$path = C:\Windows\
$File = MyFile.ext
write-host "$path$File
看起来像:
C:\Windows\myFile.ext