MUST READ - WVD Personal VM - Proper Shutdown process and deallocate VM to save costs!

%3CLINGO-SUB%20id%3D%22lingo-sub-2377241%22%20slang%3D%22en-US%22%3EMUST%20READ%20-%20WVD%20Personal%20VM%20-%20Proper%20Shutdown%20process%20and%20deallocate%20VM%20to%20save%20costs!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2377241%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EFirst%20things%20first!%3C%2FSTRONG%3E%3CBR%20%2F%3ERecently%20Microsoft%20introduced%20a%20really%20cool%20feature%20which%20is%20called%20%E2%80%9C%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-desktop%2Fstart-virtual-machine-connect%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EStart%20VM%20on%20Connect%E2%80%9D%3C%2FA%3E%20and%20was%20a%20long%20awaiting%20feature.%20This%20feature%20is%20groundbreaking%20because%20the%20WVD%20management%20plane%20is%20able%20to%20start%20the%20VM%20on%20behalf%20of%20the%20user.%20End-users%20no%20longer%20have%20to%20call%20their%20IT%20admins%20to%20start%20the%20VM.%20Once%20the%20end-user%20hits%20the%20button%20to%20start%20their%20desktop%2C%20the%20VM%20will%20be%20started%20automatically%20and%20will%20take%20approximately%205%20minutes.%20This%20mystery%20has%20been%20resolved%2C%20now%20the%20next%20piece%20of%20the%20puzzle%20to%20shutdown%20and%20deallocate%20a%20WVD%20personal%20VM%20and%20save%20consumption%20costs.%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3EChallenge!%3C%2FSTRONG%3E%3CBR%20%2F%3EMost%20of%20the%20end-users%20will%20shutdown%20the%20VM%20but%20the%20result%20is%20a%20VM%20in%20%E2%80%9Cstopped%E2%80%9D%20state.%20This%20means%20the%20VM%20is%20still%20allocated%20to%20a%20host%20server%20in%20the%20Azure%20datacenter%20and%20the%20subscription%20owner%20will%20still%20be%20charged.%20Other%20possible%20scenario%E2%80%99s%20might%20be%20that%20end-users%20disconnect%20from%20the%20session%20instead%20of%20shutdown%20the%20VM%20or%20they%20totally%20forgot%20their%20session%20which%20becomes%20inactive%2Fidle.%3C%2FP%3E%3CP%3E%3CSTRONG%3EAn%20easy%2C%20almost%20effortless%20two-step%20solution%3C%2FSTRONG%3E%20%3Asmiling_face_with_smiling_eyes%3A%3CBR%20%2F%3EIn%20order%20to%20resolve%20our%20challenge%20it%E2%80%99s%20good%20to%20understand%20the%20difference%20between%20%E2%80%9Cidle%E2%80%9D%20and%20%E2%80%9Cdisconnected%E2%80%9D%20terminology%20and%20it%E2%80%99s%20highly%20recommended%20to%20determine%20idle%20session%20time%20and%20disconnected%20session%20time%20within%20your%20organization%20or%20with%20your%20customer.%20Idle%20time%20means%20that%20the%20session%20is%20inactive%2C%20the%20end-user%20is%20not%20doing%20anything%20in%20the%20desktop%20session.%20Disconnected%20time%20means%20that%20the%20user%20closes%20the%20desktop%20session%20without%20properly%20signing%20out%20or%20shutting%20down%20the%20VM.%20My%20advise%20here%2C%20during%20onboarding%20provide%20proper%20instructions%20for%20your%20end-users%20on%20how%20to%20end%20a%20desktop%20session.%3CBR%20%2F%3E%3CBR%20%2F%3EImportant%20note%2FPrerequisite!%20This%20will%20only%20work%20when%20Logon%2FLogoff%20events%20are%20audited%20(success%2Ffailure).%20Almost%20all%20enterprises%20have%20this%20enabled%20in%20a%20security%20or%20default%20domain%20policy%20but%20please%20double%20check!%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CEM%3EConfigure%20Azure%20Automation%20Account%20%E2%80%93%20Finding%20VM%E2%80%99s%20with%20%E2%80%9Cstopped%E2%80%9D%20state%20and%20execute%20Stop-AzVm%20to%20bring%20VM%20in%20%E2%80%9Cstopped%20de-allocated%E2%80%9D%20state.%3CBR%20%2F%3E%3C%2FEM%3E%3C%2FSTRONG%3EAn%20Azure%20Automation%20Account%20is%20very%20powerful%20and%20can%20basically%20do%20anything%20within%20your%20Azure%20environment.%20For%20this%20purpose%20the%20Automation%20Account%20requires%20to%20be%20created%20along%20with%20a%20%E2%80%9CRun%20as%20Account%E2%80%9D.%20This%20account%20will%20act%20as%20a%20trusted%20service%20principal%20and%20is%20allowed%20to%20execute%20runbooks%20(regardless%20what%20a%20runbook%20contains).%3CBR%20%2F%3E%3CBR%20%2F%3E1.)%20Open%20up%20the%20Azure%20portal%20with%20your%20admin%20privileges%2C%20in%20the%20search%20bar%20find%20Automation%20Accounts%20and%20hit%20the%20icon.%3CBR%20%2F%3E2.)%20Press%20the%20create%20button%2C%20complete%20the%20fields%20(provide%20a%20clear%20name%2C%20associate%20the%20corresponding%20resource%20group%2C%20set%20location%2C%20and%20toggle%20the%20switch%20on%20YES%20to%20Create%20Azure%20Run%20As%20Account!%20Press%20the%20Create%20button.%20The%20creation%20process%20will%20take%20a%20few%20minutes.%20Time%20to%20grab%20a%20drink%20or%20whatever%20you%20might%20want%20to%20do%20%3Asmiling_face_with_smiling_eyes%3A%3CBR%20%2F%3E3.)%20Once%20completed%2C%20open%20the%20Automation%20Accounts%2C%20scroll%20a%20little%20bit%20down%2C%20click%20on%20Modules%2C%20click%20on%20Browse%20Gallery.%20Find%20the%20following%20modules%3CBR%20%2F%3E-%20Az.Accounts%20(select%20it%2C%20press%20Import%2C%20wait%20a%20few%20minutes)%3CBR%20%2F%3E-%20Az.Compute%20(select%20it%2C%20press%20Import%2C%20wait%20a%20few%20minutes)%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_0-1621637304389.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282769i19D5B0B4F3597270%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_0-1621637304389.png%22%20alt%3D%22raymondesray_0-1621637304389.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E4.)%20In%20the%20Modules%20overview%20hit%20%E2%80%9CRefresh%E2%80%9D%20to%20validate%20whether%20both%20modules%20are%20present%20and%20have%20%E2%80%9CAvailable%E2%80%9D%20status.%3CBR%20%2F%3E%3CBR%20%2F%3E5.)%20Within%20the%20%E2%80%9CProcess%20Automation%E2%80%9D%20section%20click%20on%20Runbooks%2C%20followed%20by%20pressing%20the%20%E2%80%9C%2B%20Create%20a%20runbook%E2%80%9D%20button.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_1-1621637304407.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282771i1394EE4E4A1053E0%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_1-1621637304407.png%22%20alt%3D%22raymondesray_1-1621637304407.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E6.)%20Fill%20in%20a%20logical%20name%2C%20set%20Runbook%20type%20%E2%80%9CPowerShell%E2%80%9D%2C%20followed%20by%20pressing%20the%20Create%20button%20below.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_2-1621637304413.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282770i7619F03E1D8D13A6%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_2-1621637304413.png%22%20alt%3D%22raymondesray_2-1621637304413.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E7.)%20Now%20you%20need%20to%20enter%20a%20script%2C%20completely%20copy%20following%20content%20starting%20at%20param.%3C%2FP%3E%3CTABLE%3E%3CTBODY%3E%3CTR%3E%3CTD%20width%3D%22601%22%3E%3CP%3E%3CSTRONG%3EContent%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%22601%22%3E%3CP%3E%3CSTRONG%3Eparam%20(%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5Bparameter(Mandatory%20%3D%20%24true)%5D%5BString%5D%24ResourceGroupName%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E)%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24ConnectionName%20%3D%20%22AzureRunAsConnection%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3Etry%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%7B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24Connection%20%3D%20Get-AutomationConnection%20-Name%20%24ConnectionName%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23%20Get%20the%20connection%20%22AzureRunAsConnection%20%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24ServicePrincipalConnection%20%3D%20Get-AutomationConnection%20-Name%20%24ConnectionName%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22Logging%20in%20to%20Azure...%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Connect-AzAccount%20%60%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B-ServicePrincipal%20%60%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-Tenant%20%24ServicePrincipalConnection.TenantId%20%60%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-ApplicationId%20%24ServicePrincipalConnection.ApplicationId%20%60%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-CertificateThumbprint%20%24ServicePrincipalConnection.CertificateThumbprint%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Set-AzContext%20-Subscription%20%24Connection.SubscriptionID%20-Tenant%20%24Connection.TenantId%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3Ecatch%20%7B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20(!%24ServicePrincipalConnection)%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24ErrorMessage%20%3D%20%22Connection%20%24ConnectionName%20not%20found.%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20throw%20%24ErrorMessage%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%20else%20%7B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Error%20-Message%20%24_.Exception%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20throw%20%24_.Exception%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23Get%20VMs%20Status%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5Barray%5D%24StoppedAzVMs%20%3D%20Get-AzVM%20-Status%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if(%24StoppedAzVMs)%20%7B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23Loop%20through%20each%20VM%20in%20this%20Resource%20Group%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ForEach(%24StoppedAzVM%20in%20%24StoppedAzVMs)%20%7B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%20%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%23If%20PowerState%20is%20VM%20stopped%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if(%24StoppedAzVM.PowerState%20-eq%20%22VM%20stopped%22)%20%7B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Stop-AzVM%20-Name%20%24StoppedAzVM.Name%20-ResourceGroupName%20%24StoppedAzVM.ResourceGroupName%20-Force%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7D%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%3CBR%20%2F%3E8.)%20Hit%20the%20%E2%80%9CPublish%E2%80%9D%20button%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_3-1621637304423.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282773iF0100286D9092178%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_3-1621637304423.png%22%20alt%3D%22raymondesray_3-1621637304423.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EConfirm%20by%20pressing%20Yes.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_4-1621637304426.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282772iEBB79646E505C8BC%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_4-1621637304426.png%22%20alt%3D%22raymondesray_4-1621637304426.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E9.)%20You%20will%20see%20the%20overview%20section%20of%20the%20Runbook%2C%20click%20on%20Schedules%2C%20click%20the%20button%20%E2%80%9C%2B%20Add%20a%20schedule%E2%80%9D.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_5-1621637304437.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282774i6809CCFAAA4F27DF%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_5-1621637304437.png%22%20alt%3D%22raymondesray_5-1621637304437.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E10.)%20Click%20on%20Link%20a%20schedule%20to%20your%20runbook.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_6-1621637304442.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282775iA83F6F75018CDAD8%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_6-1621637304442.png%22%20alt%3D%22raymondesray_6-1621637304442.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E11.)%20Click%20%2BAdd%20a%20schedule%2C%20complete%20the%20form%20and%20press%20Create%20below.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_7-1621637304447.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282776i5F80F57CAB4494E5%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_7-1621637304447.png%22%20alt%3D%22raymondesray_7-1621637304447.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E12.)%20Click%20on%20Configure%20Parameters%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_8-1621637304451.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282777i8B13EBFF09A7901B%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_8-1621637304451.png%22%20alt%3D%22raymondesray_8-1621637304451.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E13.)%20Fill%20in%20the%20designated%20resource%20group%20name%20and%20press%20OK%20twice.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_9-1621637304456.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282778i6399B15A9504BF30%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_9-1621637304456.png%22%20alt%3D%22raymondesray_9-1621637304456.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E14.)%20Go%20back%20to%20the%20overview%20of%20the%20runbook%20and%20press%20the%20Start%20button%20to%20validate.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_10-1621637304462.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282779i4DF7F46DF473A95F%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_10-1621637304462.png%22%20alt%3D%22raymondesray_10-1621637304462.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E15.)%20Since%20this%20is%20a%20manual%20start%20and%20validation%20action%2C%20enter%20the%20resource%20group%20name%20and%20press%20OK.%20Wait%20for%20the%20job%20to%20complete.%3CBR%20%2F%3E%3CBR%20%2F%3E16.)%20In%20the%20overview%20section%20you%20will%20see%20the%20completed%20Job%2C%20click%20on%20it%20%2C%20press%20the%20output%20tab%20and%20validate%20it%20ran%20successfully.%20It%20depends%20whether%20you%20already%20have%20stopped%20state%20VM%E2%80%99s.%3CBR%20%2F%3E%3CBR%20%2F%3ENext%20part%20is%20to%20configure%20session%20time%20limits%20in%20conjunction%20with%20a%20scheduled%20task%20to%20trigger%20a%20shutdown%20action.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CEM%3EConfigure%20group%20policy%20to%20end%20idle%20and%20disconnected%20sessions%2C%20which%20means%20the%20user%20will%20be%20logged%20off%3B%20%3C%2FEM%3E%3C%2FSTRONG%3E%3CBR%20%2F%3EStart%20your%20group%20policy%20management%20console%2C%20right%20mouse%20click%20on%20the%20OU%20that%20contains%20the%20WVD%20personal%20VM%20computer%20accounts%2C%20create%20a%20new%20GPO%20and%20ensure%20it%20is%20linked.%20Enable%20and%20configure%20the%20following%20settings%3B%3CBR%20%2F%3E%3CBR%20%2F%3EPart%201%20of%20GPO%3A%3CBR%20%2F%3EComputer%20Configuration%5CAdministrative%20Templates%5CWindows%20Components%5CRemote%20Desktop%20Services%5CRemote%20Desktop%20Session%20Host%5CSession%20Time%20Limits%3CBR%20%2F%3E1.)%20End%20session%20when%20time%20limits%20are%20reached%20%26gt%3B%20enable%3CBR%20%2F%3E2.)%20Set%20time%20limit%20for%20disconnected%20sessions%20%26gt%3B%20enable%20(for%20demo%20purpose%205%20minutes%20but%20this%20is%20what%20you%20need%20to%20discuss%20with%20your%20organization%2Fcustomer!)%3CBR%20%2F%3E3.)%20Set%20time%20limit%20for%20active%20but%20idle%20Remote%20Desktop%20Services%20sessions%20%26gt%3B%20%3CFONT%20color%3D%22%23000000%22%3Eenable%20(for%20demo%20purpose%205%20minutes%20but%20this%20is%20what%20you%20need%20to%20discuss%20with%20your%20organization%2Fcustomer!)%3C%2FFONT%3E%3CBR%20%2F%3E%3CBR%20%2F%3EPart%202%20of%20GPO%3A%3CBR%20%2F%3EComputer%20Configuration%5CPreferences%5CControl%20Panel%20Settings%5CScheduled%20Tasks%3CBR%20%2F%3E1.)%20Right%20mouse%20click%20on%20Scheduled%20Tasks%20%26gt%3B%20New%20%26gt%3B%20Scheduled%20Task%20(At%20least%20Windows%207)%3CBR%20%2F%3E%3CBR%20%2F%3E2.)%20Leave%20default%20Action%20setting%20on%20Update%2C%20provide%20a%20name%20and%20take%20over%20rest%20of%20the%20properties%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_11-1621637304475.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282780iCE14C2C14E2A5ABD%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_11-1621637304475.png%22%20alt%3D%22raymondesray_11-1621637304475.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E3.)%20Go%20to%20Triggers%20tab%2C%20press%20New%20button%3B%20The%20trigger%20will%20ignite%20the%20Action%20which%20will%20be%20configured%20in%20next%20step.%20Event%20ID%204647%20corresponds%20with%20a%20Logoff%20user%20event.%20Press%20OK%20when%20properties%20are%20completed%20as%20shown%20in%20the%20screenshot.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_12-1621637304489.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282783i15CD917C60F68983%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_12-1621637304489.png%22%20alt%3D%22raymondesray_12-1621637304489.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E4.)%20Go%20the%20Actions%20tab%2C%20press%20New%20button%3B%20Take%20over%20the%20properties%20as%20shown%20in%20the%20screenshot%20and%20press%20OK.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_13-1621637304495.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282781i1B7E4D6A126C97F6%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_13-1621637304495.png%22%20alt%3D%22raymondesray_13-1621637304495.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E5.)%20Go%20to%20the%20Settings%20tab%2C%20take%20over%20the%20properties%20as%20shown%20below%20and%20press%20OK.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_14-1621637304510.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282784iED125AC9E578B229%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_14-1621637304510.png%22%20alt%3D%22raymondesray_14-1621637304510.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E6.)%20When%20the%20VM%20receives%20the%20group%20policy%2C%20the%20WVD%20personal%20VM%20will%20shutdown%20once%20the%20user%20logs%20off%20(ending%20idle%2Fdisconnected%20session%20%3Asmiling_face_with_smiling_eyes%3A).%3CBR%20%2F%3E%3CBR%20%2F%3E7.)%20Signed%20in%20with%20my%20user%2C%20GPO%20has%20been%20applied.%20As%20a%20user%20you%20won%E2%80%99t%20see%20the%20scheduled%20task.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_15-1621637304569.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282786iA12AC55D9278F391%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_15-1621637304569.png%22%20alt%3D%22raymondesray_15-1621637304569.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E8.)%20After%205%20minutes%20the%20user%20is%20logged%20off%20automatically%20due%20to%20Remote%20Desktop%20Services%20time%20limit%20policy.%3CBR%20%2F%3E%3CBR%20%2F%3E9.)%20Once%20user%20is%20signed%20out%2C%20the%20shutdown%20action%20will%20be%20triggered.%3CBR%20%2F%3E%3CBR%20%2F%3E10.)%20Confirmed%2C%20that%20VM%20entered%20a%20stopped%20state.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_16-1621637304585.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282785iE621E057470BD90A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_16-1621637304585.png%22%20alt%3D%22raymondesray_16-1621637304585.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E11.)%20With%20the%20next%20hour%20the%20earlier%20created%20Automation%20Account%20runbook%20will%20find%20the%20stopped%20state%20VM%20and%20will%20force%20a%20Stop-AzVm%20that%20puts%20the%20VM%20in%20stopped%20de-allocated%20state.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22raymondesray_17-1621637694379.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F282788i34190EFE2B702E3A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22raymondesray_17-1621637694379.png%22%20alt%3D%22raymondesray_17-1621637694379.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EDone!%20Of%20course%20you%20can%20apply%20this%20runbook%20to%20other%20schedules%20and%20resource%20groups%20to%20ensure%20you%20won%E2%80%99t%20be%20charged%20unnecessary.%3CBR%20%2F%3E%3CBR%20%2F%3EIn%20case%20you%20have%20any%20questions%2C%20please%20let%20me%20know.%3CBR%20%2F%3E%3CBR%20%2F%3EKind%20regards%2C%3CBR%20%2F%3E%3CBR%20%2F%3ERaymond%20van%20Garderen%3CBR%20%2F%3EModern%20Workplace%20%2F%20WVD%20%2F%20M365%3CBR%20%2F%3EConsultant%26nbsp%3B%40SCCT%20The%20Netherlands%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2378947%22%20slang%3D%22en-US%22%3ERe%3A%20MUST%20READ%20-%20WVD%20Personal%20VM%20-%20Proper%20Shutdown%20process%20and%20deallocate%20VM%20to%20save%20costs!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2378947%22%20slang%3D%22en-US%22%3EA%20Great%20article%2C%20very%20detailed.%20Keep%20going%20like%20this!%20%3A)%3C%2Fimg%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2378950%22%20slang%3D%22en-US%22%3ERe%3A%20MUST%20READ%20-%20WVD%20Personal%20VM%20-%20Proper%20Shutdown%20process%20and%20deallocate%20VM%20to%20save%20costs!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2378950%22%20slang%3D%22en-US%22%3EHi!%20Thanks%20for%20your%20positive%20feedback%2C%20highly%20appreciated%20%3B)%3C%2Fimg%3E%3CBR%20%2F%3EHave%20a%20great%20day!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2593419%22%20slang%3D%22en-US%22%3ERe%3A%20MUST%20READ%20-%20WVD%20Personal%20VM%20-%20Proper%20Shutdown%20process%20and%20deallocate%20VM%20to%20save%20costs!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2593419%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F615756%22%20target%3D%22_blank%22%3E%40raymondesray%3C%2FA%3E%26nbsp%3BDoesn't%20work%20on%20GCCH%20%3A(%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22planetkris_0-1627427476612.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F298826i0A489D2744888765%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22planetkris_0-1627427476612.png%22%20alt%3D%22planetkris_0-1627427476612.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

