Need RDM P/S assistance on a script for Importing/Synching Entries

Resolved

Need RDM P/S assistance on a script for Importing/Synching Entries

avatar

Good morning everyone, and hope I've come to the right spot. I didn't even realize there was a forum for RDM P/S scripting until this morning. I am such a complete ID10T!

So I have the following script I received from the Support Forum for a topic, and modified it just a bit for my needs which works fabulous until RDM Sync's are fixed some day in some year in some future. :-)

Here's the issue I am having... With the following script, it references $updateResult = "Entry Not Found", however it doesn't right anything to the results file with an entry that wasn't found.

Between the six different RVTools Report "Syncs" I performed using the script below, there's over 225 that either aren't in my Entry List, or they are a different name in RVTools Report than what I have in RDM. That is where if it actually wrote a line of Entry Not Found when it comes across one, that would be awesome.

However, no matter what I do, I just can't figure out how to get it to create a line entry for the ones not found.

Anyone that has any ideas?

Thanks in advance!

$csvData = Import-Csv -Path "$($env:USERPROFILE)\Desktop\INPUT.csv"

# Fetch all RDM data sources
$dataSourceList = Get-RDMDataSource

# Prepare a list to hold the results
$results = @()

# Iterate through each row in the CSV
foreach ($row in $csvData) {
    $deviceName = $row.VM
    $customfield1 = $row.CustomField1Value
    $customfield2 = $row.CustomField2Value
    $customfield3 = $row.CustomField3Value
    $ClientTag = $row.ClientCode
    $AlternateHosts = $row.IPv4
    $oldcustomfield1 = $null
    $oldcustomfield2 = $null
    $oldcustomfield3 = $null
    $oldClientTag = $null
    $oldAlternateHosts = $null
    $updateResult = "Entry Not Found"
    $updatedDataSource = $null
    Write-host "Connecting to $deviceName"
    foreach ($dataSource in $dataSourceList) {
        Set-RDMCurrentDataSource -DataSource $dataSource
        $rdmSessions = Get-RDMSession -IncludeSubFolders | Where-Object { $_.Name -eq $deviceName }

        if ($rdmSessions) {
            foreach ($session in $rdmSessions) {
                $oldcustomfield1   = $session.MetaInformation.CustomField1Value
                $oldcustomfield2   = $session.MetaInformation.CustomField2Value
                $oldcustomfield3   = $session.MetaInformation.CustomField3Value
                $oldClientTag      = $session.MetaInformation.Keywords
                $oldAlternateHosts = $session.AlternateHosts

                # Check to see if CustomField1 (NIC #1 VLAN) needs to be updated or not.
                if ($oldcustomfield1 -ne $customfield1) {
                    $session.MetaInformation.CustomField1Value = $customfield1
                    Set-RDMSession -Session $session
                    update-rdmui
                    $updateResult = "Success"
                    $updatedDataSource = $dataSource.Name
                    
                    Write-Host "Updated ""NIC #1 vLAN"" to '$customfield1' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    $updateResult = "No Change Needed"
                    $updatedDataSource = $dataSource.Name

                    Write-Host "No Change needed for ""NIC #1 vLAN"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"

                # Check to see if CustomField2 (NIC #2 VLAN) needs to be updated or not.
                }
                if ($oldcustomfield2 -ne $customfield2) {
                    $session.MetaInformation.CustomField2Value = $customfield2
                    Set-RDMSession -Session $session
                    update-rdmui
                    $updateResult = "Success"
                    $updatedDataSource = $dataSource.Name
                    
                    Write-Host "Updated ""NIC #2 vLAN"" to '$customfield2' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"

                } else {
                    $updateResult = "No Change Needed"
                    $updatedDataSource = $dataSource.Name

                    Write-Host "No Change needed for ""NIC #2 vLAN"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"

                # Check to see if CustomField3 (NIC #3 VLAN) needs to be updated or not.
                }
                if ($oldcustomfield3 -ne $customfield3) {
                    $session.MetaInformation.CustomField3Value = $customfield3
                    Set-RDMSession -Session $session
                    update-rdmui
                    $updateResult = "Success"
                    $updatedDataSource = $dataSource.Name
                    
                    Write-Host "Updated ""NIC #3 vLAN"" to '$customfield3' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"

                } else {
                    $updateResult = "No Change Needed"
                    $updatedDataSource = $dataSource.Name

                    Write-Host "No Change needed for ""NIC #3 vLAN"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"

                }

                # Check to see if Tags needs to be updated or not.
                if ($oldClientTag -ne $ClientTag) {
                    $session.MetaInformation.Keywords = $ClientTag
                    Set-RDMSession -Session $session
                    update-rdmui
                    $updateResult = "Success"
                    $updatedDataSource = $dataSource.Name
                    
                    Write-Host "Updated ""Tags"" to '$ClientTag' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"

                } else {
                    $updateResult = "No Change Needed"
                    $updatedDataSource = $dataSource.Name

                    Write-Host "No Change needed for ""Tags"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"

                }

                # Check to see if Alternate hosts (IPv4 Address Reference) needs to be updated or not.
                if ($oldAlternateHosts -ne $AlternateHosts) {
                    $session.AlternateHosts = $AlternateHosts
                    Set-RDMSession -Session $session
                    update-rdmui
                    $updateResult = "Success"
                    $updatedDataSource = $dataSource.Name
                    
                    Write-Host "Updated ""Alternate Hosts"" to '$AlternateHosts' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                    Write-Host ""
                } else {
                    $updateResult = "No Change Needed"
                    $updatedDataSource = $dataSource.Name

                    Write-Host "No Change needed for ""Alternate Hosts"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                    Write-Host ""
                }

                $results += [PSCustomObject]@{
                    'Entry Name' = $deviceName
                    'Old NIC #1 vLAN' = $oldcustomfield1
                    'New NIC #1 vLAN' = if($updateResult -eq "Success") { $customfield1 } else { $null }
                    'Old NIC #2 vLAN' = $oldcustomfield2
                    'New NIC #2 vLAN' = if($updateResult -eq "Success") { $customfield2 } else { $null }
                    'Old NIC #3 vLAN' = $oldcustomfield3
                    'New NIC #3 vLAN' = if($updateResult -eq "Success") { $customfield3 } else { $null }
                    'Old Tag' = $oldClientTag
                    'New Tag' = if($updateResult -eq "Success") { $ClientTag } else { $null }
                    'Old Alternate Hosts' = $oldAlternateHosts
                    'New Alternate Hosts' = if($updateResult -eq "Success") { $AlternateHosts } else { $null }
                    'Update Result' = $updateResult
                    'Updated DataSource' = $updatedDataSource
                }
            }
        }
    }
}

