私は最近、私が読んだことから、PowerShellスクリプティングについて教え始めました。 、PowerShellのすべてがGUIと比較されます。これはWindows 10クライアントとサーバーの両方に適用されます。PS VMスクリプト:CSVファイルのAdd-VMNetworkAdapterの引数の検証でエラーが発生しました。
私はHyper-Vを本当に楽しんでいるので、自宅でWindows 10 Professionalを使用しています。
これは私がこれまでに私のスクリプトのためにしたものである:
Start-Job
{
# Create new VMs based on the names given in the "VMName.csv" file.
$NewVM = Import-Csv "D:\Hyper-V\Hyper_V_Scripts\Test_Gen2_Virtual_Machine_Scratch.csv"
# Specifies VMName and Generation Number without attaching VHDX files.
$NewVM | ForEach-Object {New-VM -Name $_.VMName -Generation $_.Generation -NoVHD}
# Sets Startup, Mininum, and Maximum Memory fields.
$NewVM | ForEach-Object {Set-VMMemory -VMName $_.VMName -StartupBytes 1GB -MinimumBytes 512MB -MaximumBytes $_.MaxMemory}
# Creates VHDX file based on the VM name.
$NewVM | ForEach-Object {New-VHD -Path ("F:\Hyper-v_Storage\"+$_.VMName+".vhdx") -SizeBytes $_.VHDXSize}
# Adds VHDX to VMs.
$NewVM | ForEach-Object {Add-VMHardDiskDrive -VMName $_.VMName -Path ("F:\Hyper-V_Storage\"+$_.VMName+".vhdx") -ControllerNumber 0 -ControllerLocation 0}
# Add VMDVDDrive with ISO to VMs.
$NewVM | ForEach-Object {Add-VMDvdDrive -VMName $_.VMName -Path ("D:\iso\"+$_.ISO+".iso") -ControllerNumber 0 -ControllerLocation 1}
# Change firmware's boot order to CD/DVD, Hard Disk Drive, and Network Adapter
$NewVM | ForEach-Object {Set-VMFirmware -VMName $_.VMName -BootOrder (Get-VM -Name $_.VMName | Get-VMDvdDrive),(Get-VM -Name $_.VMName | Get-VMHardDiskDrive),(Get-VM -Name $_.VMName | Get-VMNetworkAdapter)}
# Add RemoteFX3DAdapter to certain VMs
$NewVM | Where-Object {$_.VMName -like "Chief_Architect - Windows 10"} | ForEach-Object {Add-VMRemoteFx3dVideoAdapter -VMName $_.VMName}
# Creates External Network Switch
# New-VMSwitch "External Realtek NIC" -NetAdapterName "Realtek_NIC" -AllowManagementOS $False
# Adds External Realtek_NIC to all VMs
$NewVM | ForEach-Object {Add-VMNetworkAdapter -VMName $_.VMName -SwitchName "External Realtek NIC" -Name "External Realtek"}
}
私が知りたいのですが、それは下のコンポーネントを行うことが賢明である私はする必要があることを考慮すると、「#外部スイッチを作成します」それは一度だけですか、それとも一度実行する必要があるので、私はそれを別々に実行すべきですか?
また、私は最後の行を実行するたびに、「#は、すべてのVMに外部Realtek_NICを追加します」の下で、私は次のエラーを取得:私はときにエラーが出る理由
Add-VMNetworkAdapter : Cannot validate argument on parameter 'VMName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:55
+ $NewVM | ForEach-Object {Add-VMNetworkAdapter -VMName $_.VMName}
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-VMNetworkAdapter], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.HyperV.PowerShell.Commands.AddVMNetworkAdapter
を私は理解していません私が作成したCSVファイルは他のすべての場所で正常に動作します。 CSVファイルの
内容:提供の助けのための高度で
VMName,MaxMemory,ISO,VHDXSize,Generation
ADDS-DHCP-DNS-WDS - Windows Server 2016 TP 5,2147483648,TP_5\Windows_Server_2016\14300.1000.160324-1723.RS1_RELEASE_SVC_SERVER_OEMRET_X64FRE_EN-US,107374182400,2
Chief_Architect - Windows 10,6442450944,Win10_1607_English_x64,37580963840,2
Development - Windows 10,4294967296,Win10_1607_English_x64,161061273600,2
PFSense,1073741824,pfSense-CE-2.3.1-RELEASE-amd64,2147483648,2
Steam - Windows 10,6442450944,Win10_1607_English_x64,322122547200,2
Storage Pool - Windows Server 2016 TP 5,2147483648,TP_5\Windows_Server_2016\14300.1000.160324-1723.RS1_RELEASE_SVC_SERVER_OEMRET_X64FRE_EN-US,42949672960,2
Winixhub - Windows 10,4294967296,Win10_1607_English_x64,53687091200,2
Winixhub Web Server - Windows Server 2016 TP 5,4294967296,TP_5\Windows_Server_2016\14300.1000.160324-1723.RS1_RELEASE_SVC_SERVER_OEMRET_X64FRE_EN-US,85899345920,2
感謝。ほんとうにありがとう。 :)
Q on style:なぜあなたは '$ NewVM | forEach-Object {...} 'を複数回、各コマンドごとに1回実行しますか?より簡単で、高速で、もっと重要なことにはるかに優れたスタイルではないでしょうか?一度ループするだけで、各反復では、その反復のVM上ですべてのコマンドを実行しますか? –
...貼り付けられたCSVには、実際のものの間に空白行があります。 CSVは1行のヘッダー(オプション)を持っていると想定され、ファイル内の各レコードに対して1行があるとみなされます。それはあなたがあなたの命令に与えるものですか?そうであれば、インポートされたすべての変数は1行おきに空白になります。それ以前にエラーが発生していないのは間違いありません。後者の説明は、あなたのPSスクリプトが同じ問題をどのように持っているかによって証明されるかもしれません。 –
はい、貼り付け中に間違いでした。 2番目の投稿の示唆に合わせて、その外観/スタイルを改善することをおすすめしますか? – KennethWC