Powershell script: Copy DEV Layers to LIVE

The following Powershell script copies the AX2009 DEV Layers to LIVE thereby checking that the services have been stopped and also copying the previous layers into old and backing up old in subfolders of old:

[void][System.Reflection.Assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
$AXSERVER_DEV = "srvaos2"
$AXSERVICE_DEV = "AOS50?01" # ? stands for $
$AXPATH_DEV = "\\"+ $AXSERVER_DEV + "\c$\Program Files\Microsoft Dynamics AX\50\Application\Appl\AX09_DEV"
$AXPATH_DEV_OLD = "\\"+ $AXSERVER_DEV + "\c$\Program Files\Microsoft Dynamics AX\50\Application\Appl\AX09_DEV\old"
$AXSERVER_PROD = "srvaos1"
$AXSERVICE_PROD = "AOS50?02" # ? stands for $
$AXPATH_PROD = "\\"+ $AXSERVER_PROD + "\c$\Program Files\Microsoft Dynamics AX\50\Application\Appl\AX09_LIVE"
$AXPATH_PROD_OLD = "\\"+ $AXSERVER_PROD + "\c$\Program Files\Microsoft Dynamics AX\50\Application\Appl\AX09_LIVE\old"
$LABELFILES = "axsa*.ald"
$LAYERFILES = "axcus*.aod"
$AOIFILES = "*.aoi"
$ALIFILES = "*.ali"
function copyAXAppl
{
#DEV and PROD services must be stopped	
	echo ("checking service " + $AXSERVICE_DEV + " on " + $AXSERVER_DEV)
	if((get-service -ComputerName $AXSERVER_DEV -name $AXSERVICE_DEV).status -eq "Stopped")
	{
		echo "ok" ("checking service " + $AXSERVICE_PROD + " on " + $AXSERVER_PROD) 
		if((get-service -ComputerName $AXSERVER_PROD -name $AXSERVICE_PROD).status -eq "Stopped")
		{
			echo "ok" ("copying from " + $AXPATH_DEV + " to " + $AXPATH_PROD)
#CREATE PROD\OLD BACKUP DIR (YYYYMMDD)
			if(-not (test-path (join-path $AXPATH_PROD_OLD (get-date -uformat "%Y%m%d"))))
			{
				new-item -path $AXPATH_PROD_OLD -name (get-date -uformat "%Y%m%d") -itemtype directory
			}			
#BACKUP PROD (only CUS Files)
			(dir -path $AXPATH_PROD -name $LABELFILES ) |
				foreach-object {copy $_.pspath (join-path $AXPATH_PROD_OLD (get-date -uformat "%Y%m%d")) }
			(dir -path $AXPATH_PROD -name $LAYERFILES) |
				foreach-object {copy $_.pspath (join-path $AXPATH_PROD_OLD (get-date -uformat "%Y%m%d")) }
#COPY PROD\OLD\{14 days old} -> PROD\OLD
			if(test-path (join-path $AXPATH_PROD_OLD (get-date (get-date).addDays(-14) -uformat "%Y%m%d")))
			{
				(dir -path (join-path $AXPATH_PROD_OLD (get-date (get-date).addDays(-14) -uformat "%Y%m%d")) -name "*.ald" ) |
					foreach-object {copy $_.pspath $AXPATH_PROD_OLD }
				(dir -path (join-path $AXPATH_PROD_OLD (get-date (get-date).addDays(-14) -uformat "%Y%m%d")) -name "*.aod" ) |
					foreach-object {copy $_.pspath $AXPATH_PROD_OLD }
			}
#CREATE DEV\OLD BACKUP DIR (YYYYMMDD)
			if(-not (test-path (join-path $AXPATH_DEV_OLD (get-date -uformat "%Y%m%d"))))
			{
				new-item -path $AXPATH_DEV_OLD -name (get-date -uformat "%Y%m%d") -itemtype directory
			}			
#BACKUP DEV (only CUS Files)
			(dir -path $AXPATH_DEV -name $LABELFILES ) |
				foreach-object {copy $_.pspath (join-path $AXPATH_DEV_OLD (get-date -uformat "%Y%m%d")) }
			(dir -path $AXPATH_DEV -name $LAYERFILES) |
				foreach-object {copy $_.pspath (join-path $AXPATH_DEV_OLD (get-date -uformat "%Y%m%d")) }
#COPY DEV -> DEVOLD
			(dir -path $AXPATH_DEV -name "*.ald" ) |
				foreach-object {copy $_.pspath $AXPATH_DEV_OLD }
			(dir -path $AXPATH_DEV -name "*.aod" ) |
				foreach-object {copy $_.pspath $AXPATH_DEV_OLD }

#COPY DEV->PROD (only CUS Files)
			(dir -path $AXPATH_DEV -name $LABELFILES ) |
				foreach-object {copy $_.pspath $AXPATH_PROD }
			(dir -path $AXPATH_DEV -name $LAYERFILES ) |
				foreach-object {copy $_.pspath $AXPATH_PROD }
#DELETE PROD AOI
			(dir -path $AXPATH_PROD -name $AOIFILES ) |
				foreach-object {remove-item $_.pspath}
#DELETE DEV AOI
			(dir -path $AXPATH_DEV -name $AOIFILES ) |
				foreach-object {remove-item $_.pspath}
#DELETE PROD ALI
			(dir -path $AXPATH_PROD -name $ALIFILES ) |
				foreach-object {remove-item $_.pspath}
#DELETE DEV ALI
			(dir -path $AXPATH_DEV -name $ALIFILES ) |
				foreach-object {remove-item $_.pspath}
		}
		else
		{
			[System.Windows.Forms.MessageBox]::Show("First stop '" + (get-service -name AOS50?02).Displayname + "'")
		}
	}
	else
	{
		[System.Windows.Forms.MessageBox]::Show("First stop '" + (get-service -name AOS50?01).Displayname + "'")
	}
}

Edit 20.06.2014: variable servers

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s