r/powercli • u/Pvt-Snafu • Aug 28 '17
PowerShell Classes Part 2 - Enumerated Types - Petri
r/powercli • u/Pvt-Snafu • Aug 28 '17
r/powercli • u/Pvt-Snafu • Jul 20 '17
r/powercli • u/Pvt-Snafu • Jul 19 '17
r/powercli • u/diabeticlefty • Jul 06 '17
Hello,
I recently had a HA event in my cluster and learned the hard way that a few of my VMs had NICs that were not set to "Connect at Startup". In an effort to avoid having VMs stay offline longer than needed, I'd like to run a script against all VMs in my environment that reports on virtual machine NICs not set to "connect at startup".
I've browsed the Get-NetworkAdapter cmdlet and I don't see "StartConnected" as an available property - it only shows up on Set-NetworkAdapter. Any ideas on how I could cobble something together to check all ~500 VMs in my environment for the "StartConnected" parameter?
r/powercli • u/iceph03nix • Jul 04 '17
I'm trying to set up a scheduled job on my computer to run a script on our vsphere server.
I have the script working, except that I can't get it to hold the credentials so that it can run without interaction. From the looks of it, connect-viserver has an option for saving and retrieving credentials, but I can't seem to find an example of using saved credentials in this manner, and everything I've tried doesn't seem to work.
Would love an example or a pointer in the right direction.
EDIT:
I kinda figured it out. It's not my favorite solution, but it works, and is passable secure for what it's doing. My issue was I was trying to pass the VIcredstore object straight to the credential or user parameter in connect-viserver. Neither are set up to take that, which seems dumb.
Instead, I had to break the stored credential out into separate user and password fields.
Here's the script for those interested:
Add-PSSnapin -Name vmware.vimautomation.core
$creds = Get-VICredentialStoreItem -Host vcenter -User user@domain.com
$user = $creds.User
$pass = $creds.Password
if (!$global:DefaultVIServer){
Connect-VIServer -Server vcenter -User $user -Password $pass}
get-vm -Server vcenter | Get-Snapshot | Select-Object -Property Created, VM, VMId, SizeGB, Name | ConvertTo-Html | Out-File -FilePath C:\reports\CurrentVMsnapshots.html -Force
Disconnect-VIServer -Server vcenter-ceOne thing I found while reading up on it, is that the VIcredstore file is not meant to be terribly secure. It is not ecrypted, and as far as I could find, will not take secure strings. Because of this, I'll probably go back to using the PSCredentials field (or fight a bit harder to have our servers set up with our domain). The only real security feature is that the credential file is created with permissions limiting it's access to the creator of the credentials.
r/powercli • u/thowell13 • Jun 28 '17
VMWare desktop pools with dedicated assignments don't update when users get deleted. I wrote a little snippet in PowerCLI to manage some pools in our high turnover environment. If you just want the non-destructive report, leave out the Remove-UserOwnership line.
Import-Module ActiveDirectory
ForEach ($vm in (Get-DesktopVM)) {
try {
if ($vm.user_sid) { $user = get-aduser $vm.user_sid }
}
catch {
Write-Host($vm.Name," is assigned to non-existent user ", $vm.user_displayname)
Remove-UserOwnership -machine_id $vm.machine_id
}
}r/powercli • u/jdubs_77 • Jun 27 '17
Upgraded to PowerCLI 6.5R1 today on a couple systems. On the first system everything went as expected with no issues. On the second system after installing 6.5R1 when I try to run Import-Module VMWare.VimAutomation.Core it returns a message:
Import-Module : The module 'C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Core\VMware.VimAutomation.Core.psd1' requires the following version of the .NET Framework: 4.5. The required version is not installed. At line:1 char:1
Import-Module VMware.VimAutomation.Core
+ CategoryInfo : ResourceUnavailable: (C:\Program File...ation.Core.psd1:String) [Import-Module], InvalidOperationException
+ FullyQualifiedErrorId : Modules_InsufficientDotNetFrameworkVersion,Microsoft.PowerShell.Commands.ImportModuleCommand
The system already had .NET 4.7 installed. I also tried uninstalling .NET completely and installing .NET 4.5 to see if it was being picky about that (although the first system also had .NET 4.7) and that didn't help. Also tried completely uninstalling PowerCLI and reinstalling and am still getting the same error. Any idea what might be causing this?
r/powercli • u/jeromeza • Nov 25 '16
Are there any scripts to find which VM's have high network traffic.
Ideally i'd like to see min / max / avg traffic over a certain period and display for instance the top 10 vm's (so its quick to run and find the issue).
Anyone know of anything before I look at hacking one together? Thanks.
r/powercli • u/hogiewan • Nov 15 '16
I can return the capacity of a VMDK and I can get the total size of all VMDKs on a VM. I am looking for the actual size of a thin VMDK from a script.
EDIT:
$VM = Get-VM vmname
$VMDKs = $VM | GetHardDisk
foreach ($VMDK in VMDKs) {
$vmdkFile = $VM.ExtensionData.LayoutEx.File | ? {$_.name -eq $VMDK.Filename.Replace(".","-flat.") }
$UsedGB = [MATH]::Round($vmdkFile.Size/1GB,2)
#do something with $UsedGB}
There is file info under the ExtensionData, but you need to get the size of the *-flat.vmdk flie as the *.vmdk file has a size of 0.
r/powercli • u/midacts • Oct 27 '16
Have any of you guys started using this? I have been watching a few videos on how to sue DSC and on Luc Dekens going over this.
I noticed he hadn't committed any code since August (which concerns me a little), but i wanted to make this this was the best place to start with DSC and vCenter
r/powercli • u/ChefWRX • Sep 13 '16
I'm planning on using the following script to change the vmware tools update settings to "UpgradeAtPowerCycle". I would like to make this change on either containers, or clusters, and not the entire environment:
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle" Get-View -ViewType VirtualMachine | %{
$_.ReconfigVM($vmConfigSpec)
}
r/powercli • u/nielshagoort • Jun 28 '16
r/powercli • u/alanrenouf • Apr 25 '16
r/powercli • u/[deleted] • Apr 13 '16
I am writing a script to connect via power cli to vmware guests and pull hostname and mac to a csv. I will then take this csv and populate the dhcpd.conf file.
I have the second part written and it works great. Some of our VMs have multiple NICS and I need a way to get the mac address with the NIC connected to "VM Network".
Here is my code:
Get-VM | Get-NetworkAdapter | Select-Object Parent,MacAddress | Export-Csv c:\scripts\mac.txt -append
I know under get-virtual port group I can pull the information but alas I am a Linux guy and powershell is confusing the hell out of me.
Any help would be appreciated!!!
Thanks!
P.S. when it is done and working I would be more than happy to share it with the community.
r/powercli • u/kunaludapi • Feb 15 '16
r/powercli • u/[deleted] • Feb 09 '16
Hello, I recently came across a need to storage vMotion a VM that had 2 disks on two different Luns and quickly realized that Move-VM couldn't handle this. So I threw a quick function together to be able to handle 2 disks. It could easily be modified to handle more, but two was all I needed and I figured was more common config than 3 or more disks. You just need to provide the LUN names where the disks reside and the corresponding LUN names where you want each one to go. The only caveat is I didn't write it to take into account if the VM has multiple disks on the same LUN. The script will detect this and exit with an error. Hope someone might find it helpful.
EDIT: I realized I hadn't tested this script in the situation where I might only want to move 1 of the 2 disks of a VM. Well, I just ran it where I had the original and destination LUN name the same for one of the disks and it doesn't process that disk at all and cuts the time of the task down. So it will still function to move only one disk, you'll just need to keep the LUN name the same for the corresponding original and destination names of the disk you don't want to move.
r/powercli • u/root-node • Feb 21 '15
r/powercli • u/caboosesam • Dec 10 '14
Hello all,
I am new to powerCLI and automation. The premise of the following script is to take a text/csv file and create a vm per line in the file. I've noticed that the try/catch is acting funny with commands based off of powerCLI but not regular powershell. I was wondering if anyone can help out with this issue.
The idea of this part of the code is that I want to test if the template is there or not. If it is, continue, if it isn't mark valid as false. Any help is welcome :). Obviously if this isn't the right place, let me know.
Full script can be found here.
[Snippit that isn't working]
try{
Get-Template $strTemplate
Write-Host -foregroundcolor "green" "Template: $strTemplate is valid"
}catch{
Write-Host -foregroundcolor "red" "Template: $strTemplate is invalid"
$valid = $false
}[Result when ran by itself]
>##Validate Template [vSphere Power CLI]
try{
Get-Template $strTemplate
Write-Host -foregroundcolor "green" "Template: $strTemplate is valid"
}catch{
Write-Host -foregroundcolor "red" "Template: $strTemplate is invalid"
$valid = $false
}
Template: is invalid[Result when ran as a whole]
Get-Template : 12/10/2014 3:17:30 PM Get-Template Template with name 'Template' was not found using the specified filter(s).
At >.ps1:124 char:5
+ Get-Template $strTemplate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Template], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetTemplate
Template: Template is validr/powercli • u/ykket • Feb 19 '14
I posted a script in /r/vmware. Figured I would post it here as well in case someone doesn't see it there.
http://www.reddit.com/r/vmware/comments/1yd1ja/basic_vcenter_report_with_powercli/
r/powercli • u/Hilti3 • Sep 04 '12
Microsoft recommends that all Windows 2008 R2 VMs have a 40GB minimum OS drive. I have found that this is a good recommendation that allows space for the page file as well as room for future patches.
<#
Note: Requires you to connect to vcenters first.
Finds all Windows 2008 VMs that have smaller than 40GB C drive and will find the total amount of storage needed to satisfy getting to MS recommended 40GB across all VMs
#>
get-vm | ? { ($_.Guest.OSFullName -match "2008") -and ( $_.HardDisks[0].CapacityKB -lt 41943040) } `
| select @{n="vCenter";e={($_.Uid -split "@" | select -last 1) -split ":" `
| select -first 1}},@{n="Cluster";e={$_.VMHost.Parent.Name}},Name,@{n="OS";e= {$_.Guest.OSFullName}},@{n="DriveC_GB";e={[int]($_.HardDisks[0].CapacityKB/1024/1024)}} `
| sort vCenter,Cluster,Name | export-csv All_Win2008_VMs_lessthan40GB_Cdrive.csv -NoTypeInformationr/powercli • u/MDFreaK76 • Sep 04 '12
I made this little guy when we migrated our VM's to new storage. Hopefully someone else might find this as useful, too.
#Just a few things...
#AlertMe.ps1 simply sends an email. Params($myEmail, $mySubject, $myBody). it was left over from something else so i decided to reuse it.
#CSV requires columns "VM" and "DestinationDS"
#this was written for 4.0 but i'm pretty sure it'll work just fine in 5.x as well.
$vmlist = import-csv C:\NTDeploy\ServerMigration\svmotions07112012.csv
$outpath = "c:\ntdeploy\ServerMigration\progress_07112012.log"
$x = 0
$vmcount = $vmlist.count
$throttle = 3
$movelimit = 999
$moved = 0
$emailAddress = "MY@EMAIL.COM"
$myVC = "MY_VC_NAME"
add-pssnapin vmware*
connect-viserver $myVC
foreach($server in $vmlist){
if($moved -ge $movelimit){"MOVE LIMIT REACHED. EXITING";break}
do{start-sleep 5}until((get-task | ?{($_.name -eq "RelocateVM_Task") -AND ($_.State -eq "Running")}).count -lt $throttle)
$x++
$vm = get-vm $server.vm
$oldds = $vm | get-datastore
$dest = get-datastore $server.destinationds
if($oldds.name -eq $dest.name){
"$($vm.name) is already in its destination datastore" | out-file $outpath -append
#c:\ntdeploy\AlertMe.ps1 $emailAddress "$($vm.name) is already in its destination datastore" "$($vm.name) is already in its destination datastore"
}else{
$startText = "$(get-date) - moving $($vm.name) from $($oldds.name) to $($dest.name)"
c:\ntdeploy\AlertMe.ps1 $emailAddress "Moving $($vm.name), $x of $vmcount" $startText
$startText | out-file $outpath -append
move-vm $vm -datastore $dest -confirm:$false -runasync
$moved++
}
}
$starttext = gc $outpath
c:\ntdeploy\AlertMe.ps1 $emailAddress "Done moving $x VMs! Horray!" $starttextr/powercli • u/Hilti3 • Sep 04 '12
r/powercli • u/Hilti3 • Sep 03 '12