First things first!
Recently Microsoft introduced a really cool feature which is called “Start VM on Connect” and was a long awaiting feature. This feature is groundbreaking because the WVD management plane is able to start the VM on behalf of the user. End-users no longer have to call their IT admins to start the VM. Once the end-user hits the button to start their desktop, the VM will be started automatically and will take approximately 5 minutes. This mystery has been resolved, now the next piece of the puzzle to shutdown and deallocate a WVD personal VM and save consumption costs.

Challenge!
Most of the end-users will shutdown the VM but the result is a VM in “stopped” state. This means the VM is still allocated to a host server in the Azure datacenter and the subscription owner will still be charged. Other possible scenario’s might be that end-users disconnect from the session instead of shutdown the VM or they totally forgot their session which becomes inactive/idle.

An easy, almost effortless two-step solution :smiling_face_with_smiling_eyes:
In order to resolve our challenge it’s good to understand the difference between “idle” and “disconnected” terminology and it’s highly recommended to determine idle session time and disconnected session time within your organization or with your customer. Idle time means that the session is inactive, the end-user is not doing anything in the desktop session. Disconnected time means that the user closes the desktop session without properly signing out or shutting down the VM. My advise here, during onboarding provide proper instructions for your end-users on how to end a desktop session.

Important note/Prerequisite! This will only work when Logon/Logoff events are audited (success/failure). Almost all enterprises have this enabled in a security or default domain policy but please double check!