# Export the results to a CSV file named with the current date
$timestamp = Get-Date -Format "MMddyyyy-HHmmss"
$outputFileName = "$($env:USERPROFILE)\Desktop\vCenter CSV Updates\update_results_$timestamp.csv"
$results | Export-Csv -Path $outputFileName -NoTypeInformation

Write-Host "Results have been exported to $outputFileName"


I just can't figure it out to save my life at this point, so I appreciate the assistance!

--- Chuck
Overgaard, AZ (-7 MST / Zulu Year-Round)
RDM Version: 2025.3.11.0 64-Bit - MSSQL - Daily Usage
RDM Version: 2025.2.28.0 64-Bit - MSSQL - VM

All Comments (6)

avatar

Hello Chuck,

To add a line for the 'Entry Not Found', you can include this else block in the if ($rdmsessions):

else {
    $results += [PSCustomObject]@{
        'Entry Name' = $deviceName
        'Old NIC #1 vLAN' = $null
        'New NIC #1 vLAN' = $null
        'Old NIC #2 vLAN' = $null
        'New NIC #2 vLAN' = $null
        'Old NIC #3 vLAN' = $null
        'New NIC #3 vLAN' = $null
        'Old Tag' = $null
        'New Tag' = $null
        'Update Result' = $updateResult
        'Updated DataSource' = $dataSource.Name
    }
}


Here are some thoughts on your script:

  • Order of Operations:
    • You should call Set-RDMSession after all modifications are done, right before adding the result to the results array.
  • Refreshing the Application:
    • Update-RDMUI refreshes the RDM application. It is not required. If you still wish to call it, do so after the foreach ($session in $rdmSessions) loop.
  • Update Result Variable:
    • Your $updateResult variable will only keep the result of the AlternateHost check. This does not seem like the expected behavior.


