Friday, January 5, 2018

Get Last Backup Field in vCenter with Powershell.

#This is a little Powershell script that gets the last time a vm in the Cluster that it has been backed up.  Emails out if it has not been backed up in 14days.

$daystoalert = 14
$vCenter = "vCenter server name"

add-pssnapin vmware.vimautomation.core
connect-viserver $vCenter
$today_date = get-date

$all_vms = get-cluster "Cluster Name" | get-vm | Sort-Object Name

foreach($vm in $all_vms)
{
    #check $vm for null?
    Try {
        $last_backup_date = $vm. CustomFields.Get_Item( "Last Backup")
        $last_backup_converted = get-date $last_backup_date
        $days_with_no_backups = New-TimeSpan -Start $last_backup_converted .DateTime -End $today_date. DateTime
        if ($days_with_no_backups .Days -gt $daystoalert)
        {
            Write-output " $($vm. Name),$( $days_with_no_backups.Days) "
        }
    }
    Catch {
        # VM was never backed up gets caught in this exception.
        Write-output " $($vm. Name), Never backed up"
    }

Delete files in a folder and subfolders of files based on last access time

Get-ChildItem -path "D:\ftp.archived.logs" -Recurse | Where-Object{$_.LastAccessTime -lt (get-date).AddDays(-90)} | remove-item 

add -whatif at the end of remove-item to show what will get removed.



Enable/Disable SMBv1,2,3 in Windows


Disable form repost on browser refresh

Here's a trick I found to prevent a form repost on browser refresh


        protected void Page_PreRender(object sender, EventArgs e)
        {
                     ViewState["Check_Page_Refresh"] = Session["Check_Page_Refresh"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["Check_Page_Refresh"] = DateTime.Now.ToString();
            }
        }

protected void btn_Click(object sender, EventArgs e)
{
if (ViewState["Check_Page_Refresh"].ToString() == Session["Check_Page_Refresh"].ToString())
{
     <your main code you want to execute once on button click here>
}
}

Reset vmware ESX evaluation license.

VMWare ESXi license expires in 60 days.  You can reset that 60 days by doing the following.

-ssh into the ESXi host.
-rm -f /etc/vmware/vmware.lic /etc/vmware/license.cfg
-reboot

SQL does not allow modifying of table.

If you've dealt with MS SQL before, by default, it does not allow you to modify a table without you dropping it and recreating it. It's kind of annoying.   Here's a way to get around it.  Do this at your own risk of corrupting your database.

-Go to the Tools menu -> Options
-Click on "Designers" on the left menu tree
-Uncheck "Prevent saving changes that require table re-creation"



VMWare - Power ON a vm from the command line

To power on a virtual machine from the command line:

-List the inventory ID of the virtual machine with the command:
               vim-cmd vmsvc/getallvms |grep <vm name>
               Note: The first column of the output shows the vmid.

-Check the power state of the virtual machine with the command:
               vim-cmd vmsvc/power.getstate <vmid>

-Power-on the virtual machine with the command:
               vim-cmd vmsvc/power.on <vmid>

VSS Error while running system state/bare metal restore backup using SCDPM.

When running a system state/bare metal restore backup using SCDPM, you may encounter a VSS error leading to a protection point inconsistency.  This is due to automounter being disabled and the system restore partition being offline in disk management.  The cause of this may be a third party snapshot tool that leverages VSS such as Netapp's SnapDrive.  In order to get SCDPM to do a system state/bare metal restore, you will need to do the following steps.  Note: this works for VMWare converter as well if you're getting

System Partition is offline.  No drive letters

1.  log into the server you want to protect.
2.  open a command prompt
3.  type "diskpart"
4.  type "automount" to display the current state of automount.  If its disabled, open another command prompt and run "diskpart> automount enable" to enable it.
5.  in diskpart, get a list of volumes by typing "list volume".
6.  find the system restore partition and note down the volume number.
7.  type "select volume (x)" where x is the volume number of the system restore partition.
8.  type "online volume" to bring the volume online.
9.  rerun the SCDPM consistency check and it should be successful.

Before

After (Notice "System Reserved" volume is labeled)

ESXTOP quick troubleshooting overview


Power off a Virtual SAN Cluster

Power off a Virtual SAN Cluster

Prerequisites
If the vCenter Server VM is running on the Virtual SAN cluster, migrate the VM to the first host, or record the host where it is currently running.
Procedure

1
Power off all virtual machines that are running on the Virtual SAN cluster.
The vCenter Server virtual machine must be powered off last.
2
Place all ESXi hosts that compose the cluster in maintenance mode.
Run the esxcli command to set the Virtual SAN mode for entering the maintenance state.
esxcli system maintenanceMode set -e true -m noAction
3
Power off the ESXi hosts.