Configure Azure Automation Account – Finding VM’s with “stopped” state and execute Stop-AzVm to bring VM in “stopped de-allocated” state.
An Azure Automation Account is very powerful and can basically do anything within your Azure environment. For this purpose the Automation Account requires to be created along with a “Run as Account”. This account will act as a trusted service principal and is allowed to execute runbooks (regardless what a runbook contains).

1.) Open up the Azure portal with your admin privileges, in the search bar find Automation Accounts and hit the icon.
2.) Press the create button, complete the fields (provide a clear name, associate the corresponding resource group, set location, and toggle the switch on YES to Create Azure Run As Account! Press the Create button. The creation process will take a few minutes. Time to grab a drink or whatever you might want to do :smiling_face_with_smiling_eyes:
3.) Once completed, open the Automation Accounts, scroll a little bit down, click on Modules, click on Browse Gallery. Find the following modules
- Az.Accounts (select it, press Import, wait a few minutes)
- Az.Compute (select it, press Import, wait a few minutes)


raymondesray_0-1621637304389.png

 



4.) In the Modules overview hit “Refresh” to validate whether both modules are present and have “Available” status.

5.) Within the “Process Automation” section click on Runbooks, followed by pressing the “+ Create a runbook” button.

raymondesray_1-1621637304407.png

 

