Powershell 作业不创建虚拟机
我想用作业创建VMPowershell 作业不创建虚拟机,powershell,vmware,powercli,start-job,vcenter,Powershell,Vmware,Powercli,Start Job,Vcenter,我想用作业创建VM cls if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) { Add-PSSnapin VMware.VimAutomation.Core | Out-Null } #import VM settings $VMs = Import-CSV '...\Install_AIO_automated\Data.csv' -UseCulture Write-H
cls
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core | Out-Null
}
#import VM settings
$VMs = Import-CSV '...\Install_AIO_automated\Data.csv' -UseCulture
Write-Host "Current connections:"
$Global:DefaultVIServers.count
$ScriptBlocky = {
Param($VM)
Write-Host "Begin"
$server += $VM.vs_xstream__vc_host
$connection = Connect-VIServer $VM.vs_xstream__vc_host -User $VM.vs_xstream__vc_admin -Password $VM.vs_xstream__vc_password
$connection.SessionId
Write-Host $connection.SessionId
Write-Host $OSspec
$OSspec = 'BasicLinuxSpec'
# Selecting random Cluster for VMs deployment
Write-Host $VM.Cluster
Write-Host $VM.vs_xstream__vc_host
$ClusterHoste = Get-Cluster $VM.Cluster -server $VM.vs_xstream__vc_host | Get-VMHost | Get-Random
#$ClusterHost = Get-Cluster $VM.Cluster -server $VM.vs_xstream__vc_host | Get-VMHost | Where{$_.ConnectionState -eq "Connected"} | Get-Random
Write-Host $connection.SessionId
Write-Host $ClusterHoste
Write-Host ""
$domain_name = $VM.FQDN.split('.')
Write-Host $domain_name
# Create new OS Customization Specification template for further usage
New-OSCustomizationSpec -Name $OSspec -Domain ("{0}.{1}" -f $domain_name[1],$domain_name[2]) -DnsServer $VM.DNS,$VM.DNS2 -NamingScheme VM -OSType Linux -Server $VM.vs_xstream__vc_host
# Starting VMs customization with previously created specification
$OSspec
Write-Host "TRY"
Get-OSCustomizationSpec $OSspec -Server $VM.vs_xstream__vc_host | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.Ipaddress -SubnetMask $VM.NetMask -DefaultGateway $VM.Gateway
Write-Host "VM create:"
Write-Host $OSspec
Write-Host $ClusterHost
New-Vm -VMhost $ClusterHost -Name $VM.vm_name -Server $VM.vs_xstream__vc_host -Datastore $VM.Datastore -Template $VM.Template -Description $VM.Description -DiskStorageFormat "Thin" -OScustomizationSpec $OSspec
# Remove Customization Specification template
Remove-OSCustomizationSpec $OSspec -Server $VM.vs_xstream__vc_host -confirm:$false
Write-Host "Done"
}
foreach ($VM in $VMs) {
if ($VM.Use -eq 1) {
$session = $global:DefaultVIServer | %{ $_.sessionsecret }
$vcserver = $global:DefaultVIServer.ServiceUri.Host
$session
$vcserver
Write-Host "start script"
Start-Job -ScriptBlock $ScriptBlocky -ArgumentList $VM
Write-Host "end script"
Get-Job | Wait-Job
}
}
结果:
Current connections:
1
22f81178669d314003bc75206e3ffad384ee2568
10.xx.xx.xx (IP address)
start script
Id Name PSJobTypeName State HasMoreData Location
-- ---- ------------- ----- ----------- --------
46 Job46 BackgroundJob Running True localhost
end script
2 Job2 BackgroundJob Completed False localhost
..
46 Job46 BackgroundJob Completed True localhost
将创建虚拟机
有人看到我的错误或我做错了什么吗?这是我用来创建虚拟机的代码,我的代码
$create = New-VM -Name $VM01Name -Template $VM01Template -Location $VM01Folder -VMHost $VM01Host -ResourcePool $VM01Resource -Datastore $VM01DiskDatastore01 -OSCustomizationSpec $VM01OSCustom -RunAsync -Confirm:$false
看看这是否有帮助这是我用来创建虚拟机的代码
$create = New-VM -Name $VM01Name -Template $VM01Template -Location $VM01Folder -VMHost $VM01Host -ResourcePool $VM01Resource -Datastore $VM01DiskDatastore01 -OSCustomizationSpec $VM01OSCustom -RunAsync -Confirm:$false
看看这是否会像(线程不创建多个VM(会话问题))问题中的解决方案一样有帮助
解决方案:*在执行所有其他操作之前,先创建到vCenter的所有连接,以便连接保持稳定。例如:
# Iterate and connect to all vCenters so connections to them will be stable and read only
ForEach($VM in $VMs)
{
If ($VM.Use -eq 1)
{
If($server.Contains($VM.vs_xstream__vc_host) -eq $FALSE)
{
$server += $VM.vs_xstream__vc_host
$AllSessions += Connect-VIServer $VM.vs_xstream__vc_host -User $VM.vs_xstream__vc_admin -Password $VM.vs_xstream__vc_password -NotDefault
Write-Host "Connected to: " $VM.vs_xstream__vc_host
}
}
}
完成所有工作后,我只需断开与所有会话的连接
# Disconnect all created sessions
ForEach($item in $server)
{
Write-Host ("Disconnecting from {0}...." -f ($item))
Disconnect-VIServer $item -Confirm:$false
}
现在,该脚本工作正常,可以在不同或相同的vCenter中创建VM。如果有人遇到任何问题,请告诉我。与(线程不创建多个虚拟机(会话问题))问题中的解决方案相同
解决方案:*在执行所有其他操作之前,先创建到vCenter的所有连接,以便连接保持稳定。例如:
# Iterate and connect to all vCenters so connections to them will be stable and read only
ForEach($VM in $VMs)
{
If ($VM.Use -eq 1)
{
If($server.Contains($VM.vs_xstream__vc_host) -eq $FALSE)
{
$server += $VM.vs_xstream__vc_host
$AllSessions += Connect-VIServer $VM.vs_xstream__vc_host -User $VM.vs_xstream__vc_admin -Password $VM.vs_xstream__vc_password -NotDefault
Write-Host "Connected to: " $VM.vs_xstream__vc_host
}
}
}
完成所有工作后,我只需断开与所有会话的连接
# Disconnect all created sessions
ForEach($item in $server)
{
Write-Host ("Disconnecting from {0}...." -f ($item))
Disconnect-VIServer $item -Confirm:$false
}
现在,该脚本工作正常,可以在不同或相同的vCenter中创建VM。如果有人会遇到任何问题,请告诉我。好的,以前我尝试过对线程执行相同的操作,但失败了,现在正在处理作业。输出:start script 20 Job20 BackgroundJob运行真正的localhost结束脚本,但是Get_job给了我这个。Job20 BackgroundJob运行True localhost看起来脚本无法执行某些操作。查看接收作业的输出后,显示以下行:新OSCustomizationSpec–名称$OSspec–域“xxxx.local”–DnsServer$VM.DNS、$VM.DNS2–NamingScheme VM–OSType Linux–服务器$VM.vs_xstream_uvc_主机无错误。我不知道为什么。我想问题可能出在会话上……好吧,以前我试着用线程做同样的事情,但失败了,现在用作业做。输出:start script 20 Job20 BackgroundJob运行真正的localhost结束脚本,但是Get_job给了我这个。Job20 BackgroundJob运行True localhost看起来脚本无法执行某些操作。查看接收作业的输出后,显示以下行:新OSCustomizationSpec–名称$OSspec–域“xxxx.local”–DnsServer$VM.DNS、$VM.DNS2–NamingScheme VM–OSType Linux–服务器$VM.vs_xstream_uvc_主机无错误。我不知道为什么。我想问题可能出在会议上。。。