简体   繁体   中英

powershell vmware powerCLI automatic script

I am making a script to run in powershell (powerCLI) for vmware. I try to do a automatic report exported to a csv file but i dont know how to resolve couple of problems.

All parameters which i dont know how to export them.

  1. "virtual machine working location" I can export disks where is a mashine, but i don't know how to export all path with folders.

  2. domain / workgroup

  3. Name of computer When i try to export a name i get name with domain "name.domainname.com" (that is strange because my VM are not in domain, there are in workgroup) name i mean a name inside of OS not in esxi, because esxi name of vm i get from this

$name = (get-vm name_maschine|select-object name).name

or simple when in loop parameter is a name of mashine, i only export a parameter

less important parameters

4 . The name of vcenter in which host is working

  1. The name of datacenter in which host is working

Code:

connect-viserver -server IP-ADDRES -user root -password PASSWORD 
Get-View -ViewType VirtualMachine | %{ 
    New-Object PSObject -Property @{ 
    # mashine name 
    'Mashine name' = $_.Name 
    #date when edited 
    'Date' = Get-Date 
    # resource pull 
    'pull' = (Get-VM $_.Name | Get-ResourcePool | select-object name).name 
    #disk where is mashine 
    'Datastore' = [system.String]::Join(",",($_.Storage.PerDatastoreUsage | %{Get-View $_.Datastore} | %{$_.Name}))
    }
}

I added extra parameters except domain/workgroup. To obtain that you will need to execute Invoke-VMScript per each VM (with local admin credentials), combined with something like (Get-WmiObject Win32_ComputerSystem).Domain

$guestUser = "administrator"
$guestPass = "yourpass"
Get-View -ViewType VirtualMachine | %{ 
    New-Object PSObject -Property @{ 
    # machine name 
    'Machine name' = $_.Name
    # machine name from vmware tools
    'Guest name' = $_.Guest.HostName
    # machine name from WMI
    'Guest name(WMI)' = (Invoke-VMScript -VM $_.Name -GuestUser $guestUser -GuestPassword $guestPass -ScriptText {(Get-WmiObject Win32_ComputerSystem).Domain}).ScriptOutput
    #date when edited 
    'Date' = Get-Date 
    # resource pool 
    'pool' = (Get-VM $_.Name | Get-ResourcePool | select-object name).name 
    #disk where is mashine 
    'Datastore' = [system.String]::Join(",",($_.Storage.PerDatastoreUsage | %{Get-View $_.Datastore} | %{$_.Name}))
    # physical location
    'VM Location' = $_.Config.DataStoreURL.URL
    # vm host
    'VM Host' = (Get-VM $_.Name).VMHost
    # datacenter
    'Datacenter' = (Get-Datacenter -VM $_.Name).Name
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM