My script as below
1. Getting list of vms as input file and it is running on two center server.
2. Connecting to vcenter server one by one to get-vm for each vm
3. Collect Snapshot status for that vm
4.disconnect vcenter and report out as below
Vmname,vcentername,snapshot,createdtime.
Then second phase as continuity after giving input to proceed yes.
Connecting to all the vcenter servers.
Get-vm for the vm which has snapshot (from the previous report).
But I'm not getting output here.
When I check overall get-vm also not showing any result.
Why it's not working?
I would rather suspect a difference in configuration between both stations.
Did you check the DefaultVIServerMode on the machine where it is not working?
Did you check the $global:defaultviservers content on that machine?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you show us the script you are using?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Import-Module VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential "aetv\mpmohan_a"
$VC1 = "IP1"
$VC2 = "IP2"
$TimeStamp = (Get-Date -Format o).split(".")[0] | foreach {$_ -replace ":", "."}
$logPath = "$env:USERPROFILE\documents\" + $TimeStamp + "Snapshot_VMs.log"
try{
if(!(Test-Path $logPath)){
New-Item -Path $logPath -ItemType File -Force | Out-Null
}
elseif((Get-Item $logPath).Length -ge 5MB){
$date = Get-Date -Format MM-dd-yyyy-HH.mm.ss
$date = $date.ToString()
Rename-Item $logPath -NewName ("Snapshot_VMs" + $date +".log")
New-Item -Path $logPath -ItemType File | Out-Null
}
}
catch{
Write-Output "Failed to access/create log file. Script will now terminate"
$result = "Failed"
$message = "Failed to access/create log file."
return $result +": " + $message
break;
}
Function Write-Log(){
Param(
[Parameter(Mandatory=$true)]
[string]$Log
)
$date = Get-Date -Format "MM-dd-yyyy HH:mm:ss"
Add-Content -Path $logPath -Value ($date + " " + $Log)
}
if(Test-Path $env:USERPROFILE\documents\VMs_snapLists.txt){Remove-Item $env:USERPROFILE\documents\VMs_snapLists.txt}
New-Item -ItemType File -Path $env:USERPROFILE\documents\ -Name VMs_snapLists.txt | Out-Null
& $env:USERPROFILE\documents\VMs_snapLists.txt
Write-Host "`n`nEnter List Of server Names and save the file. Hit Enter To Continue..." -ForegroundColor:yellow
Read-Host
$vms = Get-Content "$env:USERPROFILE\documents\VMs_snapLists.txt"
Write-Log "Snapshot VMs List as below"
Write-Log "$vms"
$Report = @()
foreach ($VMname in $vms){
Connect-VIServer $VC1 -Credential $cred |Out-Null
$VM = Get-VM $vmname
if($VM){$vcenter = "vcenetername"
$PowerState= $VM.PowerState
$snapshot = $VM |Get-Snapshot
if($snapshot){
$snap = "Yes"
$snapsize = $("{0:N2}" -f ($snapshot.SizeGB))
$Created = $snapshot.Created.ToString("yyyy-MM-dd HH:mm:ss")
$how_old = (Get-Date) - $snapshot.Created
$AgeInDays = $("{0:N2}" -f $how_old.TotalDays)
}
else{
$snap = "No"
$snapsize = "No"
$Created = "No"
$how_old = "No"
$AgeInDays ="No"
}
}
else{
Connect-VIServer $VC2 -Credential $cred |Out-Null
$VM = Get-VM $VMname
if(!($vm)){
$vcenter = "VM NotFound"
$PowerState= "VM Not Found"
$snap = "VM NotFound"
$snapsize = "VM NotFound"
$Created = "VM NotFound"
$how_old = "VM NotFound"
$AgeInDays ="VM NotFound"
}
else{
$vcenter = "vcenter2"
$PowerState= $VM.PowerState
$snapshot = $VM | Get-Snapshot
if($snapshot){
$snap = "Yes"
$snapsize = $("{0:N2}" -f ($snapshot.SizeGB))
$Created = $snapshot.Created.ToString("yyyy-MM-dd HH:mm:ss")
$how_old = (Get-Date) - $snapshot.Created
$AgeInDays = $("{0:N2}" -f $how_old.TotalDays)
}
else{
$snap = "No"
$snapsize = "No"
$Created = "No"
$how_old = "No"
$AgeInDays ="No"
}
}
}
$details = [ordered]@{
vmname = $vmname
PowerState = $PowerState
snap = $snap
snapsize_GB = $snapsize
AgeInDays = $AgeInDays
Created = $Created
vcenter = $vcenter
}
$Report += New-Object PSObject -Property $details
Disconnect-VIServer * -Force -Confirm:$false
}
$TimeStamp = (Get-Date -Format o).split(".")[0] | foreach {$_ -replace ":", "."}
$filename = ( "D:\VMware\" + $TimeStamp + "snap_inputlist.csv")
$Report|Sort-Object vcenter, PowerState, snap | Export-Csv -NoTypeInformation $filename
$Report|Sort-Object vcenter, PowerState, snap |FT -AutoSize
$PON_VMs = $Report |?{($_.PowerState -eq "PoweredOn")}
$Poff_VMs = $Report |?{($_.PowerState -eq "PoweredOff")}
$PON_VMs_snap_yes = $PON_VMs |?{($_.snap -eq "yes")}
$PON_VMs_snap_No = $PON_VMs |?{($_.snap -eq "No")}
Write-Host "Do you want to proceed further? Enter 'Y' for Yes and 'N' for No" -ForegroundColor DarkYellow
$proceed = read-host
#$TimeStamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
#$filename = ( "D:\VMware\" + $TimeStamp + "_SnapshotReport.csv")
#$Report | Export-Csv -NoTypeInformation $filename
if($proceed -eq "Y"){
Connect-VIServer $VC1 -Credential $cred
Connect-VIServer $VC2 -Credential $cred
Write-Log "******************************************************"
if($PON_VMs_snap_No){
$PON_VMs_snap_No_Vmname = $PON_VMs_snap_No.Vmname
Write-Log "Creating New Snpashots for these VMs $PON_VMs_snap_No_Vmname "
Write-host "Creating New Snpashots for these VMs" `n $PON_VMs_snap_No_Vmname
foreach($VM_snap_no in $PON_VMs_snap_No){
$newname = ""
$VMnewsnap = get-vm $VM_snap_no.Vmname |out-null
$VMnewsnap_name = $VMnewsnap.name
Write-Log "There is no snapshot on $VMnewsnap_name"
Write-host "There is no snapshot on $VMnewsnap_name"
Write-Log "Creating Newsnapshot for $VMnewsnap_name"
Write-host "Creating Newsnapshot for $VMnewsnap_name"
$newname = "BfrPatch_Sshot-$(Get-Date -Format 'yyyyMMdd-hhmm')"
try{
New-Snapshot -VM $VMnewsnap -Name $newname -Quiesce -RunAsync |out-null
Write-Log "New snapshot has been created for $VMnewsnap_name as $newname"
Write-host "New snapshot has been created for $VMnewsnap_name as $newname"
}
catch{
Write-Log "Unable to create snapshot for $VMnewsnap_name, hence kindly check manually"
Write-host "Unable to create snapshot for $VMnewsnap_name, hence kindly check manually"}
}
}
else{Write-Log "There is no VMs without snapshots"
Write-host "There is no VMs without snapshots"}
$PON_VMs_snap_yesvms = $PON_VMs_snap_yes.vmname
Write-Log "******************************************************"
Write-Log "Deleting and Creating New Snpashots for these VMs $PON_VMs_snap_yesvms "
Write-host "Deleting and Creating New Snpashots for these VMs" `n $PON_VMs_snap_yes.Vmname
if($PON_VMs_snap_yes){
foreach($VM_snap_yes in $PON_VMs_snap_yes){
$newname =$snapshot= ""
$VMDel_newsnap = Get-VM $VM_snap_yes.vmname |out-null
$VMDel_newsnap_name = $VMDel_newsnap.name
if($VMDel_newsnap){
try{
Get-Snapshot -VM $VMDel_newsnap | Remove-Snapshot -Confirm:$false |out-null
Write-Log "Snapshot has been deleted successfully on $VMDel_newsnap_name"
Write-host "Snapshot has been deleted successfully on $VMDel_newsnap_name"
}
catch{
Write-Log "Unable to delete snapshot on $VMDel_newsnap_name, hence please check manually"
Write-host "Unable to delete snapshot on $VMDel_newsnap_name, hence please check manually"
}
Write-Log "Creating Newsnapshot for $VMDel_newsnap_name"
Write-host "Creating Newsnapshot for $VMDel_newsnap_name"
$newname = "BfrPatch_Sshot-$(Get-Date -Format 'yyyyMMdd-hhmm')"
try{
New-Snapshot -VM $VMDel_newsnap -Name $newname -Quiesce -RunAsync |out-null
Write-Log "New snapshot has been created for $VMDel_newsnap_name as $newname"
Write-host "New snapshot has been created for $VMDel_newsnap_name as $newname"
}
catch{
Write-Log "Unable to create snapshot for $VMDel_newsnap_name, hence kindly check manually"
Write-host "Unable to create snapshot for $VMDel_newsnap_name, hence kindly check manually"
}
}
else{
Write-Log "VMs are not found, hence please check manually"
Write-host "$VM_snap_yes.Vmname is not found, hence please check manually"
}
}
}
else{Write-Log "There is no VMs with snapshots"
Write-host "There is no VMs with snapshots"}
}
else{
Write-Log "Input is NO to proceed, hence exit the script"
Write-host "Input is NO to proceed, hence exit the script"
}
I suspect the problem comes from this line
$VMnewsnap = get-vm $VM_snap_no.Vmname |out-null
You are doing a Get-VM, but you send the result to Out-Null.
As a result the $VMnewsnap variable will be empty.
And you do the same later on with
$VMDel_newsnap = Get-VM $VM_snap_yes.vmname |out-null
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
but if i try to use GET-VM VMName, then also i am not getting all the VMs.
iam getting half of the VMs list from that vcenter.
In the script?
At which location?
Where do you get VMName from in that case?
Are you connected to a vCenter? Check with $global:defaultviservers
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Are you running in Multiple mode?
What does Get-PowerCLIConfiguration show?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
before get-vm command i am connecting all the vcenter servers.
actually at the top, i am using same commands to get the VMs list from the input and i am getting output.
only at second phase i am facing this issue.
what else i can do? to achive my request.
request is as below\
1. we will give list of VMs
2. report with VM's snapshot list
3. after then proceed to further as second phase.
4. If VM has snapshot then it should delete and create
5. If there is no snapshot then it should create new snapshot
yes
Scope ProxyPolicy DefaultVIServerMode InvalidCertificateAction DisplayD
eprecati
onWarnin
gs
----- ----------- ------------------- ------------------------ --------
Session UseSystemProxy Multiple Unset True
User Multiple
AllUsers
Did you check the DefaultVIserverMode with Get-PowerCLIConfiguration?
If that says Single, the Get-VM will only run against the last connected vCenter (which could explain why you only see half of the expected results).
You can change the setting with Set-PowerCLIConfiguration.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you check if you are actually connected to both vCenters?
Check what is in $global:defaultviservers, just before you do the Get-VM.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
seems like after removing OUT-NULL, i am able to do from one of terminal server.
but same is not working from another server, how can i fix that.
seem like it is bug in that machine?
I would rather suspect a difference in configuration between both stations.
Did you check the DefaultVIServerMode on the machine where it is not working?
Did you check the $global:defaultviservers content on that machine?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i did re-install of powercli using install-module and it is working fine with the same script.