All things VM related
Just went to VCP bootcamp. I will be putting those notes from that week long training here. Its a LOT of information.
Find All VMs on a vCenter with PowerCLI |
|
I needed to find all VMs on a vCenter. PowerCLI is your go to tool. Be sure to load the PowerCLI module
## Add-PSSnapin VMware.VimAutomation.Core
<#
help vm
get-help Get-VM -full
get-help Connect-VIServer -full
get-help Disconnect-VIServer -full
#>
$WarningPreference = 'SilentlyContinue'
##populate list variable from file or query
$list = Invoke-Sqlcmd -ServerInstance <repo server> -Database <repo DB> -Query "SELECT * from server table"
$date = get-date
foreach($server in $list)
{
Try {
write-host "connecting to "$server.vCenterName -ForegroundColor yellow -BackgroundColor blue
##Connect to VCenter
Connect-VIServer -Server $($server.vCenterName) -User root -Password <enter password> -ErrorAction "stop"
write-host "finding VMs for "$server.vCenterName -ForegroundColor Blue -BackgroundColor white
##get all vms from VC
$list = get-vm | ?{$_.PowerState -eq 'PoweredOn'} | select name
write-host "inserting...." -ForegroundColor Red -BackgroundColor white
foreach($VM in $list)
{
Invoke-Sqlcmd -ServerInstance <repo server> -Database <repo DB> -Query "insert into repo_server(Servername,vCenter_M_StackID, Collection_Time) values('$($VM.Name)',$($Server.vCenter_M_StackID),'$date')"
}
write-host "disconnecting to "$server.vCenterName -ForegroundColor blue -BackgroundColor yellow
##Disconnect from VC
Disconnect-VIServer -Server * -confirm:$false
}
Catch [Exception]
{
#General Failure
Write-Host GENERAL FAILURE! for $db.vcentername -ForegroundColor Red
<#When I want to know the full exception type:#>
$errormessage = $Error[0].Exception.Message -replace "'",""
Write-Host $errormessage -BackgroundColor blue -ForegroundColor Yellow
}
}
## Add-PSSnapin VMware.VimAutomation.Core
<#
help vm
get-help Get-VM -full
get-help Connect-VIServer -full
get-help Disconnect-VIServer -full
#>
$WarningPreference = 'SilentlyContinue'
##populate list variable from file or query
$list = Invoke-Sqlcmd -ServerInstance <repo server> -Database <repo DB> -Query "SELECT * from server table"
$date = get-date
foreach($server in $list)
{
Try {
write-host "connecting to "$server.vCenterName -ForegroundColor yellow -BackgroundColor blue
##Connect to VCenter
Connect-VIServer -Server $($server.vCenterName) -User root -Password <enter password> -ErrorAction "stop"
write-host "finding VMs for "$server.vCenterName -ForegroundColor Blue -BackgroundColor white
##get all vms from VC
$list = get-vm | ?{$_.PowerState -eq 'PoweredOn'} | select name
write-host "inserting...." -ForegroundColor Red -BackgroundColor white
foreach($VM in $list)
{
Invoke-Sqlcmd -ServerInstance <repo server> -Database <repo DB> -Query "insert into repo_server(Servername,vCenter_M_StackID, Collection_Time) values('$($VM.Name)',$($Server.vCenter_M_StackID),'$date')"
}
write-host "disconnecting to "$server.vCenterName -ForegroundColor blue -BackgroundColor yellow
##Disconnect from VC
Disconnect-VIServer -Server * -confirm:$false
}
Catch [Exception]
{
#General Failure
Write-Host GENERAL FAILURE! for $db.vcentername -ForegroundColor Red
<#When I want to know the full exception type:#>
$errormessage = $Error[0].Exception.Message -replace "'",""
Write-Host $errormessage -BackgroundColor blue -ForegroundColor Yellow
}
}
Add NFS Share to VM via Powershell |
|
I need to add a NFS share to a VM so can put my database backups there. Here is what I found so far.
import-module servermanager
get-windowsfeature "FS-NFS-Service"
Add-WindowsFeature "FS-NFS-Service"
New-NfsShare -Name "Yo_Momma" -Path "C:\Yo_Momma" -AllowRootAccess $true -Permission readwrite -Authentication all
##connect to PowerCLI to add the datastore
Connect-VIServer -Server Servername -User root -Password <enter password> -ErrorAction "stop"
$VMHOST = "IP of vSphere host" # VMware vSphere host where to create the datastore
$NFSDatastore = "FILESERVER01-NFS_MOUNT" # name that you want to give
$NFSServer = "FQDN of NFS Server" # ip of fqdn of the NFS Server
$NFSSharename = "Yo_Momma" # Share created on the NFS Server
New-Datastore -Nfs -VMHost $VMHOST -Name $NFSDatastore -NfsHost $NFSServer -path $NFSSharename
import-module servermanager
get-windowsfeature "FS-NFS-Service"
Add-WindowsFeature "FS-NFS-Service"
New-NfsShare -Name "Yo_Momma" -Path "C:\Yo_Momma" -AllowRootAccess $true -Permission readwrite -Authentication all
##connect to PowerCLI to add the datastore
Connect-VIServer -Server Servername -User root -Password <enter password> -ErrorAction "stop"
$VMHOST = "IP of vSphere host" # VMware vSphere host where to create the datastore
$NFSDatastore = "FILESERVER01-NFS_MOUNT" # name that you want to give
$NFSServer = "FQDN of NFS Server" # ip of fqdn of the NFS Server
$NFSSharename = "Yo_Momma" # Share created on the NFS Server
New-Datastore -Nfs -VMHost $VMHOST -Name $NFSDatastore -NfsHost $NFSServer -path $NFSSharename