http://myvirtualcloud.net/?p=1838&cpage=1#comment-150353
only issue i had was making this work on the windows 7 using psexec so i used invoke command powershell below are the steps to make the script work on vmware view
Needed to set on vcenter for powershell memory & Shells allowed for on vcenter server
set-item wsman:localhost\Shell\MaxMemoryPerShellMB 1024
winrm set winrm/config/winrs ‘@{MaxShellsPerUser=”50″}’
postsync script
# ——————————– Begin Script ————————————
# Load the VIM API
Write-Host $args[0]
Write-Host $args[1]
Add-PSSnapin VMware.VimAutomation.Core
# Connect to the VirtualCenter
connect-viserver $vcenterservername -User $vcenteruserid -Password $vcenterpassword
# RunFromGuest.ps1 passes the VMname and the VLAN information, then sets the VLAN accordingly
get-vm $args[0] | get-networkadapter | set-networkadapter -networkname $args[1] -confirm:$false
# Because we just disconnected the NIC and changed the VLAN, reboot the virtual machine
Write-Host $args[0]
restart-vmguest -vm $args[0] -confirm:$false
exit
# ——————————– End Script ————————————
# ——————————– Begin Script ————————————
start-sleep -seconds 15
# First get the name of the virtual machine and store it in the variable $vmName
$vmName = get-content env:computername
# Next, connect to the data source (in this case a simple CSV)
$obj = import-csv \\$vcenterserver\postsync\data.csv
# Finally, search the CSV for the virtual machine name and find the associated VLAN ID
foreach ($item in $obj)
{
if ($vmName -eq $item.vm)
{
# If we find a match, execute postsync.ps1 from the VirtualCenter server
$VLAN=$item.vlan
$pass = convertto-securestring “$vcenterpassword” -asplaintext -force
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist “$$vcenteruserid”,$pass
Invoke-Command -computername $vcenterservername -Credential $mycred -scriptBlock {param($vmName,$VLAN) C:\postsync\postsync.ps1 $vmName $VLAN } -Argumentlist $vmName,$VLAN
}
}
# ——————————– End Script ————————————
No comments:
Post a Comment