Hello All,
I am currently trying to understand how to script for PowerCLI. I am trying to create a script that will create resource pools based on user names from active directory. At the moment I don't have access to the server I will being running on so I am trying to get the framework of the script down. Here is what I have so far:
#Specify vCenter, vCenter username, and vCenter password
$servername = " "
$username = " "
$pass = " "
Write-Host "Connecting......."
Connect -VIServer - server[$servername] -User[$username] -password[$pass]
Write-Host "Connected"
Write-Host " "
#Command to pull data from Active directory domain.
Get-VIAccount -Domain " "
# Create Resource Pool cmdlet
New-ResoucePool -Location <VIContainer> -LastNameFirstName <String>
[-CpuExpandableReservation[$false]][-CpuLimitMhz <Int64>]
[-CpuReservationMhz 500] [-CpuSharesLevel High]
[-MemExpandableReservation $false] [-MemLimitMB<Int64>]
[-MemLimitGB <Decimal>] [-MemReservationMB <Int64>]
[-MemReservationGB 5] [-MemSharesLevel High]
[-NumCpuShares <Int32>] [-NumMemShares <Int32>] [-Server <VIServer[]>]
[-WhatIf] [-Confirm] [<CommonParameters>]
Any help would be greatly appreciated
There are a couple of issues in that code.
Try this
$csv_data =Import-Csv "Desktop\names.csv" -Header first,last
foreach($line in $csv_data){
New-ResoucePool -Location $VMHost -Name "$($line.last)$($line.first)"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD should be able to help you!
Do you really want to make a resourcepool for each user in AD?
Strange.
In any case, a skeleton (without adding resources setting for each resourcepool) could look like this
$cluster = Get-Cluster -Name YourCluster
Get-VIAccount -Domain YourDomain |
ForEach-Object -Process {
New-ResourcePool -Name $_.Name.Split('/')[1] -Location $cluster -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I checked and it doesn't have to create from each user in AD. It had to use AD for authentication. So I went back and wrote what I thought was a simple script but it keeps giving me errors that I can't seem to understand exactly how to resolve. This is the code I came up with after learning it was from a .csv
Connect-VIServer -Server vcenter.nmsu.edu -Protocol https -User Administrator -Password ###### (not gonna put here but just a placeholder)
$csv_data =Import-Csv "Desktop\names.csv" -headers first, last
foreach($line in $csv_data){
New-ResoucePool -Location VMHost -Name $($line.last)- $($line.first)
}
P.S The resource Pools have to been named LastnameFirstname and the .csv file is a list of names in the format first name last name.
I am trying to understand PowerCLI, but I thought it was time to reach out again for help.
There are a couple of issues in that code.
Try this
$csv_data =Import-Csv "Desktop\names.csv" -Header first,last
foreach($line in $csv_data){
New-ResoucePool -Location $VMHost -Name "$($line.last)$($line.first)"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD
If I wanted to assign admin permissions to the resourcepool based on the username would the code look somewhat like this?:
New-VIPermission -entity $resourcepool -Principal $($line.username) -role Administrator
You don't need to do variable substitution in this case, the field Username is already a string.
Also the role is Admin, unless you created a new role with the name Administrator.
You can just do
New-VIPermission -Entity $resourcepool -Principal $line.username -Role Admin
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Awesome, thank you so much for your help LucD. I'm new to use PowerCLI so I am still trying to grasp the syntax of it.