6.) Fill in a logical name, set Runbook type “PowerShell”, followed by pressing the Create button below.

raymondesray_2-1621637304413.png

 

7.) Now you need to enter a script, completely copy following content starting at param.

Content

param (

    [parameter(Mandatory = $true)][String]$ResourceGroupName

)

 

$ConnectionName = "AzureRunAsConnection"

try

{

    $Connection = Get-AutomationConnection -Name $ConnectionName

 

    # Get the connection "AzureRunAsConnection "

    $ServicePrincipalConnection = Get-AutomationConnection -Name $ConnectionName

 

    "Logging in to Azure..."

    Connect-AzAccount `

        -ServicePrincipal `

        -Tenant $ServicePrincipalConnection.TenantId `

        -ApplicationId $ServicePrincipalConnection.ApplicationId `

        -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint

 

    Set-AzContext -Subscription $Connection.SubscriptionID -Tenant $Connection.TenantId

}

catch {

    if (!$ServicePrincipalConnection)

    {

        $ErrorMessage = "Connection $ConnectionName not found."

        throw $ErrorMessage

    } else {

        Write-Error -Message $_.Exception

        throw $_.Exception

    }

}

 

#Get VMs Status

       [array]$StoppedAzVMs = Get-AzVM -Status

 

       if($StoppedAzVMs) {

#Loop through each VM in this Resource Group

           ForEach($StoppedAzVM in $StoppedAzVMs) {

  

#If PowerState is VM stopped

                   if($StoppedAzVM.PowerState -eq "VM stopped") {

       Stop-AzVM -Name $StoppedAzVM.Name -ResourceGroupName $StoppedAzVM.ResourceGroupName -Force

              }

           }

       }


