Powershell Restore-WebConfiguration script help

I have an odd use case and I was wondering if this is possible... I am making a script for a VDI desktop running Visual Studio with a local copy of IIS. I want to be able to backup IIS on logoff (not an issue) and then on log in, restore. On Login, the session will be on a fresh VM so the %windir%\system32\intsrv\backup directory doesn't exist. I would like the script to do the following.

option A:
1. repoint to a different network location to look for the backup like K:\IISBackup (I don't see this as an option in the args for the command Restore-WebConfiguration
2. Restore the very latest version in the directory.

option B:
1. xcopy the very latest version of IIS backup from K:\IISBackup to %windir%\system32\inetsrv\backup
2. Restore the very latest version in the directory.


the biggest problem I'm having is to programmatically find the latest backup in a directory.
LVL 1
jbla9028Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose TorresCertified Database AdministratorCommented:
We are now starting to convert our scripts to powershell.
These are the batch commands we are currently using.

Backup IIS - Change directories to suit your needs
rem Delete IIS backup directory if exists

IF EXIST "C:\Windows\System32\inetsrv\backup\IIS_Config_Backup\" RD /S /Q "C:\Windows\System32\inetsrv\backup\IIS_Config_Backup"
IF EXIST "C:\DisasterRecovery\Backup_IIS\Backup\" RD /S /Q "C:\DisasterRecovery\Backup_IIS\Backup"

rem Backup IIS Configuration

C:\Windows\System32\inetsrv\APPCMD.EXE ADD BACKUP IIS_Config_Backup

rem Copy IIS_Config_Backup to C:\DisasterRecovery\Backup_IIS\Backup folder

ROBOCOPY C:\Windows\System32\inetsrv\backup\IIS_Config_Backup C:\DisasterRecovery\Backup_IIS\Backup *.* /E

Open in new window


Restore IIS - Change directories to suit your needs
rem Delete IIS backup directory if exists

IF EXIST "C:\Windows\System32\inetsrv\backup\IIS_Config_Backup\" RD /S /Q "C:\Windows\System32\inetsrv\backup\IIS_Config_Backup"

rem Copy C:\DisasterRecovery\Backup_IIS\Backup to C:\Windows\System32\inetsrv\backup

ROBOCOPY C:\DisasterRecovery\Backup_IIS\Backup C:\Windows\System32\inetsrv\backup\IIS_Config_Backup *.* /E

rem Restore IIS Configuration

C:\Windows\System32\inetsrv\APPCMD.EXE RESTORE BACKUP IIS_Config_Backup

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jbla9028Author Commented:
great, this worked. I also completed with powershell.

###backup###
Import-Module WebAdministration
$dt=Get-Date -format "MMddyyyHHmmss"
$username=[environment]::username
remove-item -Force -Recurse c:\windows\System32\inetsrv\backup
Backup-WebConfiguration -Name IIS-Backup
$dir = "c:\windows\System32\inetsrv\backup\IIS-Backup"
robocopy c:\windows\System32\inetsrv\backup\IIS-Backup  k:\IISBackup\$dt /MIR
############################################################

###Restore###
Import-Module WebAdministration
$dt=Get-Date -format "MMddyyyHHmm"
Backup-WebConfiguration -Name $dt
$dir = "\\domain\public\ITShare\_temp\IISBackup"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name
robocopy $latest.Fullname  C:\Windows\System32\inetsrv\backup\$latest /MIR
stop-service  IISADMIN
stop-service W3SVC
Restore-WebConfiguration -Name $latest
stop-service  IISADMIN
Start-Service W3SVC

#############################################
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft IIS Web Server

From novice to tech pro — start learning today.