I have the following variable which is returned from a function:
[DBG]: PS C:\Users\jmilano\Documents\PowerShell_Scripts\VMs>> $RawHistoryItem | get-member
TypeName: VMware.Vim.VmMigratedEvent
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
ChainId Property int ChainId {get;set;}
ChangeTag Property string ChangeTag {get;set;}
ComputeResource Property VMware.Vim.ComputeResourceEventArgument ComputeResource {get;set;}
CreatedTime Property datetime CreatedTime {get;set;}
Datacenter Property VMware.Vim.DatacenterEventArgument Datacenter {get;set;}
Ds Property VMware.Vim.DatastoreEventArgument Ds {get;set;}
Dvs Property VMware.Vim.DvsEventArgument Dvs {get;set;}
FullFormattedMessage Property string FullFormattedMessage {get;set;}
Host Property VMware.Vim.HostEventArgument Host {get;set;}
Key Property int Key {get;set;}
Net Property VMware.Vim.NetworkEventArgument Net {get;set;}
SourceDatacenter Property VMware.Vim.DatacenterEventArgument SourceDatacenter {get;set;}
SourceDatastore Property VMware.Vim.DatastoreEventArgument SourceDatastore {get;set;}
SourceHost Property VMware.Vim.HostEventArgument SourceHost {get;set;}
Template Property bool Template {get;set;}
UserName Property string UserName {get;set;}
Vm Property VMware.Vim.VmEventArgument Vm {get;set;}
I want to assign the value of various properties of this variable to another variable using this syntax:
$GetRawHistoryAll = Get-VIEventPlus -Entity $entity -Start $start -EventType $eventTypes -Recurse:$Recurse
ForEach( $RawHistoryItem in $GetRawHistoryAll)
{
$Item = New-Object PSObject
$Item | Add-Member -type NoteProperty -Name 'CreatedTime' -Value ($RawHistoryItem.CreatedTime.TolocalTime())
$Item | Add-Member -type NoteProperty -Name 'Type' -Value {If($RawHistoryItem.SourceDatastore.Name -eq $RawHistoryItem.Ds.Name){"vMotion"}else{"svMotion"}}
$Item | Add-Member -type NoteProperty -Name 'UserName' -Value {if($RawHistoryItem.UserName){$RawHistoryItem.UserName}else{"System"}}
$Item | Add-Member -type NoteProperty -Name 'VM' -Value ($RawHistoryItem.VM.Name)
$Item | Add-Member -type NoteProperty -Name 'SrcVMHost' -Value ($RawHistoryItem.SourceHost.Name.Split('.')[0])
$Item | Add-Member -type NoteProperty -Name 'TgtVMHost' -Value {if($RawHistoryItem.Host.Name -ne $RawHistoryItem.SourceHost.Name){$RawHistoryItem.Host.Name.Split('.')[0]}}
$Item | Add-Member -type NoteProperty -Name 'SrcDatastore' -Value ($RawHistoryItem.SourceDatastore.Name)
$Item | Add-Member -type NoteProperty -Name 'TgtDatastore' -Value {if($RawHistoryItem.Ds.Name -ne $RawHistoryItem.SourceDatastore.Name){If([String]::IsNullOrEmpty($RawHistoryItem.Ds.Name)){"NA"}Else{ $RawHistoryItem.Ds.Name}}}
$history += $Item
}
Here is what I’m seeing on $Item after the assignments:
CreatedTime : 2/04/2025 5:47:22 PM
Type : If($RawHistoryItem.SourceDatastore.Name -eq $RawHistoryItem.Ds.Name){"vMotion"}else{"svMotion"}
UserName : if($RawHistoryItem.UserName){$RawHistoryItem.UserName}else{"System"}
VM : vCLS-9419f797-8615-401d-8034-0123456789
SrcVMHost : MyHost05
TgtVMHost : if($RawHistoryItem.Host.Name -ne $RawHistoryItem.SourceHost.Name){$RawHistoryItem.Host.Name.Split('.')[0]}
SrcDatastore : MyDatastore001
TgtDatastore : if($RawHistoryItem.Ds.Name -ne $RawHistoryItem.SourceDatastore.Name){If([String]::IsNullOrEmpty($RawHistoryItem.Ds.Name)){"NA"}Else{ $RawHistoryItem.Ds.Name}}
How can I evaluate the IFs in the above so that I get the values assigned to the $Item properties?