Let us know if you need any more help.

Best regards,
Maxime

avatar

I will work through the suggestions as soon as I can get this working now. :-)

I have updated the Results code as follows, and now the script doesn't work at all:

                $results += [PSCustomObject]@{
					'Entry Name'			= $deviceName
                    'Old NIC #1 vLAN'		= $oldcustomfield1
                    'New NIC #1 vLAN'		= if($updateResult -eq "Success") { $customfield1 } else { $null }
                    'Old NIC #2 vLAN'		= $oldcustomfield2
                    'New NIC #2 vLAN'		= if($updateResult -eq "Success") { $customfield2 } else { $null }
                    'Old NIC #3 vLAN'		= $oldcustomfield3
                    'New NIC #3 vLAN'		= if($updateResult -eq "Success") { $customfield3 } else { $null }
                    'Old Tag'				= $oldClientTag
                    'New Tag'				= if($updateResult -eq "Success") { $ClientTag } else { $null }
                    'Old Alternate Hosts'	= $oldAlternateHosts
                    'New Alternate Hosts'	= if($updateResult -eq "Success") { $AlternateHosts } else { $null }
                    'Old Is Virtual'		= $oldVirtual
                    'New Is Virtual'		= if($updateResult -eq "Success") { $Virtual } else { $null }
                    'Old vCenter'			= $oldvCenter
                    'New vCenter'			= if($updateResult -eq "Success") { $vCenter } else { $null }
                    'Old VM Type'			= $oldVMType
                    'New VMType'			= if($updateResult -eq "Success") { $VMType } else { $null }					
                    'Update Result'			= $updateResult
                    'Updated DataSource'	= $updatedDataSource
                } else {
				$results += [PSCustomObject]@{
					'Entry Name'			= $deviceName
					'Old NIC #1 vLAN'		= $null
					'New NIC #1 vLAN'		= $null
					'Old NIC #2 vLAN'		= $null
					'New NIC #2 vLAN'		= $null
					'Old NIC #3 vLAN'		= $null
					'New NIC #3 vLAN'		= $null
					'Old Tag'				= $null
					'New Tag'				= $null
					'Update Result'			= $updateResult
					'Updated DataSource'	= $dataSource.Name
					}
				}
            }
        }
    }
}


No matter where I put your code reference, I either get "Unexpected token 'else' in expression or statement", or "The term 'else' is not recognized as a name of a cmdlet, etc. etc. etc."

Where am I falsely putting this?

I am working on your suggestions as we speak.

--- Chuck
Overgaard, AZ (-7 MST / Zulu Year-Round)
RDM Version: 2025.3.11.0 64-Bit - MSSQL - Daily Usage
RDM Version: 2025.2.28.0 64-Bit - MSSQL - VM

avatar

The else block must be a little be lower:

                $results += [PSCustomObject]@{
                    'Entry Name' = $deviceName
                    'Old NIC #1 vLAN' = $oldcustomfield1
                    'New NIC #1 vLAN' = if($updateResult -eq "Success") { $customfield1 } else { $null }
                    'Old NIC #2 vLAN' = $oldcustomfield2
                    'New NIC #2 vLAN' = if($updateResult -eq "Success") { $customfield2 } else { $null }
                    'Old NIC #3 vLAN' = $oldcustomfield3
                    'New NIC #3 vLAN' = if($updateResult -eq "Success") { $customfield3 } else { $null }
                    'Old Tag' = $oldClientTag
                    'New Tag' = if($updateResult -eq "Success") { $ClientTag } else { $null }
                    'Old Alternate Hosts' = $oldAlternateHosts
                    'New Alternate Hosts' = if($updateResult -eq "Success") { $AlternateHosts } else { $null }
                    'Update Result' = $updateResult
                    'Updated DataSource' = $updatedDataSource
                } # -> PSCustomObject closure
            } # -> foreach ($sesion in $rdmSessions) closure
        } # -> if ($rdmSessions) close
        else {
				$results += [PSCustomObject]@{
					'Entry Name'			= $deviceName
					'Old NIC #1 vLAN'		= $null
					'New NIC #1 vLAN'		= $null
					'Old NIC #2 vLAN'		= $null
					'New NIC #2 vLAN'		= $null
					'Old NIC #3 vLAN'		= $null
					'New NIC #3 vLAN'		= $null
					'Old Tag'				= $null
					'New Tag'				= $null
					'Update Result'			= $updateResult
					'Updated DataSource'	= $dataSource.Name
					} # -> PSCustomObject closure
				} # else block closure
           } # foreach ($dataSource in $dataSourceList) closure
    } # foreach ($row in $csvdata) closure