8.) Hit the “Publish” button

raymondesray_3-1621637304423.png

 


Confirm by pressing Yes.

raymondesray_4-1621637304426.png

 

9.) You will see the overview section of the Runbook, click on Schedules, click the button “+ Add a schedule”.

raymondesray_5-1621637304437.png

 



10.) Click on Link a schedule to your runbook.

raymondesray_6-1621637304442.png

 

11.) Click +Add a schedule, complete the form and press Create below.

raymondesray_7-1621637304447.png

 

12.) Click on Configure Parameters

raymondesray_8-1621637304451.png

 

13.) Fill in the designated resource group name and press OK twice.

raymondesray_9-1621637304456.png

 

14.) Go back to the overview of the runbook and press the Start button to validate.

raymondesray_10-1621637304462.png

 

15.) Since this is a manual start and validation action, enter the resource group name and press OK. Wait for the job to complete.

16.) In the overview section you will see the completed Job, click on it , press the output tab and validate it ran successfully. It depends whether you already have stopped state VM’s.

Next part is to configure session time limits in conjunction with a scheduled task to trigger a shutdown action.


Configure group policy to end idle and disconnected sessions, which means the user will be logged off;
Start your group policy management console, right mouse click on the OU that contains the WVD personal VM computer accounts, create a new GPO and ensure it is linked. Enable and configure the following settings;

