Thanks a lot LucD.. I am able to get the output if i run it for one job, But have a problem while executing multiple jobs.. The output returned for is not correct. It always returns the output of the last VM queried.. Could you please find the mistake in my code...
Below is the Job
$code = {
param(
[string]$VIServer,
[string]$VISessionId,
[string]$VMName,
[string]$org,
[string]$vapp,
[string]$CIServer,
[string]$CISessionId
)
$rem1 = Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null
$rem2 = Connect-VIServer -Server $VIServer -Session $VISessionId
$rem3 = Connect-CIServer -Server $CIServer -SessionId $CISessionId
. C:\Migration_Powershell_Scripts\New_functions\Get-vspherevmname.ps1
get-vspherevmname -vmname $VMName -org $org -vapp $vapp -CIServer $CIServer -CISessionId $CISessionId -VIserver $VIServer -VIsessionid $VISessionId
}
$VM = @("R511-SRVA", "R511-SRVB", "VEPMASTER")
$results = @()
$org = "ID14434-1-1231231"
$vapp = "12345"
$sJOb = @{
ScriptBlock = $code
ArgumentList = $global:DefaultVIServer.Name, $global:DefaultVIServer.SessionId, $vmName, $org, $vapp, $global:DefaultCIServers.Name, $global:DefaultCIServers.SessionId
}
$VM = @("R511-SRVA", "R511-SRVB", "VEPMASTER")
$results = @()
foreach($vmName in $VM){
$j = Start-Job @sJob
Wait-Job $j
$row= new-object -TypeName PSObject -Property @{
'VM_name' = $VMname
'Job_ID' = $j.Id
'vspherename' = Receive-Job -Id $j.Id -Keep
}
$results += $row
}
Output is
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
107 Job107 BackgroundJob Completed True localhost ...
VEPMASTER-ryQF
109 Job109 BackgroundJob Completed True localhost ...
VEPMASTER-ryQF
111 Job111 BackgroundJob Completed True localhost ...
VEPMASTER-ryQF
Here i am expecting the return value to be different..
My Custom Function is
Function Get-vspherevmname{
[CmdletBinding()]
[OutputType([psobject])] # Select the output type of this function as "string/PSObject/Int"
# Enter the list of Input Parameters for the function
#The Parameter should have properties as "Mandatory", HelpMessage
Param (
[Parameter(Mandatory=$true, HelpMessage='VM name on VCD')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$vmname,
[Parameter(Mandatory=$true, HelpMessage='VM name on VCD')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$org,
[Parameter(Mandatory=$true, HelpMessage='VM name on VCD')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$vapp,
[Parameter(Mandatory=$false, HelpMessage='Server Name')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$CIServer,
[Parameter(Mandatory=$false, HelpMessage='VM name on VCD')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$CISessionId,
[Parameter(Mandatory=$false, HelpMessage='VM name on VCD')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$VIserver,
[Parameter(Mandatory=$false, HelpMessage='VM name on VCD')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$VIsessionid
)
Begin{
}
Process {
Try{
#Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null
$ignore = Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null
$ciconnect = Connect-CIServer -Server $ciserver -Session $cisessionid
$viconnect = Connect-VIServer -Server $viServer -Session $viSessionId
$vm = Get-CIVM -Org $org -VApp $vapp -Name $vmname
$vmhref = $vm.ExtensionData.Href
#Write-Host $vmhref
##[string]$ApiVersion = '31.0'
##$Headers = @{ "x-vcloud-authorization" = $vcloudtoken; "Accept" = 'application/*+xml;version=' + $ApiVersion }
##$body = $null
##Invoke-RestMethod -Method GET -Uri $vmhref -Headers $Headers -Body $body
. 'C:\Migration_Powershell_Scripts\New_functions\Invoke-vCloud.ps1'
$vmstate = Invoke-vCloud -URI $vmhref -Method GET -vCloudToken $CISessionId
$vmnvramname = $vmstate.Vm.VirtualHardwareSection.ExtraConfig | Where-Object {$_.key -eq "nvram"}
$vspherevmname = $vmnvramname.value.Split(".",6)[0]
#$Headers = @{ "x-vcloud-authorization" = $mySessionID; "Accept" = 'application/*+xml;version=' + $ApiVersion }
return $vspherevmname
}
Catch{Write-host "Catch Block"}
}
end {
Write-Verbose 'VM Name Read'
}
}