avatar

OK, I have made the following changes, and seems to work correctly for running the actual script:

  • Order of Operations:
    • You should call Set-RDMSession after all modifications are done, right before adding the result to the results array.
      • I removed the numerous Set-RDMSession and added right above the $results += [PSCustomObject]@{ call.
  • Refreshing the Application:
    • Update-RDMUI refreshes the RDM application. It is not required. If you still wish to call it, do so after the foreach ($session in $rdmSessions) loop.
      • I forgot this updated the RDM application, and not the data source itself. This has been moved right before the Export-CSV function set.
  • Update Result Variable:
    • Your $updateResult variable will only keep the result of the AlternateHost check. This does not seem like the expected behavior.
      • This is the only thing left that I can't figure out how to get everything to write correctly in the completed CSV file when there is a change made.
$csvData = Import-Csv -Path "$($env:USERPROFILE)\Desktop\INPUT.csv"

# Fetch all RDM data sources
$dataSourceList	= Get-RDMDataSource

# Prepare a list to hold the results
$results = @()

# Iterate through each row in the CSV
foreach ($row in $csvData) {
    $deviceName				= $row.VM
    $customfield1				= $row.CustomField1Value
    $customfield2				= $row.CustomField2Value
    $customfield3				= $row.CustomField3Value
    $ClientTag					= $row.ClientCode
    $AlternateHosts			= $row.IPv4
	$Virtual						= $row.Virtual
	$vCenter						= $row.vCenter
	$VMType						= $row.VMType
    $oldcustomfield1			= $null
    $oldcustomfield2			= $null
    $oldcustomfield3			= $null
    $oldClientTag				= $null
    $oldAlternateHosts		= $null
	$oldVirtual					= $null
	$oldvCenter					= $null
	$oldVMType				= $null
    $updateResult				= "Entry Not Found / Entry Mismatch"
    $updatedDataSource	= $null
	
    Write-host "Checking For Entry: $deviceName"
    foreach ($dataSource in $dataSourceList) {
        Set-RDMCurrentDataSource -DataSource $dataSource
        $rdmSessions = Get-RDMSession -IncludeSubFolders | Where-Object { $_.Name -eq $deviceName }

        if ($rdmSessions) {
            foreach ($session in $rdmSessions) {
                $oldcustomfield1		= $session.MetaInformation.CustomField1Value
                $oldcustomfield2		= $session.MetaInformation.CustomField2Value
                $oldcustomfield3		= $session.MetaInformation.CustomField3Value
                $oldClientTag			= $session.MetaInformation.Keywords
                $oldAlternateHosts	= $session.AlternateHosts
				$oldVirtual				= $session.MetaInformation.IsVirtualMachine
				$oldvCenter				= $session.MetaInformation.VirtualMachineName
				$oldVMType			= $session.MetaInformation.VirtualMachineType

                # Check to see if CustomField1 (NIC #1 VLAN) needs to be updated or not.
                if ($oldcustomfield1 -ne $customfield1) {
                    $session.MetaInformation.CustomField1Value = $customfield1
                    $updateResult				= "Success"
                    $updatedDataSource	= $dataSource.Name
                    
                    Write-Host "Updated ""NIC #1 vLAN"" to '$customfield1' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    $updateResult				= "No Change Needed"
                    $updatedDataSource	= $dataSource.Name

                    Write-Host "No Change needed for ""NIC #1 vLAN"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }
				
                # Check to see if CustomField2 (NIC #2 VLAN) needs to be updated or not.				
                if ($oldcustomfield2 -ne $customfield2) {
                    $session.MetaInformation.CustomField2Value = $customfield2
                    $updateResult				= "Success"
                    $updatedDataSource	= $dataSource.Name
                    
                    Write-Host "Updated ""NIC #2 vLAN"" to '$customfield2' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    $updateResult				= "No Change Needed"
                    $updatedDataSource	= $dataSource.Name

                    Write-Host "No Change needed for ""NIC #2 vLAN"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }
				
                # Check to see if CustomField3 (NIC #3 VLAN) needs to be updated or not.				
                if ($oldcustomfield3 -ne $customfield3) {
                    $session.MetaInformation.CustomField3Value = $customfield3
                    $updateResult				= "Success"
                    $updatedDataSource	= $dataSource.Name
                    
                    Write-Host "Updated ""NIC #3 vLAN"" to '$customfield3' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    $updateResult				= "No Change Needed"
                    $updatedDataSource	= $dataSource.Name

                    Write-Host "No Change needed for ""NIC #3 vLAN"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if Tags needs to be updated or not.
                if ($oldClientTag -ne $ClientTag) {
                    $session.MetaInformation.Keywords = $ClientTag
                    $updateResult				= "Success"
                    $updatedDataSource	= $dataSource.Name
                    
                    Write-Host "Updated ""Tags"" to '$ClientTag' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    $updateResult				= "No Change Needed"
                    $updatedDataSource	= $dataSource.Name

                    Write-Host "No Change needed for ""Tags"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if Alternate hosts (IPv4 Address Reference) needs to be updated or not.
                if ($oldAlternateHosts -ne $AlternateHosts) {
                    $session.AlternateHosts = $AlternateHosts
                    $updateResult				= "Success"
                    $updatedDataSource	= $dataSource.Name
                    
                    Write-Host "Updated ""Alternate Hosts"" to '$AlternateHosts' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    $updateResult				= "No Change Needed"
                    $updatedDataSource	= $dataSource.Name

                    Write-Host "No Change needed for ""Alternate Hosts"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if Machine is marked Virtual.
                if ($oldVirtual -ne $Virtual) {
                    $session.MetaInformation.IsVirtualMachine = $Virtual
                    $updateResult				= "Success"
                    $updatedDataSource	= $dataSource.Name
                    
                    Write-Host "Updated ""Is virtual machine?"" to '$Virtual' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    $updateResult				= "No Change Needed"
                    $updatedDataSource	= $dataSource.Name

                    Write-Host "No Change needed for ""Is virtual machine?"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if vCenter Name needs to be updated or not.
                if ($oldvCenter -ne $vCenter) {
                    $session.MetaInformation.VirtualMachineName = $vCenter
                    $updateResult				= "Success"
                    $updatedDataSource	= $dataSource.Name
                    
                    Write-Host "Updated ""Is virtual machine?: Server"" to '$vCenter' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    $updateResult				= "No Change Needed"
                    $updatedDataSource	= $dataSource.Name

                    Write-Host "No Change needed for ""Is virtual machine?: Server"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if Virtual Type needs to be updated or not.
                if ($oldVMType -ne $VMType) {
                    $session.MetaInformation.VirtualMachineType = $VMType
                    $updateResult				= "Success"
                    $updatedDataSource	= $dataSource.Name
                    
                    Write-Host "Updated ""Is virtual machine?: Type"" to '$VMType' on RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                    Write-Host ""
                } else {
                    $updateResult				= "No Change Needed"
                    $updatedDataSource	= $dataSource.Name

                    Write-Host "No Change needed for ""Is virtual machine?: Type"" on RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                    Write-Host ""
                }

                    Set-RDMSession -Session $session

                $results += [PSCustomObject]@{
                    'Entry Name'					= $deviceName
                    'Old NIC #1 vLAN'		= $oldcustomfield1
                    'New NIC #1 vLAN'		= if($updateResult -eq "Success") { $customfield1 } else { $null }
                    'Old NIC #2 vLAN'		= $oldcustomfield2
                    'New NIC #2 vLAN'		= if($updateResult -eq "Success") { $customfield2 } else { $null }
                    'Old NIC #3 vLAN'		= $oldcustomfield3
                    'New NIC #3 vLAN'		= if($updateResult -eq "Success") { $customfield3 } else { $null }
                    'Old Tag'						= $oldClientTag
                    'New Tag'               		= if($updateResult -eq "Success") { $ClientTag } else { $null }
                    'Old Alternate Hosts'   	= $oldAlternateHosts
                    'New Alternate Hosts'   = if($updateResult -eq "Success") { $AlternateHosts } else { $null }
                    'Old Is Virtual'				= $oldVirtual
                    'New Is Virtual'				= if($updateResult -eq "Success") { $Virtual } else { $null }
                    'Old vCenter'					= $oldvCenter
                    'New vCenter'				= if($updateResult -eq "Success") { $vCenter } else { $null }
                    'Old VM Type'				= $oldVMType
                    'New VMType'				= if($updateResult -eq "Success") { $VMType } else { $null }					
                    'Update Result'    			= $updateResult
                    'Updated DataSource'	= $updatedDataSource
                } # -> PSCustomObject closure
            } # -> foreach ($sesion in $rdmSessions) closure
        } # -> if ($rdmSessions) close
        else {
				$results += [PSCustomObject]@{
					'Entry Name'				= $deviceName
					'Old NIC #1 vLAN'		= $null
					'New NIC #1 vLAN'		= $null
					'Old NIC #2 vLAN'		= $null
					'New NIC #2 vLAN'		= $null
					'Old NIC #3 vLAN'		= $null
					'New NIC #3 vLAN'		= $null
					'Old Tag'						= $null
					'New Tag'						= $null
                    'Old Is Virtual'				= $null
                    'New Is Virtual'				= $null
                    'Old vCenter'					= $null
                    'New vCenter'				= $null
                    'Old VM Type'				= $null
                    'New VMType'				= $null
					'Update Result'			= $updateResult
					'Updated DataSource'	= $dataSource.Name
					} # -> PSCustomObject closure
				} # else block closure
           } # foreach ($dataSource in $dataSourceList) closure
    } # foreach ($row in $csvdata) closure

Update-RDMUI
	
# Export the results to a CSV file named with the current date
$timestamp = Get-Date -Format "MMddyyyy-HHmmss"
$outputFileName = "$($env:USERPROFILE)\Desktop\vCenter CSV Updates\update_results_$timestamp.csv"
$results | Export-Csv -Path $outputFileName -NoTypeInformation

Write-Host "Results have been exported to $outputFileName"


It is reading the original CSV just fine for the entries I have listed. I can see it go through and update the screen if there is an entry made, or if there is no change needed. I also see it create the output CSV file with everything needed. Only problem I am running in to, is like you mentioned. The output file is not writing correctly. For instance...

If I make a change in the original CSV file, I see those changes on the screen as it finds the entry, but they are not written correctly to the output file.

If I run the exact same original CSV file again, then the output matches what the change was from the previous run.

So, now I just need a little bit more assistance on getting the $updateResult to correctly create the csv file changes.

My apologies for missing the } structure where the else needed to be. I was almost there, but gave up before I moved up anymore.

--- Chuck
Overgaard, AZ (-7 MST / Zulu Year-Round)
RDM Version: 2025.3.11.0 64-Bit - MSSQL - Daily Usage
RDM Version: 2025.2.28.0 64-Bit - MSSQL - VM

avatar

A solution is to create the result object at the beginning with both the old and new values. Before saving the session, you should check if any changes are needed. If changes are required, proceed to save the session. If no changes are needed, remove all new values.

Example:

$csvData = Import-Csv -Path "C:\dev\Test\ForumTopic42172.csv"

# Fetch all RDM data sources
$dataSourceList	= Get-RDMDataSource

# Prepare a list to hold the results
$results = @()

# Iterate through each row in the CSV
foreach ($row in $csvData) {
    $deviceName				= $row.VM
    $customfield1				= $row.CustomField1Value
    $customfield2				= $row.CustomField2Value
    $customfield3				= $row.CustomField3Value
    $ClientTag					= $row.ClientCode
    $AlternateHosts			= $row.IPv4
	$Virtual						= $row.Virtual
	$vCenter						= $row.vCenter
	$VMType						= $row.VMType
    $oldcustomfield1			= $null
    $oldcustomfield2			= $null
    $oldcustomfield3			= $null
    $oldClientTag				= $null
    $oldAlternateHosts		= $null
	$oldVirtual					= $null
	$oldvCenter					= $null
	$oldVMType				= $null
    $updateResult				= "Entry Not Found / Entry Mismatch"
    $updatedDataSource	= $null
	
    Write-host "Checking For Entry: $deviceName"
    foreach ($dataSource in $dataSourceList) {
        Set-RDMCurrentDataSource -DataSource $dataSource
        $rdmSessions = Get-RDMSession -IncludeSubFolders | Where-Object { $_.Name -eq $deviceName }

        if ($rdmSessions) {
            foreach ($session in $rdmSessions) {
                $oldcustomfield1		= $session.MetaInformation.CustomField1Value
                $oldcustomfield2		= $session.MetaInformation.CustomField2Value
                $oldcustomfield3		= $session.MetaInformation.CustomField3Value
                $oldClientTag			= $session.MetaInformation.Keywords
                $oldAlternateHosts		= $session.AlternateHosts
		$oldVirtual			= $session.MetaInformation.IsVirtualMachine
		$oldvCenter			= $session.MetaInformation.VirtualMachineName
		$oldVMType			= $session.MetaInformation.VirtualMachineType

		$result = [PSCustomObject]@{
                    'Entry Name'		= $deviceName
                    'Old NIC #1 vLAN'		= $oldcustomfield1
                    'New NIC #1 vLAN'		= $customfield1
                    'Old NIC #2 vLAN'		= $oldcustomfield2
                    'New NIC #2 vLAN'		= $customfield2
                    'Old NIC #3 vLAN'		= $oldcustomfield3
                    'New NIC #3 vLAN'		= $customfield3
                    'Old Tag'			= $oldClientTag
                    'New Tag'               	= $ClientTag
                    'Old Alternate Hosts'   	= $oldAlternateHosts
                    'New Alternate Hosts'   	= $AlternateHost
                    'Old Is Virtual'		= $oldVirtual
                    'New Is Virtual'		= $Virtual
                    'Old vCenter'		= $oldvCenter
                    'New vCenter'		= $vCenter
                    'Old VM Type'		= $oldVMType
                    'New VMType'		= $VMType					
                    'Update Result'    		= "No Change Needed"
                    'Updated DataSource'	= $dataSource.Name
                }

                # Check to see if CustomField1 (NIC #1 VLAN) needs to be updated or not.
                if ($oldcustomfield1 -ne $customfield1) {
                    $session.MetaInformation.CustomField1Value = $customfield1
                    $result.'Update Result'				= "Success"
                    
                    Write-Host "Updated ""NIC #1 vLAN"" to '$customfield1' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    Write-Host "No Change needed for ""NIC #1 vLAN"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }
				
                # Check to see if CustomField2 (NIC #2 VLAN) needs to be updated or not.				
                if ($oldcustomfield2 -ne $customfield2) {
                    $session.MetaInformation.CustomField2Value = $customfield2
                    $result.'Update Result'				= "Success"
                    
                    Write-Host "Updated ""NIC #2 vLAN"" to '$customfield2' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    Write-Host "No Change needed for ""NIC #2 vLAN"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }
				
                # Check to see if CustomField3 (NIC #3 VLAN) needs to be updated or not.				
                if ($oldcustomfield3 -ne $customfield3) {
                    $session.MetaInformation.CustomField3Value = $customfield3
                    $result.'Update Result'				= "Success"
                    
                    Write-Host "Updated ""NIC #3 vLAN"" to '$customfield3' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    Write-Host "No Change needed for ""NIC #3 vLAN"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if Tags needs to be updated or not.
                if ($oldClientTag -ne $ClientTag) {
                    $session.MetaInformation.Keywords = $ClientTag
                    $result.'Update Result'				= "Success"
                    
                    Write-Host "Updated ""Tags"" to '$ClientTag' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    Write-Host "No Change needed for ""Tags"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if Alternate hosts (IPv4 Address Reference) needs to be updated or not.
                if ($oldAlternateHosts -ne $AlternateHosts) {
                    $session.AlternateHosts = $AlternateHosts
                    $result.'Update Result'				= "Success"
                    
                    Write-Host "Updated ""Alternate Hosts"" to '$AlternateHosts' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    Write-Host "No Change needed for ""Alternate Hosts"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if Machine is marked Virtual.
                if ($oldVirtual -ne $Virtual) {
                    $session.MetaInformation.IsVirtualMachine = $Virtual
                    $result.'Update Result'				= "Success"
                    
                    Write-Host "Updated ""Is virtual machine?"" to '$Virtual' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    Write-Host "No Change needed for ""Is virtual machine?"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if vCenter Name needs to be updated or not.
                if ($oldvCenter -ne $vCenter) {
                    $session.MetaInformation.VirtualMachineName = $vCenter
                    $result.'Update Result'				= "Success"
                    
                    Write-Host "Updated ""Is virtual machine?: Server"" to '$vCenter' for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                } else {
                    Write-Host "No Change needed for ""Is virtual machine?: Server"" for RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                }

                # Check to see if Virtual Type needs to be updated or not.
                if ($oldVMType -ne $VMType) {
                    $session.MetaInformation.VirtualMachineType = $VMType
                    $result.'Update Result'				= "Success"
                    
                    Write-Host "Updated ""Is virtual machine?: Type"" to '$VMType' on RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                    Write-Host ""
                } else {
                    Write-Host "No Change needed for ""Is virtual machine?: Type"" on RDM Entry '$deviceName' in data source '$($dataSource.Name)'"
                    Write-Host ""
                }
		
		if ($result.'Update Result' -eq "Success") {
                    Set-RDMSession -Session $session
		} elseif ($result.'Update Result' -eq 'No Change Needed') {
                    $result.'New NIC #1 vLAN' = $null
                    $result.'New NIC #2 vLAN' = $null
                    $result.'New NIC #3 vLAN' = $null
                    $result.'New Tag' = $null
                    $result.'New Is Virtual' = $null
                    $result.'New vCenter' = $null
                    $result.'New VMType' = $null
		}

                $results += $result
            } # -> foreach ($sesion in $rdmSessions) closure
        } # -> if ($rdmSessions) close
        else {
		$results += [PSCustomObject]@{
		    'Entry Name'			= $deviceName
		    'Old NIC #1 vLAN'		= $null
		    'New NIC #1 vLAN'		= $null
		    'Old NIC #2 vLAN'		= $null
		    'New NIC #2 vLAN'		= $null
		    'Old NIC #3 vLAN'		= $null
		    'New NIC #3 vLAN'		= $null
		    'Old Tag'			= $null
		    'New Tag'			= $null
            	    'Old Is Virtual'		= $null
                    'New Is Virtual'		= $null
                    'Old vCenter'		= $null
                    'New vCenter'		= $null
                    'Old VM Type'		= $null
                    'New VMType'		= $null
		    'Update Result'		= $updateResult
		    'Updated DataSource'	= $dataSource.Name
		} # -> PSCustomObject closure
	} # else block closure
    } # foreach ($dataSource in $dataSourceList) closure
} # foreach ($row in $csvdata) closure

Update-RDMUI
	
# Export the results to a CSV file named with the current date
$timestamp = Get-Date -Format "MMddyyyy-HHmmss"
$outputFileName = "C:\dev\Test\update_results_$timestamp.csv"
$results | Export-Csv -Path $outputFileName -NoTypeInformation

Write-Host "Results have been exported to $outputFileName"


avatar

You are a rock star, sir! That last little tid bid was all I needed.

It threw me off for a couple minutes though cuz I thought I was seeing things and it wasn't working.

So with the latest update, if there is absolutely no change needed, I see this result:


If there is anything in any column that needs an update, this is what I see:


I did a test on that last one and just changed the "Old/New Tag" and that's what was throwing me off. Just one simple change will make it reprint all columns. Which at this point, I can live with! It's kind of nice actually, showing the complete entry and then one thing that changed, so we can keep track.

This has been an awesome couple of days with y'all's help. Enjoy the weekend and as always, thanks for the assistance to your customers and community!

--- Chuck
Overgaard, AZ (-7 MST / Zulu Year-Round)
RDM Version: 2025.3.11.0 64-Bit - MSSQL - Daily Usage
RDM Version: 2025.2.28.0 64-Bit - MSSQL - VM

8d18a717-c0b7-474b-a26f-a8876f867725.png

8ac4b030-0455-44da-8f32-2610dba3bbbc.png