2016-08-17 1 views
0

私は最近、私が読んだことから、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 

感謝。ほんとうにありがとう。 :)

+0

Q on style:なぜあなたは '$ NewVM | forEach-Object {...} 'を複数回、各コマンドごとに1回実行しますか?より簡単で、高速で、もっと重要なことにはるかに優れたスタイルではないでしょうか?一度ループするだけで、各反復では、その反復のVM上ですべてのコマンドを実行しますか? –

+0

...貼り付けられたCSVには、実際のものの間に空白行があります。 CSVは1行のヘッダー(オプション)を持っていると想定され、ファイル内の各レコードに対して1行があるとみなされます。それはあなたがあなたの命令に与えるものですか?そうであれば、インポートされたすべての変数は1行おきに空白になります。それ以前にエラーが発生していないのは間違いありません。後者の説明は、あなたのPSスクリプトが同じ問題をどのように持っているかによって証明されるかもしれません。 –

+0

はい、貼り付け中に間違いでした。 2番目の投稿の示唆に合わせて、その外観/スタイルを改善することをおすすめしますか? – KennethWC

答えて

1

私はあなたのコメントを残しましたが、$NewVMを通してすべてのさまざまなループを1つのループにまとめました(そして、そのループの前にスイッチの作成を移動してコメントアウトしました)。また、GFXコマンドをIfステートメントに変更したので、指定したVMにのみそのコマンドが適用されます。最後に、あなたのAdd-VMNetworkAdapterコマンドを修正しました。あなたが無効なパラメータセットを持っていたので、それは動作していませんでした。あなたが表示されますGet-Help Add-VMNetworkAdapterあなたが実行した場合、パラメータセットは、以下のとおりです。

Add-VMNetworkAdapter [-VMName] <string[]> [-CimSession <CimSession[]>] [-ComputerName <string[]>] [-Credential <pscredential[]>] [-SwitchName <string>] [-IsLegacy <bool>] 
[-Name <string>] [-DynamicMacAddress] [-StaticMacAddress <string>] [-Passthru] [-ResourcePoolName <string>] [-DeviceNaming {On | Off}] [-WhatIf] [-Confirm] [<CommonParameters>] 

Add-VMNetworkAdapter [-CimSession <CimSession[]>] [-ComputerName <string[]>] [-Credential <pscredential[]>] [-ManagementOS] [-SwitchName <string>] [-Name <string>] 
[-DynamicMacAddress] [-StaticMacAddress <string>] [-Passthru] [-DeviceNaming {On | Off}] [-WhatIf] [-Confirm] [<CommonParameters>] 

Add-VMNetworkAdapter [-VM] <VirtualMachine[]> [-SwitchName <string>] [-IsLegacy <bool>] [-Name <string>] [-DynamicMacAddress] [-StaticMacAddress <string>] [-Passthru] 
[-ResourcePoolName <string>] [-DeviceNaming {On | Off}] [-WhatIf] [-Confirm] [<CommonParameters>] 

あなたが最後のセットから最初のセットから-VMName、および-SwitchNameを使用しようとしました。変数に変数を作成したときにVMをキャプチャするように変更した後、-VMパラメータにVMを使用するように変更しました。また、VMを作成した直後にそのコマンドを移動して、変数内のデータが初期状態になるようにしました。ここで私は結局何をしたのですか?

# 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" 

# Creates External Network Switch 
# New-VMSwitch "External Realtek NIC" -NetAdapterName "Realtek_NIC" -AllowManagementOS $False 

$NewVM | ForEach-Object { 
    # Specifies VMName and Generation Number without attaching VHDX files. 
    $VM = New-VM -Name $_.VMName -Generation $_.Generation -NoVHD 

    # Adds External Realtek_NIC to all VMs 
    Add-VMNetworkAdapter -VM $VM -SwitchName "External Realtek NIC" -Name "External Realtek" 

    # Sets Startup, Mininum, and Maximum Memory fields. 
    $VMMem = Set-VMMemory -VMName $_.VMName -StartupBytes 1GB -MinimumBytes 512MB -MaximumBytes $_.MaxMemory 

    # Creates VHDX file based on the VM name. 
    $VHD = New-VHD -Path ("F:\Hyper-v_Storage\"+$_.VMName+".vhdx") -SizeBytes $_.VHDXSize 

    # Adds VHDX to VMs 
    $VMHD = Add-VMHardDiskDrive -VMName $_.VMName -Path ("F:\Hyper-V_Storage\"+$_.VMName+".vhdx") -ControllerNumber 0 -ControllerLocation 0 

    # Add VMDVDDrive with ISO to VMs. 
    $VMDVD = 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 
    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 
    If($_.VMName -like "Chief_Architect - Windows 10") {Add-VMRemoteFx3dVideoAdapter -VMName $_.VMName} 
} 
+0

私は自分のスクリプトを改訂しましたが、アダプターの追加以外はすべて動作します。 スクリプトのレンディションを実行しましたが、起動順序以外はすべて動作しました。 – KennethWC

+0

ああ、あなたはいつもあなたの元のコマンドをその行に使うことができます。私は自分の答えを編集してオリジナルをその行に使用します。それがなぜ失敗するのかは分かりませんが、あなたの作品が簡単な修正だとすれば。 – TheMadTechnician

関連する問題