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
$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 }
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
Thanks for viewing
Aman Dhally
Great resource! Thanks for sharing, I found it valuable.
ReplyDeletecloud hosting provider
if(-not $(get-module -ListAvailable -Name "ActiveDirectory")){
ReplyDelete$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;
}
}