Sayfalar

12 Ekim 2010 Salı

PowerShell İle "Hyper-V Administrators" Yerel Grubu Oluşturmak

Diyelim ki firmanızda administrator yetkisinde olmayan kullanıcılar var. Bunların sadece Hyper-V üzerinde yönetim yetkisinde olması gerekiyor. Bunu etkili ve hızlı bir şekilde nasıl başarabiliriz? Bu yazıda bunu tartışıyoruz.

Kendi gereksiniminize göre özelleştirebileceğiniz aşağıdaki PowerShell script'i sayesinde bunu başarabilirsiniz:
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
 
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
 
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
clear-host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process
exit
}
 
# Create "Hyper-V Administrators" group
$LocalComputer = [ADSI] "WinNT://$env:computername"
$HvAdminGroup = $LocalComputer.create("Group", "Hyper-V Administrators")
$HvAdminGroup.setinfo()
 
# Get the SID for the newly created group
$HvAdminGroupSID = (gwmi Win32_Group | ?{$_.Name -eq "Hyper-V Administrators"}).sid
 
# Add current user to Hyper-V Administrators group
$fixedUserName = $myWindowsID.Name -replace "\\","/"
$HvAdminGroup.add("WinNT://$env:computername/$fixedUserName")
 
# Get the current AzMan store location from the registry
$AzManStoreLocation = (Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization").StoreLocation
 
# Open the AzMan store
$AzManStore = new-object -ComObject "AzRoles.AzAuthorizationStore"
$AzManStore.Initialize(2, $AzManStoreLocation)
 
# Handle the default Hyper-V AzMan store and the SCVMM AzMan store
if (@($AzManStore.Applications | ? {$_.Name -contains "Hyper-V services"}).count -eq 1)
{
$HyperVAzManStore = $AzManStore.OpenApplication("Hyper-V services")
}
elseif (@($AzManStore.Applications | ? {$_.Name -contains "Virtual Machine Manager"}).count -eq 1)
{
$HyperVAzManStore = $AzManStore.OpenApplication("Virtual Machine Manager")
}
else
{
Write-Host "Unable to find AzMan application group."
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit
}
 
# Get the administrator role from the Hyper-V service in the AzMan store
$HyperVAdministratorsRole = $HyperVAzManStore.OpenRoleAssignment("Administrator")
 
# Add the Hyper-V Admin group to the AzMan store
$HyperVAdministratorsRole.AddMember($HvAdminGroupSID)
$HyperVAdministratorsRole.Submit()

Bu script yerel olarak "Hyper-V Administrators" grubu oluşturarak bu gruba Hyper-V üzerinde yapılacak işlemlerde tam yetki verir. Siz de kullanıcılarınızı bu gruba dahil ederek sadece Hyper-V üzerinde tam yönetim yetkili olmalarını sağlayabilirsiniz. Script'in çalıştırılabilmesi için elbette ki yönetim yetkili kullanıcı olmalısınız.

Bu script uzaktan çalıştırılamaz; yani sadece Hyper-V sunucu üzerinde çalıştırabilirsiniz.
Bu script çalıştırıldıktan sonra sisteme SCVMM ile yönetim görevi için bağlanılırsa script'in yeniden çalıştırılası gerekir.

Bol kazançlı çalışmalar.

Hiç yorum yok:

Yorum Gönder