User Tools

Site Tools


vrealize:code:changeip

Change IP of a Deployed VM

If you have changed the IP of a VM after deployment, vRA does not update the IP database, and you will have trouble when you later delete this vm. To change an IP of a VM, you can add this Stored Procedure to the orginal MSSQL database for vRA. NOTE: This is unsupported by VMware, and you might end up in trouble later. However this has been working perfectly for me from vRA 7.3 to 7.6.Be aware that the new IP must be within a network range allready allocated in vRA.

To use it, open a query window to the vRA database and run it with: exec Custom_ReassignVMToNewNetwork 'NameOfVm' 'OldIP' 'NewIP'

USE [VRA]
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE OR ALTER FUNCTION [dbo].[ConvertIPToLong](@IP VARCHAR(15))
RETURNS BIGINT
AS
BEGIN
    DECLARE @Long BIGINT
    SET @Long = CONVERT(BIGINT, PARSENAME(@IP, 4)) * 256 * 256 * 256 +
        CONVERT(BIGINT, PARSENAME(@IP, 3)) * 256 * 256 +
        CONVERT(BIGINT, PARSENAME(@IP, 2)) * 256 +
        CONVERT(BIGINT, PARSENAME(@IP, 1))
 
    RETURN (@Long)
END
GO
 
/****** Object:  StoredProcedure [dbo].[Custom_ReassignVMToNewNetwork]    Script Date: 08.04.2019 11:49:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
-- ============================================================
-- Description:	Re-assign a vm to a new Network Reservation ===
-- ============================================================
CRETE OR ALTER PROCEDURE [dbo].[Custom_ReassignVMToNewNetwork]
	@ServerName AS nvarchar(256),
	@OldIP AS nvarchar(15),
	@NewIP AS nvarchar(15)
AS
BEGIN
	SET NOCOUNT ON;
 
	DECLARE @C AS INT
	DECLARE @VirtualMachineID AS uniqueidentifier
	DECLARE @OLDStaticIPv4AddressID AS uniqueidentifier
	DECLARE @OLDStaticIPv4NetworkProfile AS uniqueidentifier
	DECLARE @OLDStaticIPv4Range AS uniqueidentifier
	DECLARE @OLDStaticIPv4NetworkProfileName AS nvarchar(255)
	DECLARE @OLDStaticIPv4RangeName AS nvarchar(255)
	DECLARE @OLDSubnetMaskIPv4 AS nvarchar(255)
	DECLARE @OLDGatewayIPv4Address AS nvarchar(255)
	DECLARE @OLDPrimaryDNSIPv4Address AS nvarchar(255)
	DECLARE @OLDSecondaryDNSIPv4Address AS nvarchar(255)
	DECLARE @OLDDnsSuffix AS nvarchar(255)
	DECLARE @OLDDnsSearchSuffix AS nvarchar(255)
	DECLARE @VirtualInterface AS nvarchar(128)
 
	DECLARE @OLDIPSortMask AS BIGINT
	DECLARE @NEWIPSortMask AS BIGINT
	DECLARE @NEWStaticIPv4NetworkProfile AS uniqueidentifier
	DECLARE @NEWStaticIPv4Range AS uniqueidentifier
	DECLARE @NEWStaticIPv4NetworkProfileName AS nvarchar(255)
	DECLARE @NEWSubnetMaskIPv4 AS nvarchar(255)
	DECLARE @NEWGatewayIPv4Address AS nvarchar(255)
	DECLARE @NEWPrimaryDNSIPv4Address AS nvarchar(255)
	DECLARE @NEWSecondaryDNSIPv4Address AS nvarchar(255)
	DECLARE @NEWDnsSuffix AS nvarchar(255)
	DECLARE @NEWDnsSearchSuffix AS nvarchar(255)
 
 
 
 
    -- First Find the VM
	SELECT @C = COUNT(*) FROM VirtualMachine WHERE VirtualMachineName = @ServerName
	IF @C != 1 
	BEGIN
		SELECT 'VM not found, or multiple VMs with same name!' AS ErrorText
		RETURN
	END
	-- Get vRAs internal ID of the VM
 
	SELECT @VirtualMachineID=VirtualMachineID FROM VirtualMachine WHERE VirtualMachineName = @ServerName
 
	-- Verify that the Old IP spesified is actually the IP allocated for this VM.
	SELECT @OLDIPSortMask= dbo.[ConvertIPToLong] (@OldIP)
 
	SELECT @C=COUNT(*) FROM StaticIPv4Address WHERE VirtualMachineID=@VirtualMachineID AND IPSortValue=@OLDIPSortMask
	IF @C != 1
	BEGIN
		SELECT 'VM name and VM ipaddress is not connected in vRA, will not update!' AS ErrorText
		RETURN
	END
 
	-- Get the OLD data incase we need to roleback
	SELECT @OLDStaticIPv4AddressID=Sip.ID,
		@OLDStaticIPv4NetworkProfileName=Snp.StaticIPv4NetworkProfileName,
		@OLDStaticIPv4NetworkProfile=Snp.ID,
		@OLDStaticIPv4Range=Sra.ID,
		@OLDStaticIPv4RangeName=Sra.StaticIPv4RangeName,
		@OLDGatewayIPv4Address=Snp.GatewayIPv4Address,
		@OLDSubnetMaskIPv4=Snp.SubnetMaskIPv4,
		@OLDPrimaryDNSIPv4Address=Snp.PrimaryDNSIPv4Address,
		@OLDSecondaryDNSIPv4Address=Snp.SecondaryDNSIPv4Address,
		@OLDDnsSuffix=Snp.DNSSuffix,
		@OLDDnsSearchSuffix=Snp.DnsSearchSuffix FROM StaticIPv4Address  AS Sip JOIN StaticIPv4NetworkProfile AS Snp ON Sip.StaticIPv4NetworkProfileID=Snp.ID JOIN StaticIPv4Range AS Sra ON Sip.StaticIPv4RangeID=Sra.ID WHERE VirtualMachineID=@VirtualMachineID
 
		-- Output Old data 
		SELECT @VirtualMachineID,@OLDStaticIPv4AddressID,@OLDStaticIPv4NetworkProfile,@OLDStaticIPv4Range,@OLDStaticIPv4NetworkProfileName,@OLDStaticIPv4RangeName,@OLDSubnetMaskIPv4,@OLDGatewayIPv4Address,@OLDPrimaryDNSIPv4Address,@OLDSecondaryDNSIPv4Address,@OLDDnsSuffix,@OLDDnsSearchSuffix
 
		-- Check the Custom Properties (Find witch VirtualMachine.Networkx.Address interface the Old IP belongs to
		-- (And extract the Number)
 
		SELECT @VirtualInterface=PropertyName FROM VirtualMachineProperties WHERE EntityID =@VirtualMachineID AND PropertyValue=@OldIP AND PropertyName LIKE 'VirtualMachine.Network%'
 
		WHILE PATINDEX('%[^0-9]%',@VirtualInterface) <> 0
		    SET @VirtualInterface = STUFF(@VirtualInterface,PATINDEX('%[^0-9]%',@VirtualInterface),1,'')
 
		IF @VirtualInterface IS NULL
		BEGIN
			SELECT 'Old IP is not found as Custom Properties, will not update custom properties for this vm.' AS StatusUpdate
		END
 
		-- First we need the SortMask of the new IP
		SELECT @NEWIPSortMask= dbo.[ConvertIPToLong] (@NewIP)
 
		-- Now verify that the IP is not allready allocated
		SELECT @C=COUNT(*) FROM StaticIPv4Address WHERE  IPSortValue=@NEWIPSortMask
		IF @C != 0 
		BEGIN
			SELECT 'NEW IP allready in use' AS ErrorText
			RETURN
		END
 
 
		-- Search all Network profiles, to find if we have a range created in vRA
		-- NOTE: This Query can cause some work on your SQL server, depending on how many Ranges you have. (Normally it should not be a problem)
		-- (Using Function in a where clause is not a good idea in general)
		SELECT @C=COUNT(*) FROM StaticIPv4Range WHERE ([dbo].[ConvertIPToLong](BeginIPv4Address) <= @NEWIPSortMask) AND ([dbo].[ConvertIPToLong](EndIPv4Address) >= @NEWIPSortMask)
		IF @C =0 
		BEGIN
			-- Add auto creation of a new range here later...
			SELECT 'No range found' AS ErrorText
			RETURN
		END
		IF @C != 1
		BEGIN
			SELECT 'Multiple Ranges has same IP scope. This is not supported by this Hack (vRA does support it, so no worries!)' AS ErrorText
			RETURN 
		END
 
		-- Now Fetch the RangeID and The Profile ID
		SELECT @NEWStaticIPv4Range=ID,@NEWStaticIPv4NetworkProfile=StaticIPv4NetworkProfileID FROM StaticIPv4Range WHERE ([dbo].[ConvertIPToLong](BeginIPv4Address) <= @NEWIPSortMask) AND ([dbo].[ConvertIPToLong](EndIPv4Address) >= @NEWIPSortMask)
 
		SELECT @NEWStaticIPv4Range,@NEWStaticIPv4NetworkProfile
 
		-- Fetch the DNS/Gateway stuff from the profile (we need this data to update the Custom Properties)
		SELECT @NEWStaticIPv4NetworkProfileName=StaticIPv4NetworkProfileName,
		@NEWGatewayIPv4Address=GatewayIPv4Address,
		@NEWSubnetMaskIPv4=SubnetMaskIPv4,
		@NEWPrimaryDNSIPv4Address=PrimaryDNSIPv4Address,
		@NEWSecondaryDNSIPv4Address=SecondaryDNSIPv4Address,
		@NEWDnsSuffix=DNSSuffix,
		@NEWDnsSearchSuffix=DnsSearchSuffix FROM StaticIPv4NetworkProfile WHERE ID=@NEWStaticIPv4NetworkProfile
 
 
		--
		DECLARE @TempPropName AS nvarchar(128)
		DECLARE @NewIPid uniqueidentifier  
		SET @NewIPid = NEWID()  
 
		-- Run as Tranaction
		BEGIN TRY
			BEGIN TRANSACTION
				-- Remove OLD IP
				DELETE FROM  StaticIPv4Address WHERE VirtualMachineID=@VirtualMachineID
 
				-- Allocate IP in Address Space (This is an internal vRA Stored Proc)
				EXEC usp_StaticIPv4AddressCreateAddress @NewIPid,@NEWStaticIPv4NetworkProfile,@NEWStaticIPv4Range,@VirtualMachineID,NULL,@NewIP, @NEWIPSortMask,0,NULL
 
				IF @VirtualInterface IS NOT NULL
				BEGIN
					-- Update Custom Properties
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.Address')
					UPDATE VirtualMachineProperties SET PropertyValue =@NewIP WHERE EntityID =@VirtualMachineID AND PropertyName=@TempPropName
			--		select * from VirtualMachineProperties where EntityID =@VirtualMachineID and PropertyName=@TempPropName
 
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.DnsSuffix')
					UPDATE VirtualMachineProperties SET PropertyValue =@NEWDnsSuffix WHERE EntityID =@VirtualMachineID AND PropertyName=@TempPropName
 
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.DnsSearchSuffixes')
					UPDATE VirtualMachineProperties SET PropertyValue =@NEWDnsSearchSuffix WHERE EntityID =@VirtualMachineID AND PropertyName=@TempPropName
 
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.Gateway')
					UPDATE VirtualMachineProperties SET PropertyValue =@NEWGatewayIPv4Address WHERE EntityID =@VirtualMachineID AND PropertyName=@TempPropName
 
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.SubnetMask')
					UPDATE VirtualMachineProperties SET PropertyValue =@NEWSubnetMaskIPv4 WHERE EntityID =@VirtualMachineID AND PropertyName=@TempPropName
 
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.Name')
					UPDATE VirtualMachineProperties SET PropertyValue =@NEWStaticIPv4NetworkProfileName WHERE EntityID =@VirtualMachineID AND PropertyName=@TempPropName
 
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.NetworkProfileName')
					UPDATE VirtualMachineProperties SET PropertyValue =@NEWStaticIPv4NetworkProfileName WHERE EntityID =@VirtualMachineID AND PropertyName=@TempPropName
 
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.PrimaryDns')
					UPDATE VirtualMachineProperties SET PropertyValue =@NEWPrimaryDNSIPv4Address WHERE EntityID =@VirtualMachineID AND PropertyName=@TempPropName
 
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.SecondaryDns')
					UPDATE VirtualMachineProperties SET PropertyValue =@NEWSecondaryDNSIPv4Address WHERE EntityID =@VirtualMachineID AND PropertyName=@TempPropName
 
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.MacAddress')
					SET @TempPropName  =CONCAT('VirtualMachine.Network',@VirtualInterface,'.PortID')
				END
			COMMIT
		END TRY
		BEGIN CATCH
			IF @@TRANCOUNT >0
				ROLLBACK TRAN
				SELECT ERROR_MESSAGE() AS ErrorText
				RETURN
		END CATCH
		SELECT 'VM IP address updated successfully'
 
 
END
vrealize/code/changeip.txt ยท Last modified: 2021/04/08 20:25 by vmware