PowerShell: Wait for SQL Server is ready for client connections

The useful PowerShell script waiting SQL server is ready.
I found that waiting for starting service

while((get-service MSSQL`$INSTANCENAME).Status -ne 'Running'){Sleep 3}

is not enough.
Better to check the event log entry EventID = 17126: SQL Server is now ready for client connections. This is an informational message; no user action is required.

$ew = new-object system.management.ManagementEventWatcher                                                         
$ew.query = "Select * From __InstanceCreationEvent Where TargetInstance ISA 'Win32_NTLogEvent'"                   
while(!(get-eventlog -logname 'Application' -Source 'MSSQL$INSTANCENAME' | ? {$_.EventId -eq 17126})){