Monday, February 13, 2012

Automate "Remote Server Administration Tools (RSAT)" Deployment using PowerShell.


HI,

These days I am working on a script which does few “Active directory” task for us. This was seems simple so for but then I have found a major hurdle comes.

Problem:

The script on which I am working will be run on newly formatted system which is having Windows7 installed. The problem was that this script will is using few “Active Directory” cmdlets. And the new installation of windows doesn’t have “RSAT” installed by default. So until or unless I don’t have “ActiveDirectory” module installed till then I can’t use my
 get-ADComputer ; get-ADComputerServiceAccount” cmdlets.

Then I thought there should be way to sort this out. So finally after spending whole day I found a way to do this within my PowerShell Script.

Solution:

The “Active Directory” module only available after installing the “Windows6.1-KB958830-x86-RefreshPkg.msu” package and then enable the “Active Directory Module for windows Powershell” in optional features.

The “Windows6.1-KB958830-x86-RefreshPkg.msu” is a “Microsoft update Standalone Package” and we can install these packages using wusa.exe {Windows Update Standalone installer}.


So after installation of the “Windows6.1-KB958830-x86-RefreshPkg.msu” we need to enable the “Active Directory Module for windows Powershell” in optional features for this we are using dism.exe { Deployment Image Servicing and Management tool}.

The Script:

·         I am placing “Windows6.1-KB958830-x86-RefreshPkg.msu” in a central location so that script the executable from it.
·         Normally I copied all the scripts in “c:\tools” folder and if that folder doesn’t exists it create it for us and write a message on screen that “C:\tools” is created.
o    $tool = test-Path C:\tools
if ($tool -eq $false ) {new-Item -Name "Tools" -Path c:\ -ItemType Directory; write-Host " folder "C:\Tools" created" -ForegroundColor Green }
·         Now it check if “Windows6.1-KB958830-x86-RefreshPkg.msu” file in exists in “C:\tools” folder and if it doesn’t exists it will copy the file from our central share folder  \\soft-server\SoftRes\$hotfix
o    hotfix = "Windows6.1-KB958830-x86-RefreshPkg.msu"
if ($testpath -eq $false) { Copy-Item \\dc3-del\PublicShare\$hotfix c:\tools\
Write-host "Files has Copied Sucessfully, Now I am going to  install the HOTFIX." -ForegroundColor RED
}
·         After copy the file from server it will install the files using wusa.exe
o    & wusa.exe "c:\tools\$hotfix" /quiet | out-null
·         After the installation finishes it will enable the “Active Directory PowerShell Module” feature in “windows Optional feature” using DISM
o    & dism.exe /Online /Enable-Feature /FeatureName:RemoteServerAdministrationTools  /FeatureName:RemoteServerAdministrationTools-Roles /FeatureName:RemoteServerAdministrationTools-Roles-AD  /FeatureName:RemoteServerAdministrationTools-Roles-AD-Powershell | Out-Null


:::::::::::::::::::::::::::::::;; Start of the script ############
$hotfix = "Windows6.1-KB958830-x86-RefreshPkg.msu"
$testpath =  test-path -path "c:\tools\$hotfix"
$tool = test-Path C:\tools
if ($tool -eq $false ) {new-Item -Name "Tools" -Path c:\ -ItemType Directory; write-Host " folder "C:\Tools" created" -ForegroundColor Green } 
if ($testpath -eq $false) { Copy-Item \\soft-server\SoftRes\$hotfix
c:\tools\
Write-host "Files has Copied Sucessfully, Now I am going to  install the HOTFIX." -ForegroundColor RED
}
Write-Host "Installing Remote Server Administration Tools...... " -ForegroundColor Yellow                  
& wusa.exe "c:\tools\$hotfix" /quiet | out-null
Write-Host "HotFix Installed, lets enable the Active Directory Features in "Optional Features"" -ForegroundColor "Green"
& dism.exe /Online /Enable-Feature /FeatureName:RemoteServerAdministrationTools  /FeatureName:RemoteServerAdministrationTools-Roles /FeatureName:RemoteServerAdministrationTools-Roles-AD  /FeatureName:RemoteServerAdministrationTools-Roles-AD-Powershell | Out-Null
Write-Host "Lets Import the Active directory PowerShell Module Now.." -BackgroundColor Green  -ForegroundColor Blue
Import-Module ActiveDirectory
write-Host "Module successfully Imported" -ForegroundColor Red -BackgroundColor White

########## End of the Script ############## 



Screenshots:
13-02-2012 20-41-20 
Note: I tested this script on Windows 7 machine and it works perfectly for me . you can download installation file of "Windows6.1-KB958830-x86-RefreshPkg.msu" from here http://www.microsoft.com/download/en/details.aspx?id=7887. Also this is a simple static script no error checking is enable in it.
 
Thanks for viewing
Aman Dhally
messenger_freak_adp1 (30)



2 comments:

  1. Great resource! Thanks for sharing, I found it valuable.
    cloud hosting provider

    ReplyDelete
  2. if(-not $(get-module -ListAvailable -Name "ActiveDirectory")){

    $hotfix = "Windows6.1-KB958830-x86-RefreshPkg.msu"
    if ($(test-path -path "c:\temp\rsat\$hotfix") -eq $false) {
    Copy-Item -container \\server.local\logon\packages\rsat\ -destination "c:\temp\rsat" -Recurse -Force -Verbose;
    wusa.exe "c:\temp\rsat\$hotfix" /quiet | out-null;
    dism.exe /Online /Enable-Feature /FeatureName:RemoteServerAdministrationTools /FeatureName:RemoteServerAdministrationTools-Roles /FeatureName:RemoteServerAdministrationTools-Roles-AD /FeatureName:RemoteServerAdministrationTools-Roles-AD-Powershell | Out-Null;
    remove-item "c:\temp\rsat" -Force -Recurse -Verbose;
    }
    }

    ReplyDelete

Note: Only a member of this blog may post a comment.