Part 1 of GPO:
Computer Configuration\Administrative Templates\Windows Components\Remote Desktop Services\Remote Desktop Session Host\Session Time Limits
1.) End session when time limits are reached > enable
2.) Set time limit for disconnected sessions > enable (for demo purpose 5 minutes but this is what you need to discuss with your organization/customer!)
3.) Set time limit for active but idle Remote Desktop Services sessions > enable (for demo purpose 5 minutes but this is what you need to discuss with your organization/customer!)

Part 2 of GPO:
Computer Configuration\Preferences\Control Panel Settings\Scheduled Tasks
1.) Right mouse click on Scheduled Tasks > New > Scheduled Task (At least Windows 7)

2.) Leave default Action setting on Update, provide a name and take over rest of the properties

raymondesray_11-1621637304475.png

 



3.) Go to Triggers tab, press New button; The trigger will ignite the Action which will be configured in next step. Event ID 4647 corresponds with a Logoff user event. Press OK when properties are completed as shown in the screenshot.

raymondesray_12-1621637304489.png

 

4.) Go the Actions tab, press New button; Take over the properties as shown in the screenshot and press OK.

raymondesray_13-1621637304495.png

 

5.) Go to the Settings tab, take over the properties as shown below and press OK.

raymondesray_14-1621637304510.png

 



6.) When the VM receives the group policy, the WVD personal VM will shutdown once the user logs off (ending idle/disconnected session :smiling_face_with_smiling_eyes:).

7.) Signed in with my user, GPO has been applied. As a user you won’t see the scheduled task.

raymondesray_15-1621637304569.png


8.) After 5 minutes the user is logged off automatically due to Remote Desktop Services time limit policy.

9.) Once user is signed out, the shutdown action will be triggered.

10.) Confirmed, that VM entered a stopped state.

raymondesray_16-1621637304585.png

 

11.) With the next hour the earlier created Automation Account runbook will find the stopped state VM and will force a Stop-AzVm that puts the VM in stopped de-allocated state.

raymondesray_17-1621637694379.png


Done! Of course you can apply this runbook to other schedules and resource groups to ensure you won’t be charged unnecessary.

In case you have any questions, please let me know.

Kind regards,

Raymond van Garderen
Modern Workplace / WVD / M365
Consultant @SCCT The Netherlands

 

3 Replies

A Great article, very detailed.

For real, this is a gamechanger!

Keep going like this! :)

Hi! Thanks for your positive feedback, highly appreciated ;)
Have a great day!