簡體   English   中英

Invoke-VMScript-變量變為參數“ -scripttext”

[英]Invoke-VMScript - variable into parameter “-scripttext”

我正在嘗試通過計算機上的Powercli修改VM的IP。

我想使用Invoke-VMScript,但找不到將$ vms.ip和vms.gateway(標頭之一)的結果導入參數“ -scripttext”的方法。

VM找不到變量,因為它不知道。

$vm= import-csv C:\temp\create_vm.csv -Delimiter ';'

foreach($vms in $vm){

Invoke-VMScript -VM $vms.machine -ScriptType Powershell -ScriptText 'New-NetIPAddress -InterfaceAlias "Ethernet" -ipaddress $vms.ip -PrefixLength 24 -DefaultGateway $vms.gateway' -GuestUser test -GuestPassword test

謝謝你的幫助 !

單引號表示PowerShell將不會擴展變量-它將使用文字字符串$ vms.gateway。

$vms = @{
    gateway = "test"
}

$vms.gateway      # prints test

"$vms.gateway"    # prints System.Collections.Hashtable.gateway
"$($vms.gateway)" # prints test

'$vms.gateway'    # prints $vms.gateway
'$($vms.gateway)' # prints $($vms.gateway)

您將需要使用子表達式來訪問$vms變量的屬性,並在Ethernet轉義雙引號,或者對該部分使用單引號:

$script = "New-NetIPAddress -InterfaceAlias 'Ethernet' -ipaddress $($vms.ip) -PrefixLength 24 -DefaultGateway $($vms.gateway)"
-ScriptText $script

更新為根據示例3分配變量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM