首页 > 代码库 > PowerShell 批量创建Linux虚机
PowerShell 批量创建Linux虚机
Write-Host -NoNewline -ForegroundColor Magenta ‘请输入要创建的虚机名称(如:VLNX******)‘ [String]$VM_Name = Read-Host Write-Host -NoNewline -ForegroundColor Magenta ‘请输入需要放在哪台宿主机上(如:PWSR******)‘ [String]$VM_HostName= Read-Host Write-Host -NoNewline -ForegroundColor Magenta ‘要部署CentOS6还是7?直接回车默认安装的是6(如:7 回车 )‘ [int16]$VM_OSVersion= Read-Host #判断虚机名称是否为大写字母 if ($VM_Name |findstr ‘vlnx‘) { Write-Host -NoNewline -ForegroundColor Yellow ‘注意:虚机名需要使用大写字母!!!‘ break } Else { } $VM_Memory = 2GB $VM_CpuCount = 2 $NetworkSwitch = "PublicNetwork" #if判断要部署的是CentOS6还是CentOS7,默认是CentOS6 if ($VM_OSVersion -eq ‘7‘) {$Template_VHDX = "En_CentOS7_Std.vhdx"} Else {$Template_VHDX = "En_CentOS6_Std.vhdx"} $Template_VHDX_FilePath = "D:\Deploy_Script" #if判断如果虚机所在宿主机为730xd,虚机文件存放位置为E盘 if (( $VM_HostName -eq ‘PWSR000006‘ ) -or ($VM_HostName -eq ‘PWSR000007‘ )) { $VM_Path = ‘E:\‘ + $VM_HostName + ‘_E_VMs‘ $VM_RemotePath = ‘\\‘ + $VM_HostName + ‘\E$\‘ + $VM_HostName + ‘_E_VMs‘ } Else { $VM_Path = ‘D:\‘ + $VM_HostName + ‘_D_VMs‘ $VM_RemotePath = ‘\\‘ + $VM_HostName + ‘\D$\‘ + $VM_HostName + ‘_D_VMs‘ } $VM_VHDPath = $VM_Path + ‘\‘ + $VM_Name + ‘\Virtual Hard Disks\‘+ $VM_Name + ‘.vhdx‘ $VM_VHDRemotePath = $VM_RemotePath + ‘\‘ + $VM_Name + ‘\Virtual Hard Disks\‘ + $VM_Name + ‘.vhdx‘ #新建新虚机硬盘目录,并将目标硬盘Copy到新虚拟硬盘目录下 New-item -path $VM_RemotePath\$VM_Name -name "Virtual Hard Disks" -type directory Copy-Item -Path $Template_VHDX_FilePath\$Template_VHDX -Destination $VM_VHDRemotePath #新建虚机 #Write-Host -ForegroundColor White "Debug info VMNAME: $VM_Name,HostName: $VM_HostName" New-VM –Name $VM_Name -ComputerName $VM_HostName –Generation 1 -MemoryStartupBytes $VM_Memory -BootDevice IDE -SwitchName $NetworkSwitch –Path $VM_Path -VHDPath $VM_VHDPath #设置CPU个数 Set-VM -Name $VM_Name -ComputerName $VM_HostName -ProcessorCount $VM_CpuCount #禁用动态内存功能 #Set-VMMemory -Name $VM_Name -ComputerName $VM_HostName -DynamicMemoryEnabled $False #设置VLANID if ($VM_name -like ‘VLNX112*‘ -or $VM_name -like ‘VLNX113*‘ -or $VM_name -like ‘VLNX114*‘) { Set-VMNetworkAdapterVlan -ComputerName $VM_HostName -VMName $VM_Name -Access -VlanId 11} Else { Set-VMNetworkAdapterVlan -ComputerName $VM_HostName -VMName $VM_Name -Access -VlanId 21} #通过SCVMM分配静态MACAddress #Import-Module virtualmachinemanager -Verbose #$VM_HostServer=Get-SCVMHost -ComputerName $VM_HostName -VMMServer SCVMMServer.Contoso.com #Read-SCVMHost -VMHost $FS_HostServer |Out-Null Read-SCVMHost -VMHost $VM_HostName |Out-Null $VM=Get-SCVirtualMachine -Name $VM_Name #Write-Host "Debug info" #$VM.Name #Pause $Adapter=$VM.VirtualNetworkAdapters[0] Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $Adapter[0] -MACAddressType "Static" -MACAddress "00-00-00-00-00-00" #启动虚机 #Start-VM -Name $VM_Name -ComputerName $VM_HostName Start-SCVirtualMachine -VM $VM_Name #添加计算机描述 #Set-ADComputer -Identity $VM_Name -Description ""
本文出自 “Ray” 博客,请务必保留此出处http://sxleilong.blog.51cto.com/5022169/1909330
PowerShell 批量创建Linux虚机
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。