����JFIFXX�����    $.' ",#(7),01444'9=82<.342  2!!22222222222222222222222222222222222222222222222222����"��4�� ���,�PG"Z_�4�˷����kjز�Z�,F+��_z�,�© �����zh6�٨�ic�fu���#ډb���_�N�?��wQ���5-�~�I���8����TK<5o�Iv-�����k�_U_�����~b�M��d����Ӝ�U�Hh��?]��E�w��Q���k�{��_}qFW7HTՑ��Y��F�?_�'ϔ��_�Ջt��=||I ��6�έ"�����D���/[�k�9���Y�8ds|\���Ҿp6�Ҵ���]��.����6�z<�v��@]�i%��$j��~�g��J>��no����pM[me�i$[����s�o�ᘨ�˸ nɜG-�ĨU�ycP�3.DB�li�;��hj���x7Z^�N�h������N3u{�:j�x�힞��#M&��jL P@_���� P��&��o8������9�����@Sz6�t7#O�ߋ �s}Yf�T���lmr����Z)'N��k�۞p����w\�Tȯ?�8`�O��i{wﭹW�[�r�� ��Q4F�׊���3m&L�=��h3����z~��#�\�l :�F,j@�� ʱ�wQT����8�"kJO���6�֚l����}���R�>ډK���]��y����&����p�}b��;N�1�m�r$�|��7�>e�@B�TM*-iH��g�D�)� E�m�|�ؘbҗ�a��Ҿ����t4���o���G��*oCN�rP���Q��@z,|?W[0�����:�n,jWiE��W��$~/�hp\��?��{(�0���+�Y8rΟ�+����>S-S����VN;�}�s?.����� w�9��˟<���Mq4�Wv'��{)0�1mB��V����W[�����8�/<� �%���wT^�5���b��)iM� pg�N�&ݝ��VO~�q���u���9� ����!��J27����$O-���! �:�%H��� ـ����y�ΠM=t{!S�� oK8������t<����è:a������[�����ա�H���~��w��Qz`�po�^ ����Q��n� �,uu�C�$ ^���,������8�#��:�6��e�|~���!�3�3.�\0��q��o�4`.|� ����y�Q�`~;�d�ׯ,��O�Zw�������`73�v�܋�<���Ȏ�� ـ4k��5�K�a�u�=9Yd��$>x�A�&�� j0� ���vF��� Y�|�y��� ~�6�@c��1vOp�Ig����4��l�OD���L����� R���c���j�_�uX6��3?nk��Wy�f;^*B� ��@�~a�`��Eu������+���6�L��.ü>��}y���}_�O�6�͐�:�YrG�X��kG�����l^w���~㒶sy��Iu�!� W ��X��N�7BV��O��!X�2����wvG�R�f�T#�����t�/?���%8�^�W�aT��G�cL�M���I��(J����1~�8�?aT ���]����AS�E��(��*E}� 2��#I/�׍qz��^t�̔���b�Yz4x���t�){ OH��+(E��A&�N�������XT��o��"�XC��'���)}�J�z�p� ��~5�}�^����+�6����w��c��Q�|Lp�d�H��}�(�.|����k��c4^�"�����Z?ȕ ��a<�L�!039C� �Eu�C�F�Ew�ç ;�n?�*o���B�8�bʝ���'#Rqf���M}7����]����s2tcS{�\icTx;�\��7K���P���ʇ Z O-��~��c>"��?�������P��E��O�8��@�8��G��Q�g�a�Վ���󁶠�䧘��_%#r�>�1�z�a��eb��qcPѵ��n���#L��� =��׀t� L�7�`��V���A{�C:�g���e@�w1 Xp3�c3�ġ����p��M"'-�@n4���fG��B3�DJ�8[Jo�ߐ���gK)ƛ��$���� ���8�3�����+���� �����6�ʻ���� ���S�kI�*KZlT _`���?��K����QK�d����B`�s}�>���`��*�>��,*@J�d�oF*����弝��O}�k��s��]��y�ߘ��c1G�V���<=�7��7����6�q�PT��tXԀ�!9*4�4Tހ3XΛex�46���Y��D ����� �BdemDa����\�_l,��G�/���֌7���Y�](�xTt^%�GE�����4�}bT���ڹ�����;Y)���B�Q��u��>J/J �⮶.�XԄ��j�ݳ�+E��d ��r�5�_D�1 ��o�� �B�x�΢�#���<��W�����8���R6�@g�M�.��� dr�D��>(otU��@x=��~v���2� ӣ�d�oBd��3�eO�6�㣷�����ݜ6��6Y��Qz`��S��{���\P�~z m5{J/L��1������<�e�ͅPu�b�]�ϔ���'������f�b� Zpw��c`"��i���BD@:)ִ�:�]��hv�E�w���T�l��P���"Ju�}��وV J��G6��. J/�Qgl߭�e�����@�z�Zev2u�)]կ�����7x���s�M�-<ɯ�c��r�v�����@��$�ޮ}lk���a���'����>x��O\�ZFu>�����ck#��&:��`�$�ai�>2Δ����l���oF[h��lE�ܺ�Πk:)���`�� $[6�����9�����kOw�\|���8}������ބ:��񶐕��I�A1/�=�2[�,�!��.}gN#�u����b��� ~��݊��}34q����d�E��Lc��$��"�[q�U�硬g^��%B �z���r�pJ�ru%v\h1Y�ne`ǥ:g���pQM~�^�Xi� ��`S�:V29.�P���V�?B�k�� AEvw%�_�9C�Q����wKekPؠ�\�;Io d�{ ߞo�c1eP����\� `����E=���@K<�Y���eڼ�J���w����{av�F�'�M�@/J��+9p���|]�����Iw &`��8���&M�hg��[�{��Xj��%��Ӓ�$��(����ʹN���<>�I���RY���K2�NPlL�ɀ)��&e����B+ь����( � �JTx���_?EZ� }@ 6�U���뙢ط�z��dWI�n` D����噥�[��uV��"�G&Ú����2g�}&m��?ċ�"����Om#��������� ��{�ON��"S�X��Ne��ysQ���@Fn��Vg���dX�~nj�]J�<�K]:��FW��b�������62�=��5f����JKw��bf�X�55��~J �%^����:�-�QIE��P��v�nZum� z � ~ə ���� ���ة����;�f��\v���g�8�1��f24;�V���ǔ�)����9���1\��c��v�/'Ƞ�w�������$�4�R-��t���� e�6�/�ġ �̕Ecy�J���u�B���<�W�ַ~�w[B1L۲�-JS΂�{���΃������A��20�c#��@ 0!1@AP"#2Q`$3V�%45a6�FRUq��� ����^7ׅ,$n�������+��F�`��2X'��0vM��p�L=������5��8������u�p~���.�`r�����\���O��,ư�0oS ��_�M�����l���4�kv\JSd���x���SW�<��Ae�IX����������$I���w�:S���y���›R��9�Q[���,�5�;�@]�%���u�@ *ro�lbI �� ��+���%m:�͇ZV�����u�̉����θau<�fc�.����{�4Ա� �Q����*�Sm��8\ujqs]{kN���)qO�y�_*dJ�b�7���yQqI&9�ԌK!�M}�R�;������S�T���1���i[U�ɵz�]��U)V�S6���3$K{�ߊ<�(� E]Զ[ǼENg�����'�\?#)Dkf��J���o��v���'�%ƞ�&K�u�!��b�35LX�Ϸ��63$K�a�;�9>,R��W��3�3� d�JeTYE.Mϧ��-�o�j3+y��y^�c�������VO�9NV\nd�1 ��!͕_)a�v;����թ�M�lWR1��)El��P;��yوÏ�u 3�k�5Pr6<�⒲l�!˞*��u־�n�!�l:����UNW ��%��Chx8vL'��X�@��*��)���̮��ˍ��� ���D-M�+J�U�kvK����+�x8��cY������?�Ԡ��~3mo��|�u@[XeY�C�\Kp�x8�oC�C�&����N�~3-H���� ��MX�s�u<`���~"WL��$8ξ��3���a�)|:@�m�\���^�`�@ҷ)�5p+��6���p�%i)P M���ngc�����#0Aruz���RL+xSS?���ʮ}()#�t��mˇ!��0}}y����<�e� �-ή�Ԩ��X������ MF���ԙ~l L.3���}�V뽺�v�����멬��Nl�)�2����^�Iq��a��M��qG��T�����c3#������3U�Ǎ���}��לS�|qa��ڃ�+���-��2�f����/��bz��ڐ�� �ݼ[2�ç����k�X�2�* �Z�d���J�G����M*9W���s{��w���T��x��y,�in�O�v��]���n����P�$�JB@=4�OTI�n��e�22a\����q�d���%�$��(���:���: /*�K[PR�fr\nڙdN���F�n�$�4�[�� U�zƶ����� �mʋ���,�ao�u 3�z� �x��Kn����\[��VFmbE;�_U��&V�Gg�]L�۪&#n%�$ɯ�dG���D�TI=�%+AB�Ru#��b4�1�»x�cs�YzڙJG��f��Il��d�eF'T� iA��T���uC�$����Y��H?����[!G`}���ͪ� �纤Hv\������j�Ex�K���!���OiƸ�Yj�+u-<���'q����uN�*�r\��+�]���<�wOZ.fp�ێ��,-*)V?j-kÊ#�`�r��dV����(�ݽBk�����G�ƛk�QmUڗe��Z���f}|����8�8��a���i��3'J�����~G_�^���d�8w������ R�`(�~�.��u���l�s+g�bv���W���lGc}��u���afE~1�Ue������Z�0�8�=e�� f@/�jqEKQQ�J��oN��J���W5~M>$6�Lt�;$ʳ{���^��6�{����v6���ķܰg�V�cnn �~z�x�«�,2�u�?cE+Ș�H؎�%�Za�)���X>uW�Tz�Nyo����s���FQƤ��$��*�&�LLXL)�1�" L��eO��ɟ�9=���:t��Z���c��Ž���Y?�ӭV�wv�~,Y��r�ۗ�|�y��GaF�����C�����.�+� ���v1���fήJ�����]�S��T��B��n5sW}y�$��~z�'�c ��8 ��� ,! �p��VN�S��N�N�q��y8z˱�A��4��*��'������2n<�s���^ǧ˭P�Jޮɏ�U�G�L�J�*#��<�V��t7�8����TĜ>��i}K%,���)[��z�21z ?�N�i�n1?T�I�R#��m-�����������������1����lA�`��fT5+��ܐ�c�q՝��ʐ��,���3�f2U�եmab��#ŠdQ�y>\��)�SLY����w#��.���ʑ�f��� ,"+�w�~�N�'�c�O�3F�������N<���)j��&��,-� �љ���֊�_�zS���TǦ����w�>��?�������n��U仆�V���e�����0���$�C�d���rP �m�׈e�Xm�Vu� �L��.�bֹ��� �[Դaզ���*��\y�8�Է:�Ez\�0�Kq�C b��̘��cө���Q��=0Y��s�N��S.���3.���O�o:���#���v7�[#߫ ��5�܎�L���Er4���9n��COWlG�^��0k�%<���ZB���aB_���������'=��{i�v�l�$�uC���mƎҝ{�c㱼�y]���W�i ��ߧc��m�H� m�"�"�����;Y�ߝ�Z�Ǔ�����:S#��|}�y�,/k�Ld� TA�(�AI$+I3��;Y*���Z��}|��ӧO��d�v��..#:n��f>�>���ȶI�TX��� 8��y����"d�R�|�)0���=���n4��6ⲑ�+��r<�O�܂~zh�z����7ܓ�HH�Ga롏���nCo�>������a ���~]���R���̲c?�6(�q�;5%� |�uj�~z8R=X��I�V=�|{v�Gj\gc��q����z�؋%M�ߍ����1y��#��@f^���^�>N�����#x#۹��6�Y~�?�dfPO��{��P�4��V��u1E1J �*|���%���JN��`eWu�zk M6���q t[�� ��g�G���v��WIG��u_ft����5�j�"�Y�:T��ɐ���*�;� e5���4����q$C��2d�}���� _S�L#m�Yp��O�.�C�;��c����Hi#֩%+) �Ӎ��ƲV���SYź��g |���tj��3�8���r|���V��1#;.SQ�A[���S������#���`n�+���$��$I �P\[�@�s��(�ED�z���P��])8�G#��0B��[ى��X�II�q<��9�~[Z멜�Z�⊔IWU&A>�P~�#��dp<�?����7���c��'~���5 ��+$���lx@�M�dm��n<=e�dyX��?{�|Aef ,|n3�<~z�ƃ�uۧ�����P��Y,�ӥQ�*g�#먙R�\���;T��i,��[9Qi歉����c>]9�� ��"�c��P�� �Md?٥��If�ت�u��k��/����F��9�c*9��Ǎ:�ØF���z�n*�@|I�ށ9����N3{'��[�'ͬ�Ҳ4��#}��!�V� Fu��,�,mTIk���v C�7v���B�6k�T9��1�*l� '~��ƞF��lU��'�M ����][ΩũJ_�{�i�I�n��$���L�� j��O�dx�����kza۪��#�E��Cl����x˘�o�����V���ɞ�ljr��)�/,�߬h�L��#��^��L�ф�,íMƁe�̩�NB�L�����iL����q�}��(��q��6IçJ$�W�E$��:������=#����(�K�B����zђ <��K(�N�۫K�w��^O{!����)�H���>x�������lx�?>Պ�+�>�W���,Ly!_�D���Ō�l���Q�!�[ �S����J��1��Ɛ�Y}��b,+�Lo�x�ɓ)����=�y�oh�@�꥟/��I��ѭ=��P�y9��� �ۍYӘ�e+�p�Jnϱ?V\SO%�(�t� ���=?MR�[Ș�����d�/ ��n�l��B�7j� ��!�;ӥ�/�[-���A�>�dN�sLj ��,ɪv��=1c�.SQ�O3�U���ƀ�ܽ�E����������̻��9G�ϷD�7(�}��Ävӌ\�y�_0[w ���<΍>����a_��[0+�L��F.�޺��f�>oN�T����q;���y\��bՃ��y�jH�<|q-eɏ�_?_9+P���Hp$�����[ux�K w�Mw��N�ی'$Y2�=��q���KB��P��~������Yul:�[<����F1�2�O���5=d����]Y�sw:���Ϯ���E��j,_Q��X��z`H1,#II ��d�wr��P˂@�ZJV����y$�\y�{}��^~���[:N����ߌ�U�������O��d�����ؾe��${p>G��3c���Ė�lʌ�� ת��[��`ϱ�-W����dg�I��ig2��� ��}s ��ؤ(%#sS@���~���3�X�nRG�~\jc3�v��ӍL��M[JB�T��s3}��j�Nʖ��W����;7��ç?=X�F=-�=����q�ߚ���#���='�c��7���ڑW�I(O+=:uxq�������������e2�zi+�kuG�R��������0�&e�n���iT^J����~\jy���p'dtG��s����O��3����9* �b#Ɋ�� p������[Bws�T�>d4�ۧs���nv�n���U���_�~,�v����ƜJ1��s�� �QIz��)�(lv8M���U=�;����56��G���s#�K���MP�=��LvyGd��}�VwWBF�'�à �?MH�U�g2�� ����!�p�7Q��j��ڴ����=��j�u��� Jn�A s���uM������e��Ɔ�Ҕ�!)'��8Ϣ�ٔ��ޝ(��Vp���צ֖d=�IC�J�Ǡ{q������kԭ�߸���i��@K����u�|�p=..�*+����x�����z[Aqġ#s2a�Ɗ���RR�)*HRsi�~�a &f��M��P����-K�L@��Z��Xy�'x�{}��Zm+���:�)�) IJ�-i�u���� ���ܒH��'�L(7�y�GӜq���� j��� 6ߌg1�g�o���,kر���tY�?W,���p���e���f�OQS��!K�۟cҒA�|ս�j�>��=⬒��˧L[�� �߿2JaB~R��u�:��Q�] �0H~���]�7��Ƽ�I���(}��cq '�ήET���q�?f�ab���ӥvr� �)o��-Q��_'����ᴎo��K������;��V���o��%���~OK ����*��b�f:���-ťIR��`B�5!RB@���ï�� �u �̯e\�_U�_������� g�ES��3�������QT��a����x����U<~�c?�*�#]�MW,[8O�a�x��]�1bC|踤�P��lw5V%�)�{t�<��d��5���0i�XSU��m:��Z�┵�i�"��1�^B�-��P�hJ��&)O��*�D��c�W��vM��)����}���P��ܗ-q����\mmζZ-l@�}��a��E�6��F�@��&Sg@���ݚ�M����� ȹ 4����#p�\H����dYDo�H���"��\��..R�B�H�z_�/5˘����6��KhJR��P�mƶi�m���3�,#c�co��q�a)*Pt����R�m�k�7x�D�E�\Y�閣_X�<���~�)���c[[�BP����6�Yq���S��0����%_����;��Àv�~�| VS؇ ��'O0��F0��\���U�-�d@�����7�SJ*z��3n��y��P����O���������m�~�P�3|Y��ʉr#�C�<�G~�.,! ���bqx���h~0=��!ǫ�jy����l�O,�[B��~��|9��ٱ����Xly�#�i�B��g%�S��������tˋ���e���ې��\[d�t)��.+u�|1 ������#�~Oj����hS�%��i.�~X���I�H�m��0n���c�1uE�q��cF�RF�o���7� �O�ꮧ� ���ۛ{��ʛi5�rw?׌#Qn�TW��~?y$��m\�\o����%W� ?=>S�N@�� �Ʈ���R����N�)�r"C�:��:����� �����#��qb��Y�. �6[��2K����2u�Ǧ�HYR��Q�MV��� �G�$��Q+.>�����nNH��q�^��� ����q��mM��V��D�+�-�#*�U�̒ ���p욳��u:�������IB���m���PV@O���r[b= �� ��1U�E��_Nm�yKbN�O���U�}�the�`�|6֮P>�\2�P�V���I�D�i�P�O;�9�r�mAHG�W�S]��J*�_�G��+kP�2����Ka�Z���H�'K�x�W�MZ%�O�YD�Rc+o��?�q��Ghm��d�S�oh�\�D�|:W������UA�Qc yT�q������~^�H��/��#p�CZ���T�I�1�ӏT����4��"�ČZ�����}��`w�#�*,ʹ�� ��0�i��課�Om�*�da��^gJ݅{���l�e9uF#T�ֲ��̲�ٞC"�q���ߍ ոޑ�o#�XZTp����@ o�8��(jd��xw�]�,f���`~�|,s��^����f�1���t��|��m�򸄭/ctr��5s��7�9Q�4�H1꠲BB@l9@���C�����+�wp�xu�£Yc�9��?`@#�o�mH�s2��)�=��2�.�l����jg�9$�Y�S�%*L������R�Y������7Z���,*=�䷘$�������arm�o�ϰ���UW.|�r�uf����IGw�t����Zwo��~5 ��YյhO+=8fF�)�W�7�L9lM�̘·Y���֘YLf�큹�pRF���99.A �"wz��=E\Z���'a� 2��Ǚ�#;�'}�G���*��l��^"q��+2FQ� hj��kŦ��${���ޮ-�T�٭cf�|�3#~�RJ����t��$b�(R��(����r���dx� >U b�&9,>���%E\� Ά�e�$��'�q't��*�א���ެ�b��-|d���SB�O�O��$�R+�H�)�܎�K��1m`;�J�2�Y~9��O�g8=vqD`K[�F)k�[���1m޼c��n���]s�k�z$@��)!I �x՝"v��9=�ZA=`Ɠi �:�E��)`7��vI��}d�YI�_ �o�:ob���o ���3Q��&D&�2=�� �Ά��;>�h����y.*ⅥS������Ӭ�+q&����j|UƧ����}���J0��WW< ۋS�)jQR�j���Ư��rN)�Gű�4Ѷ(�S)Ǣ�8��i��W52���No˓� ۍ%�5brOn�L�;�n��\G����=�^U�dI���8$�&���h��'���+�(������cȁ߫k�l��S^���cƗjԌE�ꭔ��gF���Ȓ��@���}O���*;e�v�WV���YJ\�]X'5��ղ�k�F��b 6R�o՜m��i N�i����>J����?��lPm�U��}>_Z&�KK��q�r��I�D�Չ~�q�3fL�:S�e>���E���-G���{L�6p�e,8��������QI��h��a�Xa��U�A'���ʂ���s�+טIjP�-��y�8ۈZ?J$��W�P� ��R�s�]��|�l(�ԓ��sƊi��o(��S0��Y� 8�T97.�����WiL��c�~�dxc�E|�2!�X�K�Ƙਫ਼�$((�6�~|d9u+�qd�^3�89��Y�6L�.I�����?���iI�q���9�)O/뚅����O���X��X�V��ZF[�یgQ�L��K1���RҖr@v�#��X�l��F���Нy�S�8�7�kF!A��sM���^rkp�jP�DyS$N���q��nxҍ!U�f�!eh�i�2�m���`�Y�I�9r�6� �TF���C}/�y�^���Η���5d�'��9A-��J��>{�_l+�`��A���[�'��յ�ϛ#w:݅�%��X�}�&�PSt�Q�"�-��\縵�/����$Ɨh�Xb�*�y��BS����;W�ջ_mc�����vt?2}1�;qS�d�d~u:2k5�2�R�~�z+|HE!)�Ǟl��7`��0�<�,�2*���Hl-��x�^����'_TV�gZA�'j� ^�2Ϊ��N7t�����?w�� �x1��f��Iz�C-Ȗ��K�^q�;���-W�DvT�7��8�Z�������� hK�(P:��Q- �8�n�Z���܃e貾�<�1�YT<�,�����"�6{/ �?�͟��|1�:�#g��W�>$����d��J��d�B��=��jf[��%rE^��il:��B���x���Sּ�1հ��,�=��*�7 fcG��#q� �eh?��2�7�����,�!7x��6�n�LC�4x��},Geǝ�tC.��vS �F�43��zz\��;QYC,6����~;RYS/6���|2���5���v��T��i����������mlv��������&� �nRh^ejR�LG�f���? �ۉҬܦƩ��|��Ȱ����>3����!v��i�ʯ�>�v��オ�X3e���_1z�Kȗ\<������!�8���V��]��?b�k41�Re��T�q��mz��TiOʦ�Z��Xq���L������q"+���2ۨ��8}�&N7XU7Ap�d�X��~�׿��&4e�o�F��� �H����O���č�c�� 懴�6���͉��+)��v;j��ݷ�� �UV�� i��� j���Y9GdÒJ1��詞�����V?h��l����l�cGs�ځ�������y�Ac�����\V3�? �� ܙg�>qH�S,�E�W�[�㺨�uch�⍸�O�}���a��>�q�6�n6����N6�q������N ! 1AQaq�0@����"2BRb�#Pr���3C`��Scst���$4D���%Td�� ?���N����a��3��m���C���w��������xA�m�q�m���m������$����4n淿t'��C"w��zU=D�\R+w�p+Y�T�&�պ@��ƃ��3ޯ?�Aﶂ��aŘ���@-�����Q�=���9D��ռ�ѻ@��M�V��P��܅�G5�f�Y<�u=,EC)�<�Fy'�"�&�չ�X~f��l�KԆV��?�� �W�N����=(� �;���{�r����ٌ�Y���h{�١������jW����P���Tc�����X�K�r��}���w�R��%��?���E��m�� �Y�q|����\lEE4���r���}�lsI�Y������f�$�=�d�yO����p�����yBj8jU�o�/�S��?�U��*������ˍ�0������u�q�m [�?f����a�� )Q�>����6#������� ?����0UQ����,IX���(6ڵ[�DI�MNލ�c&���υ�j\��X�R|,4��� j������T�hA�e��^���d���b<����n�� �즇�=!���3�^�`j�h�ȓr��jẕ�c�,ٞX����-����a�ﶔ���#�$��]w�O��Ӫ�1y%��L�Y<�wg#�ǝ�̗`�x�xa�t�w��»1���o7o5��>�m뭛C���Uƃߜ}�C���y1Xνm�F8�jI���]����H���ۺиE@I�i;r�8ӭ����V�F�Շ| ��&?�3|x�B�MuS�Ge�=Ӕ�#BE5G�����Y!z��_e��q�р/W>|-�Ci߇�t�1ޯќd�R3�u��g�=0 5��[?�#͏��q�cf���H��{ ?u�=?�?ǯ���}Z��z���hmΔ�BFTW�����<�q�(v� ��!��z���iW]*�J�V�z��gX֧A�q�&��/w���u�gYӘa���; �i=����g:��?2�dž6�ى�k�4�>�Pxs����}������G�9��3 ���)gG�R<>r h�$��'nc�h�P��Bj��J�ҧH� -��N1���N��?��~��}-q!=��_2hc�M��l�vY%UE�@|�v����M2�.Y[|y�"Eï��K�ZF,�ɯ?,q�?v�M 80jx�"�;�9vk�����+ ֧�� �ȺU��?�%�vcV��mA�6��Qg^M����A}�3�nl� QRN�l8�kkn�'�����(��M�7m9و�q���%ޟ���*h$Zk"��$�9��: �?U8�Sl��,,|ɒ��xH(ѷ����Gn�/Q�4�P��G�%��Ա8�N��!� �&�7�;���eKM7�4��9R/%����l�c>�x;������>��C�:�����t��h?aKX�bhe�ᜋ^�$�Iհ �hr7%F$�E��Fd���t��5���+�(M6�t����Ü�UU|zW�=a�Ts�Tg������dqP�Q����b'�m���1{|Y����X�N��b �P~��F^F:����k6�"�j!�� �I�r�`��1&�-$�Bevk:y���#yw��I0��x��=D�4��tU���P�ZH��ڠ底taP��6����b>�xa����Q�#� WeF��ŮNj�p�J* mQ�N����*I�-*�ȩ�F�g�3 �5��V�ʊ�ɮ�a��5F���O@{���NX��?����H�]3��1�Ri_u��������ѕ�� ����0��� F��~��:60�p�͈�S��qX#a�5>���`�o&+�<2�D����: �������ڝ�$�nP���*)�N�|y�Ej�F�5ټ�e���ihy�Z �>���k�bH�a�v��h�-#���!�Po=@k̆IEN��@��}Ll?j�O������߭�ʞ���Q|A07x���wt!xf���I2?Z��<ץ�T���cU�j��]��陎Ltl �}5�ϓ��$�,��O�mˊ�;�@O��jE��j(�ا,��LX���LO���Ц�90�O �.����a��nA���7������j4 ��W��_ٓ���zW�jcB������y՗+EM�)d���N�g6�y1_x��p�$Lv:��9�"z��p���ʙ$��^��JԼ*�ϭ����o���=x�Lj�6�J��u82�A�H�3$�ٕ@�=Vv�]�'�qEz�;I˼��)��=��ɯ���x �/�W(V���p�����$ �m�������u�����񶤑Oqˎ�T����r��㠚x�sr�GC��byp�G��1ߠ�w e�8�$⿄����/�M{*}��W�]˷.�CK\�ުx���/$�WPw���r� |i���&�}�{�X� �>��$-��l���?-z���g����lΆ���(F���h�vS*���b���߲ڡn,|)mrH[���a�3�ר�[1��3o_�U�3�TC�$��(�=�)0�kgP���� ��u�^=��4 �WYCҸ:��vQ�ר�X�à��tk�m,�t*��^�,�}D*� �"(�I��9R����>`�`��[~Q]�#af��i6l��8���6�:,s�s�N6�j"�A4���IuQ��6E,�GnH��zS�HO�uk�5$�I�4��ؤ�Q9�@��C����wp�BGv[]�u�Ov���0I4���\��y�����Q�Ѹ��~>Z��8�T��a��q�ޣ;z��a���/��S��I:�ܫ_�|������>=Z����8:�S��U�I�J��"IY���8%b8���H��:�QO�6�;7�I�S��J��ҌAά3��>c���E+&jf$eC+�z�;��V����� �r���ʺ������my�e���aQ�f&��6�ND��.:��NT�vm�<- u���ǝ\MvZY�N�NT��-A�>jr!S��n�O 1�3�Ns�%�3D@���`������ܟ 1�^c<���� �a�ɽ�̲�Xë#�w�|y�cW�=�9I*H8�p�^(4���՗�k��arOcW�tO�\�ƍR��8����'�K���I�Q�����?5�>[�}��yU�ײ -h��=��% q�ThG�2�)���"ו3]�!kB��*p�FDl�A���,�eEi�H�f�Ps�����5�H:�Փ~�H�0Dت�D�I����h�F3�������c��2���E��9�H��5�zԑ�ʚ�i�X�=:m�xg�hd(�v����׊�9iS��O��d@0ڽ���:�p�5�h-��t�&���X�q�ӕ,��ie�|���7A�2���O%P��E��htj��Y1��w�Ѓ!����  ���� ࢽ��My�7�\�a�@�ţ�J �4�Ȼ�F�@o�̒?4�wx��)��]�P��~�����u�����5�����7X ��9��^ܩ�U;Iꭆ 5 �������eK2�7(�{|��Y׎ �V��\"���Z�1� Z�����}��(�Ǝ"�1S���_�vE30>���p;� ΝD��%x�W�?W?v����o�^V�i�d��r[��/&>�~`�9Wh��y�;���R��� ;;ɮT��?����r$�g1�K����A��C��c��K��l:�'��3 c�ﳯ*"t8�~l��)���m��+U,z��`(�>yJ�?����h>��]��v��ЍG*�{`��;y]��I�T� ;c��NU�fo¾h���/$���|NS���1�S�"�H��V���T���4��uhǜ�]�v;���5�͠x��'C\�SBpl���h}�N����� A�Bx���%��ޭ�l��/����T��w�ʽ]D�=����K���ž�r㻠l4�S�O?=�k �M:� ��c�C�a�#ha���)�ѐxc�s���gP�iG��{+���x���Q���I= �� z��ԫ+ �8"�k�ñ�j=|����c ��y��CF��/��*9ж�h{ �?4�o� ��k�m�Q�N�x��;�Y��4膚�a�w?�6�>e]�����Q�r�:����g�,i"�����ԩA�*M�<�G��b�if��l^M��5� �Ҩ�{����6J��ZJ�����P�*�����Y���ݛu�_4�9�I8�7���������,^ToR���m4�H��?�N�S�ѕw��/S��甍�@�9H�S�T��t�ƻ���ʒU��*{Xs�@����f�����֒Li�K{H�w^���������Ϥm�tq���s� ���ք��f:��o~s��g�r��ט� �S�ѱC�e]�x���a��) ���(b-$(�j>�7q�B?ӕ�F��hV25r[7 Y� }L�R��}����*sg+��x�r�2�U=�*'WS��ZDW]�WǞ�<��叓���{�$�9Ou4��y�90-�1�'*D`�c�^o?(�9��u���ݐ��'PI&� f�Jݮ�������:wS����jfP1F:X �H�9dԯ���˝[�_54 �}*;@�ܨ�� ð�yn�T���?�ןd�#���4rG�ͨ��H�1�|-#���Mr�S3��G�3�����)�.᧏3v�z֑��r����$G"�`j �1t��x0<Ɔ�Wh6�y�6��,œ�Ga��gA����y��b��)��h�D��ß�_�m��ü �gG;��e�v��ݝ�nQ� ��C����-�*��o���y�a��M��I�>�<���]obD��"�:���G�A��-\%LT�8���c�)��+y76���o�Q�#*{�(F�⽕�y����=���rW�\p���۩�c���A���^e6��K������ʐ�cVf5$�'->���ՉN"���F�"�UQ@�f��Gb~��#�&�M=��8�ט�JNu9��D��[̤�s�o�~������ G��9T�tW^g5y$b��Y'��س�Ǵ�=��U-2 #�MC�t(�i� �lj�@Q 5�̣i�*�O����s�x�K�f��}\��M{E�V�{�υ��Ƈ�����);�H����I��fe�Lȣr�2��>��W�I�Ȃ6������i��k�� �5�YOxȺ����>��Y�f5'��|��H+��98pj�n�.O�y�������jY��~��i�w'������l�;�s�2��Y��:'lg�ꥴ)o#'Sa�a�K��Z� �m��}�`169�n���"���x��I ��*+� }F<��cГ���F�P�������ֹ*�PqX�x۩��,� ��N�� �4<-����%����:��7����W���u�`����� $�?�I��&����o��o��`v�>��P��"��l���4��5'�Z�gE���8���?��[�X�7(��.Q�-��*���ތL@̲����v��.5���[��=�t\+�CNܛ��,g�SQnH����}*F�G16���&:�t��4ُ"A��̣��$�b �|����#rs��a�����T�� ]�<�j��BS�('$�ɻ� �wP;�/�n��?�ݜ��x�F��yUn�~mL*-�������Xf�wd^�a�}��f�,=t�׵i�.2/wpN�Ep8�OР���•��R�FJ� 55TZ��T �ɭ�<��]��/�0�r�@�f��V��V����Nz�G��^���7hZi����k��3�,kN�e|�vg�1{9]_i��X5y7� 8e]�U����'�-2,���e"����]ot�I��Y_��n�(JҼ��1�O ]bXc���Nu�No��pS���Q_���_�?i�~�x h5d'�(qw52] ��'ޤ�q��o1�R!���`ywy�A4u���h<קy���\[~�4�\ X�Wt/� 6�����n�F�a8��f���z �3$�t(���q��q�x��^�XWeN'p<-v�!�{�(>ӽDP7��ո0�y)�e$ٕv�Ih'Q�EA�m*�H��RI��=:��� ���4牢) �%_iN�ݧ�l]� �Nt���G��H�L��� ɱ�g<���1V�,�J~�ٹ�"K��Q�� 9�HS�9�?@��k����r�;we݁�]I�!{ �@�G�[�"��`���J:�n]�{�cA�E����V��ʆ���#��U9�6����j�#Y�m\��q�e4h�B�7��C�������d<�?J����1g:ٳ���=Y���D�p�ц� ׈ǔ��1�]26؜oS�'��9�V�FVu�P�h�9�xc�oq�X��p�o�5��Ա5$�9W�V(�[Ak�aY錎qf;�'�[�|���b�6�Ck��)��#a#a˙��8���=äh�4��2��C��4tm^ �n'c���]GQ$[Wҿ��i���vN�{Fu ��1�gx��1┷���N�m��{j-,��x�� Ūm�ЧS�[�s���Gna���䑴�� x�p 8<������97�Q���ϴ�v�aϚG��Rt�Һ׈�f^\r��WH�JU�7Z���y)�vg=����n��4�_)y��D'y�6�]�c�5̪�\� �PF�k����&�c;��cq�$~T�7j ���nç]�<�g ":�to�t}�159�<�/�8������m�b�K#g'I'.W�����6��I/��>v��\�MN��g���m�A�yQL�4u�Lj�j9��#44�t��l^�}L����n��R��!��t��±]��r��h6ٍ>�yҏ�N��fU�� ���� Fm@�8}�/u��jb9������he:A�y�ծw��GpΧh�5����l}�3p468��)U��d��c����;Us/�֔�YX�1�O2��uq�s��`hwg�r~�{ R��mhN��؎*q 42�*th��>�#���E����#��Hv�O����q�}�����6�e��\�,Wk�#���X��b>��p}�դ��3���T5��†��6��[��@�P�y*n��|'f�֧>�lư΂�̺����SU�'*�q�p�_S�����M�� '��c�6�����m�� ySʨ;M��r���Ƌ�m�Kxo,���Gm�P��A�G�:��i��w�9�}M(�^�V��$ǒ�ѽ�9���|���� �a����J�SQ�a���r�B;����}���ٻ֢�2�%U���c�#�g���N�a�ݕ�'�v�[�OY'��3L�3�;,p�]@�S��{ls��X�'���c�jw�k'a�.��}�}&�� �dP�*�bK=ɍ!����;3n�gΊU�ߴmt�'*{,=SzfD� A��ko~�G�aoq�_mi}#�m�������P�Xhύ����mxǍ�΂���巿zf��Q���c���|kc�����?���W��Y�$���_Lv����l߶��c���`?����l�j�ݲˏ!V��6����U�Ђ(A���4y)H���p�Z_�x��>���e��R��$�/�`^'3qˏ�-&Q�=?��CFVR �D�fV�9��{�8g�������n�h�(P"��6�[�D���< E�����~0<@�`�G�6����Hг�cc�� �c�K.5��D��d�B���`?�XQ��2��ٿyqo&+�1^� DW�0�ꊩ���G�#��Q�nL3��c���������/��x ��1�1[y�x�პCW��C�c�UĨ80�m�e�4.{�m��u���I=��f�����0QRls9���f���������9���~f�����Ǩ��a�"@�8���ȁ�Q����#c�ic������G��$���G���r/$W�(��W���V�"��m�7�[m�A�m����bo��D� j����۳� l���^�k�h׽����� ��#� iXn�v��eT�k�a�^Y�4�BN��ĕ��0 !01@Q"2AaPq3BR������?���@4�Q�����T3,���㺠�W�[=JK�Ϟ���2�r^7��vc�:�9 �E�ߴ�w�S#d���Ix��u��:��Hp��9E!�� V 2;73|F��9Y���*ʬ�F��D����u&���y؟��^EA��A��(ɩ���^��GV:ݜDy�`��Jr29ܾ�㝉��[���E;Fzx��YG��U�e�Y�C���� ����v-tx����I�sם�Ę�q��Eb�+P\ :>�i�C'�;�����k|z�رn�y]�#ǿb��Q��������w�����(�r|ӹs��[�D��2v-%��@;�8<a���[\o[ϧw��I!��*0�krs)�[�J9^��ʜ��p1)� "��/_>��o��<1����A�E�y^�C��`�x1'ܣn�p��s`l���fQ��):�l����b>�Me�jH^?�kl3(�z:���1ŠK&?Q�~�{�ٺ�h�y���/�[��V�|6��}�KbX����mn[-��7�5q�94�������dm���c^���h� X��5��<�eޘ>G���-�}�دB�ޟ� ��|�rt�M��V+�]�c?�-#ڛ��^ǂ}���Lkr���O��u�>�-D�ry� D?:ޞ�U��ǜ�7�V��?瓮�"�#���r��չģVR;�n���/_� ؉v�ݶe5d�b9��/O��009�G���5n�W����JpA�*�r9�>�1��.[t���s�F���nQ� V 77R�]�ɫ8����_0<՜�IF�u(v��4��F�k�3��E)��N:��yڮe��P�`�1}�$WS��J�SQ�N�j�ٺ��޵�#l���ј(�5=��5�lǏmoW�v-�1����v,W�mn��߀$x�<����v�j(����c]��@#��1������Ǔ���o'��u+����;G�#�޸��v-lη��/(`i⣍Pm^���ԯ̾9Z��F��������n��1��� ��]�[��)�'������:�֪�W��FC����� �B9،!?���]��V��A�Վ�M��b�w��G F>_DȬ0¤�#�QR�[V��kz���m�w�"��9ZG�7'[��=�Q����j8R?�zf�\a�=��O�U����*oB�A�|G���2�54 �p��.w7� �� ��&������ξxGHp� B%��$g�����t�Џ򤵍z���HN�u�Я�-�'4��0��;_��3 !01"@AQa2Pq#3BR������?��ʩca��en��^��8���<�u#��m*08r��y�N"�<�Ѳ0��@\�p��� �����Kv�D��J8�Fҽ� �f�Y��-m�ybX�NP����}�!*8t(�OqѢ��Q�wW�K��ZD��Δ^e��!� ��B�K��p~�����e*l}z#9ң�k���q#�Ft�o��S�R����-�w�!�S���Ӥß|M�l޶V��!eˈ�8Y���c�ЮM2��tk���� ������J�fS����Ö*i/2�����n]�k�\���|4yX�8��U�P.���Ы[���l��@"�t�<������5�lF���vU�����W��W��;�b�cД^6[#7@vU�xgZv��F�6��Q,K�v��� �+Ъ��n��Ǣ��Ft���8��0��c�@�!�Zq s�v�t�;#](B��-�nῃ~���3g������5�J�%���O������n�kB�ĺ�.r��+���#�N$?�q�/�s�6��p��a����a��J/��M�8��6�ܰ"�*������ɗud"\w���aT(����[��F��U՛����RT�b���n�*��6���O��SJ�.�ij<�v�MT��R\c��5l�sZB>F��<7�;EA��{��E���Ö��1U/�#��d1�a�n.1ě����0�ʾR�h��|�R��Ao�3�m3 ��%�� ���28Q� ��y��φ���H�To�7�lW>����#i`�q���c����a��� �m,B�-j����݋�'mR1Ήt�>��V��p���s�0IbI�C.���1R�ea�����]H�6����������4B>��o��](��$B���m�����a�!=��?�B� K�Ǿ+�Ծ"�n���K��*��+��[T#�{E�J�S����Q�����s�5�:�U�\wĐ�f�3����܆&�)����I���Ԇw��E T�lrTf6Q|R�h:��[K�� �z��c֧�G�C��%\��_�a�84��HcO�bi��ؖV��7H �)*ģK~Xhչ0��4?�0��� �E<���}3���#���u�?�� ��|g�S�6ꊤ�|�I#Hڛ� �ա��w�X��9��7���Ŀ%�SL��y6č��|�F�a 8���b��$�sק�h���b9RAu7�˨p�Č�_\*w��묦��F ����4D~�f����|(�"m���NK��i�S�>�$d7SlA��/�²����SL��|6N�}���S�˯���g��]6��; �#�.��<���q'Q�1|KQ$�����񛩶"�$r�b:���N8�w@��8$�� �AjfG|~�9F ���Y��ʺ��Bwؒ������M:I岎�G��`s�YV5����6��A �b:�W���G�q%l�����F��H���7�������Fsv7��k�� 403WebShell
403Webshell
Server IP : 64.57.112.4  /  Your IP : 216.73.216.57
Web Server : Microsoft-IIS/10.0
System : Windows NT WP 10.0 build 17763 (Windows Server 2016) AMD64
User : voguebusinesssuppliers ( 0)
PHP Version : 7.3.3
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : OFF  |  Perl : OFF  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  C:/Windows/System32/WindowsPowerShell/v1.0/Modules/RemoteDesktop/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : C:/Windows/System32/WindowsPowerShell/v1.0/Modules/RemoteDesktop//VirtualDesktopCollection.psm1

Import-Module $PSScriptRoot\Utility.psm1


# .ExternalHelp RemoteDesktop.psm1-help.xml
function New-VirtualDesktopCollection {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254090")]
param(
#switch for collection type
    [Parameter(Mandatory=$true, ParameterSetName="PooledMgd")]
    [switch]
    $PooledManaged,

    [Parameter(Mandatory=$true, ParameterSetName="PersonalMgd")]
    [switch]
    $PersonalManaged,

    [Parameter(Mandatory=$true, ParameterSetName="PooledUnmgd")]
    [switch]
    $PooledUnmanaged,

    [Parameter(Mandatory=$true, ParameterSetName="PersonalUnmgd")]
    [switch]
    $PersonalUnmanaged,
#common params
    [Parameter(Mandatory=$true, Position=0)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$false)]
    [string]
    $Description,

    [Parameter(Mandatory=$false)]
    [string[]]
    $UserGroups,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker,

#Unmanaged collection Params
    [Parameter(Mandatory=$true, ParameterSetName="PooledUnmgd")]
    [Parameter(Mandatory=$true, ParameterSetName="PersonalUnmgd")]
    [string[]]
    $VirtualDesktopName,

#Managed collection Params
    [Parameter(Mandatory=$true, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$true, ParameterSetName="PersonalMgd")]
    [string]
    $VirtualDesktopTemplateName,

    [Parameter(Mandatory=$true, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$true, ParameterSetName="PersonalMgd")]
    [string]
    $VirtualDesktopTemplateHostServer,

    [Parameter(Mandatory=$true, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$true, ParameterSetName="PersonalMgd")]
    [Hashtable]
    $VirtualDesktopAllocation,

    [Parameter(Mandatory=$true, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$true, ParameterSetName="PersonalMgd")]
    [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]
    $StorageType,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [string]
    $CentralStoragePath,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [string]
    $LocalStoragePath,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [string]
    $VirtualDesktopTemplateStoragePath,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [string]
    $Domain,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [string]
    $OU="",

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [string]
    $CustomSysprepUnattendFilePath,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [string]
    $VirtualDesktopNamePrefix,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [switch]
    $DisableVirtualDesktopRollback,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [ValidateRange(31,365)]
    [int]
    $VirtualDesktopPasswordAge,

#shared coll params
    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PooledUnmgd")]
    [string]
    $UserProfileDiskPath,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PooledUnmgd")]
    [ValidateRange(1,9999)]
    [int]
    $MaxUserProfileDiskSizeGB=40,
   
#personal coll params
    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PersonalUnmgd")]
    [switch]
    $AutoAssignPersonalVirtualDesktopToUser,

    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [Parameter(Mandatory=$false, ParameterSetName="PersonalUnmgd")]
    [switch]
    $GrantAdministrativePrivilege,

    [Parameter(Mandatory=$false)]
    [switch]
    $Force

)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    #verify deployment
    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #Verify that collection with same name does not exist
    Write-Verbose (Get-ResourceString VerifyingInput)
    if(-not (Confirm-CollectionNameDoesNotExist -CollectionName $CollectionName -RDMgmtServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString RDVHCollectionAlreadyExist)
        return
    }

    #verify IF User VHD path is given is not already used in the deployment
    if($PSBoundParameters["UserProfileDiskPath"])
    {   
        #check UserVhd path is not in use     
        if (Test-UserVhdPathInUse -UserVhdPath $UserProfileDiskPath -CollectionName $CollectionName -ConnectionBroker $ConnectionBroker)
        {
            Write-Error (Get-ResourceString FolderPathInUse $UserProfileDiskPath)
            return
        }
        #check UserVhd path has Everyone - warn the admin if true
        if(Test-EveryoneInUserVhdPath -UserVhdPath $UserProfileDiskPath)
        {
              Write-Warning (Get-ResourceString EveryoneHasAccess $UserProfileDiskPath)
        }
    }

    if(-not ($PSBoundParameters["Domain"]))
    {
        $Domain = Get-RDManagementServerDomain($ConnectionBroker)        
        if(-not $Domain)
        {
            Write-Error (Get-ResourceString FailedToGetComputerObject $ConnectionBroker)
            return
        }
    }
    #Handle if the user explicitly set empty array or null (e.g. "-UserGroups @()")
    if($null -eq $UserGroups)
    {
        # find domain users name
        
        $UserGroups = ConvertTo-NtAccount([microsoft.remotedesktopservices.common.commonutility]::GetCurrentDomainUserSid())
    }
    elseif($UserGroups.Count -eq 0)
    {
        Write-Error (Get-ResourceString ErrVmCollectionRequiresUserGroups)
        return
    }
    
    #Prepare Security descriptor for all the user Groups, also does the validation by converting them to Sids
    $secDesc=Get-SecurityDescriptorFromUserGroup($UserGroups)
    if(-not $secDesc)
    {
        Write-Error (Get-ResourceString InvalidUserGroupNoErr $UserGroups)
        return
    }

    #set collection type depending on switch param and AutoAssign
    $type=1
    if( ($PSBoundParameters["PooledManaged"]) -or
        ($PSBoundParameters["PooledUnmanaged"]) )
    {
        $type=1
    }
    elseif ($AutoAssignPersonalVirtualDesktopToUser)
    {
        $type=3
    }
    else
    {
        $type=2
    }

    #Generate Unique collection ID, For now reuse Name
    $alias = Get-CollectionAlias -CollectionName $CollectionName -RDManagementServer $ConnectionBroker
    if([string]::IsNullOrEmpty($alias))
    {
        Write-Error (Get-ResourceString CannotCreateCollectionAlias)
        return
    }

    #check if managed or Unmanaged
    $managed = $false
    if( ($PSBoundParameters["PooledManaged"]) -or
        ($PSBoundParameters["PersonalManaged"]) )
    {
        $managed = $true
    }

    if(-not ($managed))
    {
        $UnmgdVmHostList = New-Object System.Collections.Generic.List[string]
        #verify the input list of unamanged VMs
        if (-not (Test-UnmanagedVmList -VirtualDesktopList $VirtualDesktopName -ConnectionBroker $ConnectionBroker -UnmgdVmHostList $UnmgdVmHostList))
        {
            return
        }

        #create Unmanaged Collection WMI instance
        Write-Verbose (Get-ResourceString CreatingRdvhCollection $CollectionName)

        $scope = New-Object System.Management.ManagementScope
        $scope.Path = '\\'+$ConnectionBroker+'\root\cimv2\RDMS:Win32_RDMSVirtualDesktopCollection'
        $scope.Options.Authentication = [system.Management.AuthenticationLevel]::PacketPrivacy
        $scope.Options.Impersonation = [system.Management.ImpersonationLevel]::Impersonate

        $classInst = new-Object System.Management.ManagementClass($scope, $scope.Path, $null)
        
        try
        {
            $vmColl= $classInst.CreateInstance()

            $vmColl.Name=$CollectionName
            $vmColl.Alias=$alias
            $vmColl.IsHA=$false
            $vmColl.IsManaged=$false
            $vmColl.IsUserAdmin=$GrantAdministrativePrivilege
            $vmColl.SecurityDescriptor=$secDesc
            $vmColl.ShowInPortal=$true
            $vmColl.Type=$type
            $vmColl.CollectionDescription=$Description
            $putRes = $vmColl.Put()
        }
        catch
        {
            $errMsg = $_.Exception.Message
        }

        if( -not $putRes )
        {
            Write-Error (Get-ResourceString CreateRdvhCollectionFailed $errMsg)
            return
        }


        #join virtual machines to collection
        Add-RDVirtualDesktopToCollection $CollectionName -VirtualDesktopName $VirtualDesktopName -ConnectionBroker $ConnectionBroker | Out-Null

        #set the default RDP properties for the collection
        Update-CollectionPropertiesToDefault -CollectionName $CollectionName -IsVMCollection $true -ConnectionBroker $ConnectionBroker

        if($PSBoundParameters["UserProfileDiskPath"])
        {        
            Write-Verbose "Enabling user profile disks"
            try
            {
                $workflowSession = New-PSSession -ConfigurationName Microsoft.Windows.ServerManagerWorkflows -EnableNetworkAccess

                Invoke-Command -Session $workflowSession -ArgumentList $ConnectionBroker, $alias, $UserProfileDiskPath, $MaxUserProfileDiskSizeGB `
                {
                    param($ConnectionBroker, $alias, $UserProfileDiskPath, $MaxUserProfileDiskSizeGB ) 
                    RDManagement\Enable-UserDataVHD -RDMSServer $ConnectionBroker -CollectionAlias $alias -UserVhdShare $UserProfileDiskPath `
                        -DiskSize ([UInt64]($MaxUserProfileDiskSizeGB*1GB)) -DiskType 1 -RemapMode 0
                } | Out-Null
            }
            catch
            {
                Write-Error (Get-ResourceString FailedToEnableUvhd)
            }
            finally
            {
                if($workflowSession)
                {
                    Remove-PSSession -Session $workflowSession
                }
            }
        }

        #return object for the created collection
        Get-RDVirtualDesktopCollection -CollectionName $CollectionName -ConnectionBroker $ConnectionBroker
        return
    }


    #Call the Util fn to verify domain and OU
    # this fn will convert the OU to the DN if only OU name is specified in non-distinguished name format
    if( ($PSBoundParameters["OU"]))
    {
        $OU = Get-DistinguishedName -Domain $Domain -OrganizationalUnit $OU
        if(-not $OU)
        {
            Write-Error (Get-ResourceString InvalidDomainOrOU $Domain, $OU)
            return
        }
        Write-Verbose ("Testing OU permissions")
        if(-not (Test-RDOUAccess -Domain $Domain -OU $OU -ConnectionBroker $ConnectionBroker))
        {
            if($Force)
            {
                Write-Warning (Get-ResourceString WrnOUAccessNotGranted $OU)
            }
            else
            {
                if(-not $PSCmdlet.ShouldContinue((Get-ResourceString ShouldContinueOUAccessNotGranted $OU), ""))
                {
                    return
                }
            }
        }
    }
    else
    {
        #call the Get-DistinguishedName to verify the domain only
        $computersOU = Get-DistinguishedName -Domain $Domain -OrganizationalUnit "Computers"
        if(-not $computersOU)
        {
            Write-Error (Get-ResourceString InvalidDomainOrOU $Domain, $OU)
            return
        } 

        if(-not (Test-RDOUAccess -Domain $Domain -OU $computersOU -ConnectionBroker $ConnectionBroker))
        {
            if($Force)
            {
                Write-Warning (Get-ResourceString WrnOUAccessNotGranted $computersOU)
            }
            else
            {
                if(-not $PSCmdlet.ShouldContinue((Get-ResourceString ShouldContinueOUAccessNotGranted $computersOU), ""))
                {
                    return
                }
            }
        }
    }
    

    #Trim the Naming prefix for auto created VMs
    if (-not ($VirtualDesktopNamePrefix))
    {
        $VirtualDesktopNamePrefix = ($alias+"-")
        Write-Verbose ("No virtual desktop name prefix specified, using collection alias: "+$VirtualDesktopNamePrefix)
    }
    if($VirtualDesktopNamePrefix.Length -gt 11)
    {
        $VirtualDesktopNamePrefix=$VirtualDesktopNamePrefix.Substring(0,11)
        Write-Verbose ("Name prefix exceeds max length--Trimmed virtual desktop name prefix: "+$VirtualDesktopNamePrefix)
    }

    #set the machine password expiry registry keys for all the hosts only when Rollback is enabled!
    #hence applicable only to PooledManaged
    if (($PSBoundParameters["VirtualDesktopPasswordAge"]))
    {
        if ( -not($PSBoundParameters["PooledManaged"]) -or ($DisableVirtualDesktopRollback))
        {
            Write-Error (Get-ResourceString InvalidHostConfigurationSetting "VirtualDesktopPasswordAge")
            return
        }
    }

    #validate input managed VM collection params
    $validateParams=Get-BoundParameter $PSBoundParameters @{
                                        "CollectionName" = "CollectionName"
                                        "VirtualDesktopTemplateHostServer"="VirtualDesktopTemplateHostServer"
                                        "VirtualDesktopTemplateName"="VirtualDesktopTemplateName"
                                        "CustomSysprepUnattendFilePath"="CustomUnattend"
                                        "StorageType"="StorageType"
                                        "LocalStoragePath"="LocalStoragePath"
                                        "CentralStoragePath"="CentralStoragePath"
                                        "VirtualDesktopTemplateStoragePath"="VmTemplateStoragePath"
                                        "UserProfileDiskPath"="UvhdSharePath"
                                        "MaxUserProfileDiskSizeGB"="UvhdSize"
                                        }
    if( -not (Test-ManagedParam @validateParams -Type $type -VmNamePrefix $VirtualDesktopNamePrefix `
                -VmDistribution $VirtualDesktopAllocation -ConnectionBroker $ConnectionBroker))
    {
        return
    }
        

    ##
    ## Setup export paths
    ##

    #prepare host name list
    $hostList = New-Object System.Collections.Generic.List[string]
    foreach($hostName in $VirtualDesktopAllocation.Keys)
    {
        $hostList.Add($hostName)
    }
    #Get export root
    $exportRoot = Get-ExportRoot -StorageType $StorageType  -CentralStoragePath $CentralStoragePath -VmTemplateStoragePath $VirtualDesktopTemplateStoragePath -ConnectionBroker $ConnectionBroker
    if(-not $exportRoot)
    {
        return
    }
    #make sure export root is accessible from masterVM host for SAN case
    if($StorageType -eq [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::CentralSanStorage)
    {
        if(-not (Test-LocalStoragePath -Path $exportRoot -HostList $VirtualDesktopTemplateHostServer))
        {
            return
        }
    }

    #make sure that Master VM Host server has permissions and access to the export share
    if(-not (Set-ShareProvisioningPermissions -Path $exportRoot -HostList $VirtualDesktopTemplateHostServer))
    {
        return
    }

    # make sure that for local storage the export share has perms for all the host, because export root will be 
    # different from local VM creation path and template Storage paths
    if($StorageType -eq [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::LocalStorage)
    {
        if(-not (Set-ShareProvisioningPermissions -Path $exportRoot -HostList $hostList))
        {
            return
        }

        #make sure that Master VM Host server has permissions and access to the export share
        if(-not (Set-ShareProvisioningPermissions -Path $exportRoot -HostList $VirtualDesktopTemplateHostServer))
        {
            return
        }
    }

    #TODO: ???share the "default" export Root when Storage type is local, OR is it shared by RDMS automatically???
    $exportPath = New-VMExportLocation -ExportRoot $exportRoot -Alias $alias -VirtualDesktopTemplateHostServer $VirtualDesktopTemplateHostServer
    if(-not $exportPath)
    {
        return
    }

    #get all host servers in the deployment
    $allHostsList = Get-RDServer -ConnectionBroker $ConnectionBroker -Role "RDS-VIRTUALIZATION"
    $hostNamesList = New-Object System.Collections.Generic.List[string]
    foreach($hostName in $allHostsList.Server)
    {
        $hostNamesList.Add($hostName)
    }

    if(-not $hostNamesList)
    {
        Write-Error ("The RDVHs list is empty!")
        return
    } 
    #Setup paths and constants depending on storage type - give all hosts permissions to the SMB share
    if(-not (Set-StoragePermissions  -StorageType $StorageType -CentralStoragePath $CentralStoragePath -LocalStoragePath $LocalStoragePath `
                            -VmTemplateStoragePath $VirtualDesktopTemplateStoragePath -HostList $hostNamesList))
 
    {
        return
    }

    ##
    ## Prepare Provisioning Parameters
    ##

    #select disk type depending on the pooltype
    $vhdType = "DiffDisk"
    if(1 -ne $type)
    {
        $vhdType = "Clone"
    }

    $runVmFromSmb = 0
    $enableVmStreaming = 0
    $highlyAvailable = $false
    switch($StorageType)
    {
        #[Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::
        LocalStorage
        {
            $smbShare=""
            $runVmFromSmb=0
            $enableVmStreaming=0
        }
        #[Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::
        CentralSmbShareStorage
        {
            $smbShare=$CentralStoragePath
            $runVmFromSmb=1
            $enableVmStreaming=0
            $LocalStoragePath=$null #make sure it is null
        }
        #[Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::
        CentralSanStorage
        {
            $smbShare=""
            $runVmFromSmb=1
            $enableVmStreaming=0
            $LocalStoragePath=$CentralStoragePath
            $highlyAvailable = $true            
        }
    }

    # Setting the password age for the VMs across all the hosts.
    if (($PSBoundParameters["VirtualDesktopPasswordAge"]))
    {
        if (-not (Set-VirtualDesktopHostSetting -CollectionName $CollectionName -ConnectionBroker $ConnectionBroker `
                        -Name "VirtualDesktopPasswordAge" -Value $VirtualDesktopPasswordAge))
        {
            return
        }
    }

    #create provisioning XML
    $provXml = Get-ProvisioningXml -CollectionAlias $alias -Job "Create" -VhdType $vhdType -BaseVmLocation $exportPath `
                    -SmbShare $smbShare -LocalVmCreationPath $LocalStoragePath -LocalGoldCachePath $VirtualDesktopTemplateStoragePath `
                    -RunVmFromSmb $runVmFromSmb -EnableVmStreaming $enableVmStreaming -CustomUnattend $CustomSysprepUnattendFilePath `
                    -Prefix $VirtualDesktopNamePrefix -StartIndex 1 -Domain $Domain -OU $OU -HostList $VirtualDesktopAllocation `
                    -HighlyAvailable $highlyAvailable
    if(-not $provXml)
    {
        return
    }

    ##
    ## Call New-RDVMCollection Workflow
    ##

    #get mapping input params
    $params = Get-BoundParameter $PSBoundParameters @{
                                            "CollectionName" = "Name"
                                            "Description"="Description"
                                            "VirtualDesktopTemplateHostServer"="MasterVmhostServer"
                                            "VirtualDesktopTemplateName"="MasterVmName"
                                            "UserProfileDiskPath"="UserDataVHDShare"
                                            "MaxUserProfileDiskSizeGB"="UserDataVHDDiskSize"
                                            }                                                                                            
    #set EnableRollback input param
    $params["RollbackEnabled"] = [bool](-not $DisableVirtualDesktopRollback)

    #invoke create collection XAML from RDManagement, it does the export, creates collection and rollout VMs
    $workflowSuccess = $true
    try
    {
        $workflowSession = New-PSSession -ConfigurationName Microsoft.Windows.ServerManagerWorkflows -EnableNetworkAccess

        Invoke-Command -Session $workflowSession -ArgumentList @($params, $type, $alias, $highlyAvailable, $exportPath, $secDesc, $provXml, $ConnectionBroker, $GrantAdministrativePrivilege) `
        {
            param($params, $type, $alias, $highlyAvailable, $exportPath, $secDesc, $provXml, $ConnectionBroker, $GrantAdministrativePrivilege) 
            RDManagement\New-RDVMCollection @params -Type $type -Managed $true -CollectionAlias $alias -HighlyAvailable $highlyAvailable `
                            -ExportLocation $exportPath -UserSecurityDescriptor $secDesc `
                            -ProvisioningXml $provXml -RDManagementServer $ConnectionBroker -IsUserAdmin $GrantAdministrativePrivilege
        }| Out-Null
        # TODO: error handling
    }
    catch
    {
        $workflowSuccess=$false
        $errMsg = $_.Exception.Message
    }
    finally
    {
        if($workflowSession)
        {
            Remove-PSSession -Session $workflowSession
        }
    }
    
    #workflow succeeded, get the collection instance
    if($workflowSuccess)
    {
        $x=Get-RDVirtualDesktopCollection -CollectionName $CollectionName -ConnectionBroker $ConnectionBroker -ErrorAction SilentlyContinue
        if($x)
        {
            Update-CollectionPropertiesToDefault -CollectionName $CollectionName -IsVMCollection $true -ConnectionBroker $ConnectionBroker
            #update the ExportInProgress property to default
            try
            {
                $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                        -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                            -Filter "Name='$CollectionName'"
                $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
            }
            catch
            {
                #ignore the error
                Write-Debug ("Failed to set the exportInProgress to default");
            }

            $x
        }
        else
        {
            Write-Error (Get-ResourceString CreateRdvhCollectionFailed $errMsg)
        }
    }
    else
    {
        Write-Error (Get-ResourceString CreateRdvhCollectionFailed $errMsg)
    }
}


# .ExternalHelp RemoteDesktop.psm1-help.xml
function Get-VirtualDesktopCollection {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254091")]
[OutputType("Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopCollection[]")]
param(
    [Parameter(Mandatory=$false, Position=0)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    #verify deployment
    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    $query=""
    if( -not $CollectionName)
    {
        $query = "Select * from Win32_RDMSVirtualDesktopCollection"
    }
    else
    {
        $query = "Select * from Win32_RDMSVirtualDesktopCollection where Name='"+$CollectionName+"'"
    }
    try
    {
        $rdvmCollections = Get-WmiObject -Query $query -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                        -Impersonation Impersonate -Authentication PacketPrivacy -ErrorAction Stop
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if(-not $rdvmCollections)
    {
        Write-Verbose (Get-ResourceString RdvmCollectionNotFound)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        if(![string]::IsNullOrEmpty($CollectionName))
        {
            Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        }
        return
    }

    foreach($rdvmColl in $rdvmCollections)
    {
        $securityGroups = Get-UserGroupsFromSecurityDescriptor($rdvmColl.SecurityDescriptor)
        $type = $rdvmColl.Type
        
        $rdCollectionType = 0
        $autoassign = $false
            
        Get-RDVirtualDesktopCollectionType -BackEndCollectionType $rdvmColl.Type -IsManaged $rdvmColl.IsManaged ([ref] $rdCollectionType) ([ref] $autoassign)

        #get size and in use counts
        try
        {
            $alias=$rdvmColl.Alias
            $vdObjs = [wmi[]](Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                -Filter "CollectionAlias='$alias'")
            $size=$vdObjs.Count

            $percentInUse=0

            if($size)
            {
                $currSessions = [wmi[]](Get-WmiObject -Class "Win32_SessionDirectorySessionEx" -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "CollectionAlias='$alias'")


                if($currSessions)
                {
                    $percentInUse = [int](($currSessions.Count/$size)*100)
                } 
            }
        }
        catch 
        {
            Write-Error ($_.Exception.Message)
        }
        New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopCollection `
                                        -ArgumentList $rdvmColl.Alias, $rdvmColl.Name, $rdvmColl.CollectionDescription, $rdCollectionType, `
                                        $rdvmColl.ShowInPortal, $autoassign, $rdvmColl.IsUserAdmin, $rdvmColl.RollbackEnabled, `
                                        $securityGroups,$size,$percentInUse
    }
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function Remove-VirtualDesktopCollection {

[CmdletBinding(SupportsShouldProcess=$true,
HelpURI="http://go.microsoft.com/fwlink/?LinkId=254092")]
param(
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory=$false)]
    [switch]
    $Force
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    #verify deployment
    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #get all the VMs in the collection
    try
    {
        $alias = $rdvmColl.Alias
        $vmObjs = Get-WmiObject Win32_RDMSVirtualDesktop -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "CollectionAlias='$alias'"
    }
    catch
    {
        #write the error and try to delete the pool
        Write-Error ($_.Exception.Message)
    }

    $promptMsg = (Get-ResourceString RemoveVmCollMsg $rdvmColl.Name)
    if( -not ($PSCmdlet.ShouldProcess( $promptMsg, $promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }

    if( (-not $Force) -and (-not $PSCmdlet.ShouldContinue($promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }

    #If there are VMs in the pool, clenaup
    if(($vmObjs))
    {
        Write-Verbose (Get-ResourceString RemovingVirtDesktopFromCollection)
			
        #If managed pool start provisioning delete job
        if ($rdvmColl.IsManaged) 
        {
            #verify if there is another job already running
            if(-not (Test-ScheduleProvisioningJob ($rdvmColl)))
            {
                return
            }

            #get Provisioning XML using saved Prov Props for the coll
            $masterLocation=""
            $provXml = Get-ProvXmlForExistingCollection -RdvmColl $rdvmColl -Type $rdvmColl.Type -JobType "Delete" ([ref] $masterLocation) -ConnectionBroker $ConnectionBroker
            if(-not $provXml)
            {
                return
            }

            #start provisioning job
            try
            {
                $workflowSession = New-PSSession -ConfigurationName Microsoft.Windows.ServerManagerWorkflows -EnableNetworkAccess

                Invoke-Command -Session $workflowSession -ArgumentList @($rdvmColl.Alias, $provXml, $ConnectionBroker) `
                {
                    param($alias, $provXml, $ConnectionBroker)
                    RDManagement\Start-RDVMRollout -Alias $alias -ProvisioningXml $provXml -ManagementServer $ConnectionBroker
                } | Out-Null
                # TODO: error handling
            }
            catch
            {
                Write-Error (Get-ResourceString DeleteMgdVmFromCollectionFailed $_.Exception.Message)
            }
            finally
            {
                if($workflowSession)
                {
                    Remove-PSSession -Session $workflowSession
                }
            }


            #delete master export location(s)
            Remove-Item ($masterLocation+"\..\__*") -Recurse -Force -ErrorAction SilentlyContinue
        }

        #now for both managed and unmanaged collections, remove vms from collection
        foreach($vm in $vmObjs)
        { 
            $errMsg = $null
            try
            {
                $remRes = $rdvmColl.RemoveVirtualDesktop($vm.Name)
            }
            catch
            {
                $errMsg = $_.Exception.Message
            }
            if(-not $remRes)
            {
                Write-Error (Get-ResourceString DeleteUnmgdVmFromCollectionFailed $vm,$errMsg)
            }
            elseif(0 -ne $remRes.ReturnValue)
            {
                Write-Error (Get-ResourceString DeleteUnmgdVmFromCollectionFailed $vm,$remRes.ReturnValue)
            }
        }
    }

    #remove collection instance
    try
    {
        #todo: check $out
        $delRes = $rdvmColl.Delete()
    }
    catch
    {
        $delErrMsg = $_.Exception.Message
    }
    if($delErrMsg)
    {
        Write-Error (Get-ResourceString DeleteRdvhCollectionFailed $delErrMsg)
        return
    }
    Write-Verbose (Get-ResourceString CollectionDeleted)
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function Add-VirtualDesktopToCollection{
[CmdletBinding(DefaultParameterSetName="PooledMgd",
HelpURI="http://go.microsoft.com/fwlink/?LinkId=254093")]
param(
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true, ParameterSetName="UnManaged")]
    [string[]]
    $VirtualDesktopName,

    [Parameter(Mandatory=$true, ParameterSetName="PooledMgd")]
    [Parameter(Mandatory=$true, ParameterSetName="PersonalMgd")]
    [Hashtable]
    $VirtualDesktopAllocation,

    #The following three applies only to PersonalDesktop collection
    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [string]
    $VirtualDesktopTemplateName,

    [Parameter(Mandatory=$false, ParameterSetName="PersonalMgd")]
    [string]
    $VirtualDesktopTemplateHostServer,

    [Parameter(Mandatory=$false, ParameterSetName="PooledMgd")]
    [ValidateRange(31,365)]
    [int]
    $VirtualDesktopPasswordAge,
            
    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    #verify deployment
    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #VirtualDesktopPasswordAge is allowed only for pools with rollback enb
    if ($PSBoundParameters["VirtualDesktopPasswordAge"])
    {
        if( -not ($rdvmColl.RollbackEnabled))
        {
            Write-Error (Get-ResourceString InvalidHostConfigurationSetting "VirtualDesktopPasswordAge")
            return
        }
    }
       
    if($rdvmColl.IsManaged)
    {                
        # Verify the virtual machine ditribution
        if(-not ($VirtualDesktopAllocation))
        {
            Write-Error (Get-ResourceString SpecifyVmAllocation)
            return
        }

        if (-not (Test-VmDistribution -VmDistribution $VirtualDesktopAllocation -ConnectionBroker $ConnectionBroker))
        {
            return
        }

        #verify if there is another job already running
        if(-not (Test-ScheduleProvisioningJob ($rdvmColl)))
        {
            return
        }

        # Setting the password age for the VMs across all the hosts.
        if (($PSBoundParameters["VirtualDesktopPasswordAge"]))
        {
            if (-not (Set-VirtualDesktopHostSetting -CollectionName $CollectionName -ConnectionBroker $ConnectionBroker `
                            -Name "VirtualDesktopPasswordAge" -Value $VirtualDesktopPasswordAge))
            {
                return
            }
        }
             
        #we will not support specified new mastervm for shared/temp pools
        if ( (-not($rdvmColl.type -eq 3)) -and (-not($rdvmColl.type -eq 2)))
        {
            if(-not [string]::IsNullOrEmpty($VirtualDesktopTemplateName))
            {
                Write-Error (Get-ResourceString UnsupportedVirtualDesktopTemplateOption "VirtualDesktopTemplateName")
                return
            }  
            if(-not [string]::IsNullOrEmpty($VirtualDesktopTemplateHostServer))
            {
                Write-Error (Get-ResourceString UnsupportedVirtualDesktopTemplateOption "VirtualDesktopTemplateHostServer")
                return
            }    
        }

        #if it a managed personal desktop collection
        if( ($rdvmColl.type -eq 3) -or ($rdvmColl.type -eq 2))
        {        
            #Do we use the existing mastervm or is a new one specified?
            if(-not [string]::IsNullOrEmpty($VirtualDesktopTemplateName))
            {            
                #The check will apply only for pd collections
                if(-not(Test-MasterVm   -VirtualDesktopTemplateName $VirtualDesktopTemplateName `
                            -VirtualDesktopTemplateHostServer $VirtualDesktopTemplateHostServer `
                            -ConnectionBroker $ConnectionBroker))
                {
                    return
                }
            
                #get provisioning properties
                try
                {
                    $alias=$rdvmColl.Alias
                    $collProps= Get-WmiObject Win32_RDMSCollectionProperties -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                            -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                            -Filter "Alias='$alias'"
                }
                catch
                {
                    Write-Error ($_.Exception.Message)
                }
                if( -not $collProps )
                {
                    Write-Error (Get-ResourceString VmCollectionPropNotFound)
                    return
                }

                try
                {
                    $provProps=$collProps.GetProvisioningProperties()
                }
                catch
                {
                    Write-Error ($_.Exception.Message)
                }
                if(-not $provProps)
                {
                    Write-Error (Get-ResourceString VmCollectionProvPropNotFound)
                    return
                }

                $oldExportPath = $provProps.MasterVmLocation

                $centralStoragePath = [string]::Empty
                if($rdvmColl.IsHA)
                {
                    $storageType = [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::CentralSanStorage
                    $centralStoragePath = $provProps.LocalVmLocation
                }
                elseif(-not [string]::IsNullOrEmpty($provProps.SMBLocation))
                {
                    $storageType = [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::CentralSmbShareStorage
                    $centralStoragePath = $provProps.SMBLocation
                }
                else
                {
                    $storageType = [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::LocalStorage
                }
                $vmTemplateStoragePath = $provProps.LocalGoldVmLocation

                #generate Export Path
                $exportRoot = Get-ExportRoot -StorageType $storageType  -CentralStoragePath $centralStoragePath -VmTemplateStoragePath $vmTemplateStoragePath -ConnectionBroker $ConnectionBroker
                if( -not $exportRoot)
                {
                    return
                }

                $exportPath = New-VMExportLocation -ExportRoot $exportRoot -Alias $rdvmColl.Alias -VirtualDesktopTemplateHostServer $VirtualDesktopTemplateHostServer
                if(-not $exportPath)
                {
                    return
                }

                #Set Export info so that Get-Status could show export in progress
                try
                {
                    $rdvmColl.SetStringProperty("ExportInProgress",$VirtualDesktopTemplateName)|Out-Null
                }
                catch
                {
                    Write-Verbose ("Failed to set export in progress")
                    #ignore error
                }

                #export the master VM
                try
                {
                    $workflowSession = New-PSSession -ConfigurationName Microsoft.Windows.ServerManagerWorkflows -EnableNetworkAccess

                    $masterVmDetails = Invoke-Command -Session $workflowSession -ArgumentList @($exportPath, $VirtualDesktopTemplateHostServer, $VirtualDesktopTemplateName) `
                                        {
                                            param($exportPath, $VirtualDesktopTemplateHostServer, $VirtualDesktopTemplateName)
                                            RDManagement\Export-MasterVM -ExportLocation $exportPath -VMHostName $VirtualDesktopTemplateHostServer `
                                                -VMName $VirtualDesktopTemplateName -Override $false -SysPrepGeneralize $false
                                        }
                    # TODO: error handling
                }
                catch
                {
                    Write-Error ($_.Exception.Message)
                }
                finally
                {
                    if($workflowSession)
                    {
                        Remove-PSSession -Session $workflowSession
                    }
                }
    
                if(-not $masterVmDetails)
                {
                    Write-Error (Get-ResourceString ErrorExportFailed)

                    #remove export folder
                    Remove-Item $exportPath -Recurse -Force -ErrorAction SilentlyContinue
                    try
                    {
                        $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
                    }
                    catch
                    {
                        Write-Verbose ("Failed to clear export in progress")
                        #ignore error
                    }
                    return
                }

                #save Master VM details
                $collProps.ReferenceVirtualDesktopName = $VirtualDesktopTemplateName
                $collProps.ReferenceVirtualDesktopHost = $VirtualDesktopTemplateHostServer
                $collProps.ReferenceVirtualDesktopOsversion = $masterVmDetails.OSVersion
                $collProps.ReferenceVirtualDesktopArchitecture = $masterVMDetails.ProcessorArchitecture
                $collProps.ReferenceVirtualDesktopGuid = $masterVMDetails.VirtualMachineGuid
                $collProps.ReferenceVirtualDesktopRamsizeMB = $masterVMDetails.Memory
                $collProps.ReferenceVirtualDesktopRemoteFX = $masterVMDetails.RemoteFX

                $adapterNames = New-Object System.Collections.Generic.List[string]
                foreach($adapter in $masterVmDetails.VirtualSwitch)
                {
                    $adapterNames.Add($adapter)
                }
                $collProps.ReferenceVirtualDesktopAdapters = $adapterNames

                try
                {
                    $putRes = $collProps.Put()
                }
                catch
                {
                    Write-Error ($_.Exception.Message)
                }
                if(-not $putRes)
                {
                    Write-Error(Get-ResourceString FailedToSaveVmCollProp)
                    #remove export folder
                    Remove-Item $exportPath -Recurse -Force -ErrorAction SilentlyContinue
                    try
                    {
                        $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
                    }
                    catch
                    {
                        Write-Verbose ("Failed to clear export in progress")
                        #ignore error
                    }
                    return
                }            
            }
        }

        # get provisioning XML, also makes sure all the VMHost have permission on the shares
        $provXml = Get-ProvXmlForExistingCollection -RdvmColl $rdvmColl -Type $rdvmColl.Type -JobType "Update" `
                    -CustomMasterVmLocation $exportPath -VmDistribution $VirtualDesktopAllocation -CheckStoragePerms $true -ConnectionBroker $ConnectionBroker
        if(-not $provXml)
        {
            try
            {
                $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
            }
            catch
            {
                Write-Verbose ("Failed to clear export in progress")
                #ignore error
            }
            return
        }

        try
        {
            $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
        }
        catch
        {
            Write-Verbose ("Failed to clear export in progress")
            #ignore error
        }
        #start provisioning job
        try
        {
            $workflowSession = New-PSSession -ConfigurationName Microsoft.Windows.ServerManagerWorkflows -EnableNetworkAccess

            Invoke-Command -Session $workflowSession -ArgumentList @($rdvmColl.Alias, $provXml, $ConnectionBroker) `
            {
                param($alias, $provXml, $ConnectionBroker)
                RDManagement\Start-RDVMRollout -Alias $alias -ProvisioningXml $provXml -ManagementServer $ConnectionBroker
            } | Out-Null
            # TODO: error handling
        }
        catch
        {
            Write-Error (Get-ResourceString AddVmToRdvhCollectionFailed $_.Exception.Message)
        }
        finally
        {
            if($workflowSession)
            {
                Remove-PSSession -Session $workflowSession
            }
        }

        # cleanup old export path, old export path will only be non-null if 
        # new template was exported for PD pools
        if ($oldExportPath)
        {
            Remove-Item $oldExportPath -Recurse -Force -ErrorAction SilentlyContinue
        }

        $jobStatus=Get-RDVirtualDesktopCollectionJobStatus $CollectionName -ConnectionBroker $ConnectionBroker
        if(-not $jobStatus)
        {
            return
        }
	    $virtualDesktopStatus= ($jobStatus.VirtualDesktopStatus | Sort -property VirtualDesktopName)
        foreach($vmStatus in $virtualDesktopStatus)
        {
            if(![string]::IsNullOrEmpty( $vmStatus.Error ))
            {
                continue
            }
            $vm=$vmStatus.VirtualDesktopName
            try
            {
                Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$vm'" | `
                    ForEach-Object {
                            New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktop `
                                -ArgumentList $_.Name, $CollectionName, $_.HostName,  $_.VMState, $_.Status
                            }
            }
            catch
            {
                Write-Warning (Get-ResourceString FailedToGetVirtualDesktop $vm,$_.Exception.Message)
            }
        }
    }
    else
    {
        if (-not ($VirtualDesktopName))
        {
            Write-Error (Get-ResourceString SpecifyVmList)
            return
        }

        if (-not (Test-UnmanagedVmList -VirtualDesktopList $VirtualDesktopName -ConnectionBroker $ConnectionBroker))
        {
            return
        }

        foreach($vm in $VirtualDesktopName)
        {
            Write-Verbose ( Get-ResourceString AddingVmToCollection $vm )
            $errMsg=$null
            try
            {
                $addRes = $rdvmColl.AddVirtualDesktop($vm)
                Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$vm'" | `
                    ForEach-Object {
                            New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktop `
                                -ArgumentList $_.Name, $CollectionName, $_.HostName,  $_.VMState, $_.Status
                         }
            }
            catch
            {
                $errMsg = $_.Exception.Message
            }
            if(-not $addRes)
            {
                Write-Error (Get-ResourceString AddUnmgdVmFailed $vm,$errMsg)
            }
            elseif(0 -ne $addRes.ReturnValue)            
            {
                Write-Error (Get-ResourceString AddUnmgdVmFailed $vm,$addRes.ReturnValue)
            }
            elseif($errMsg)
            {
                Write-Warning (Get-ResourceString FailedToGetVirtualDesktop $vm,$errMsg)
            }
        }
    }
}


# .ExternalHelp RemoteDesktop.psm1-help.xml
function Update-VirtualDesktopCollection {

[CmdletBinding(DefaultParameterSetName="Now",SupportsShouldProcess=$true,
HelpURI="http://go.microsoft.com/fwlink/?LinkId=254094")]
param(
#common params
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true)]
    [string]
    $VirtualDesktopTemplateName,

    [Parameter(Mandatory=$true)]
    [string]
    $VirtualDesktopTemplateHostServer,

    [Parameter(Mandatory=$false)]
    [switch]
    $DisableVirtualDesktopRollback,

    [Parameter(Mandatory=$false)]
    [ValidateRange(31,365)]
    [int]
    $VirtualDesktopPasswordAge,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory=$false)]
    [switch]
    $Force,

#Selective Params
    [Parameter(Mandatory=$true, ParameterSetName="OnUserLogoff")]
    [DateTime]
    $StartTime,
    
    [Parameter(Mandatory=$true, ParameterSetName="OnUserLogoff")]
    [Parameter(Mandatory=$true, ParameterSetName="OnSchedule")]
    [DateTime]
    $ForceLogoffTime
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    #verify deployment
    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #verify it is managed and shared
    if(-not ($rdvmColl.IsManaged))
    {
        Write-Error (Get-ResourceString RecreateErrorNotManagedVmCollection)
        return
    }
    
    if( 1 -ne $rdvmColl.Type)
    {
        Write-Error (Get-ResourceString RecreateErrorNotSharedVmCollection)
        return
    }

    if ($PSBoundParameters["VirtualDesktopPasswordAge"])
    {
        if( -not ($rdvmColl.RollbackEnabled))
        {
            Write-Error (Get-ResourceString InvalidHostConfigurationSetting "VirtualDesktopPasswordAge")
            return
        }
    }

    switch($PsCmdlet.ParameterSetName)
    {
        "OnUserLogoff"
        {
            if($StartTime -gt $ForceLogoffTime)
            {
                Write-Error (Get-ResourceString ErrStartTimeGTLogoffTime)
                return
            }
        }
        "OnSchedule"
        {
            $StartTime = $ForceLogoffTime
        }
        "Now"
        {
            $StartTime = Get-Date
            $ForceLogoffTime = $StartTime
        }
    }

    if(-not(Test-MasterVm   -VirtualDesktopTemplateName $VirtualDesktopTemplateName `
                -VirtualDesktopTemplateHostServer $VirtualDesktopTemplateHostServer `
                -ConnectionBroker $ConnectionBroker))
    {
        return
    }


    #get provisioning properties
    try
    {
        $alias=$rdvmColl.Alias
        $collProps= Get-WmiObject Win32_RDMSCollectionProperties -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                -Filter "Alias='$alias'"
    }
    catch
    {
        Write-Error ($_.Exception.Message)
    }
    if( -not $collProps )
    {
        Write-Error (Get-ResourceString VmCollectionPropNotFound)
        return
    }

    try
    {
        $provProps=$collProps.GetProvisioningProperties()
    }
    catch
    {
        Write-Error ($_.Exception.Message)
    }
    if(-not $provProps)
    {
        Write-Error (Get-ResourceString VmCollectionProvPropNotFound)
        return
    }

    #all validations are complete, now prompt
    $promptMsg = (Get-ResourceString UpdateVmCollMsg $rdvmColl.Name)
    if( -not ($PSCmdlet.ShouldProcess( $promptMsg, $promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }

    if( (-not $Force) -and (-not $PSCmdlet.ShouldContinue($promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }

    #verify if there is another job already running
    if(-not (Test-ScheduleProvisioningJob ($rdvmColl)))
    {
        return
    }

    #determine storage
    $centralStoragePath = [string]::Empty
    if($rdvmColl.IsHA)
    {
        $storageType = [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::CentralSanStorage
        $centralStoragePath = $provProps.LocalVmLocation
    }
    elseif(-not [string]::IsNullOrEmpty($provProps.SMBLocation))
    {
        $storageType = [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::CentralSmbShareStorage
        $centralStoragePath = $provProps.SMBLocation
    }
    else
    {
        $storageType = [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::LocalStorage
    }
    $vmTemplateStoragePath = $provProps.LocalGoldVmLocation

    #generate Export Path
    $exportRoot = Get-ExportRoot -StorageType $storageType  -CentralStoragePath $centralStoragePath -VmTemplateStoragePath $vmTemplateStoragePath -ConnectionBroker $ConnectionBroker
    if( -not $exportRoot)
    {
        return
    }

    $exportPath = New-VMExportLocation -ExportRoot $exportRoot -Alias $rdvmColl.Alias -VirtualDesktopTemplateHostServer $VirtualDesktopTemplateHostServer
    if(-not $exportPath)
    {
        return
    }

    if ($PSBoundParameters["VirtualDesktopPasswordAge"])
    {
        #set the value
        if (-not (Set-VirtualDesktopHostSetting -CollectionName $CollectionName -ConnectionBroker $ConnectionBroker `
                        -Name "VirtualDesktopPasswordAge" -Value $VirtualDesktopPasswordAge))
        {
            return
        }
    }

    #Set Export info so that Get-Status could show export in progress
    try
    {
        $rdvmColl.SetStringProperty("ExportInProgress",$VirtualDesktopTemplateName)|Out-Null
    }
    catch
    {
        Write-Verbose ("Failed to set export in progress")
        #ignore error
    }

    #export the master VM
    try
    {
        $workflowSession = New-PSSession -ConfigurationName Microsoft.Windows.ServerManagerWorkflows -EnableNetworkAccess

        $masterVmDetails = Invoke-Command -Session $workflowSession -ArgumentList @($exportPath, $VirtualDesktopTemplateHostServer, $VirtualDesktopTemplateName) `
                            {
                                param($exportPath, $VirtualDesktopTemplateHostServer, $VirtualDesktopTemplateName)
                                RDManagement\Export-MasterVM -ExportLocation $exportPath -VMHostName $VirtualDesktopTemplateHostServer `
                                    -VMName $VirtualDesktopTemplateName -Override $false -SysPrepGeneralize $false
                            }
        # TODO: error handling
    }
    catch
    {
        Write-Error ($_.Exception.Message)
    }
    finally
    {
        if($workflowSession)
        {
            Remove-PSSession -Session $workflowSession
        }
    }
    
    if(-not $masterVmDetails)
    {
        Write-Error (Get-ResourceString ErrorExportFailed)
        try
        {
            $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
        }
        catch
        {
            Write-Verbose ("Failed to clear export in progress")
            #ignore error
        }
        return
    }

    #save Master VM details
    $collProps.ReferenceVirtualDesktopName = $VirtualDesktopTemplateName
    $collProps.ReferenceVirtualDesktopHost = $VirtualDesktopTemplateHostServer
    $collProps.ReferenceVirtualDesktopOsversion = $masterVmDetails.OSVersion
    $collProps.ReferenceVirtualDesktopArchitecture = $masterVMDetails.ProcessorArchitecture
    $collProps.ReferenceVirtualDesktopGuid = $masterVMDetails.VirtualMachineGuid
    $collProps.ReferenceVirtualDesktopRamsizeMB = $masterVMDetails.Memory
    $collProps.ReferenceVirtualDesktopRemoteFX = $masterVMDetails.RemoteFX

    $adapterNames = New-Object System.Collections.Generic.List[string]
    foreach($adapter in $masterVmDetails.VirtualSwitch)
    {
        $adapterNames.Add($adapter)
    }
    $collProps.ReferenceVirtualDesktopAdapters = $adapterNames
    
    try
    {
        $putRes = $collProps.Put()
    }
    catch
    {
        Write-Error ($_.Exception.Message)
    }
    if(-not $putRes)
    {
        Write-Error(Get-ResourceString FailedToSaveVmCollProp)
        try
        {
            $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
        }
        catch
        {
            Write-Verbose ("Failed to clear export in progress")
            #ignore error
        }
        return
    }

    $vhdType = "DiffDisk"
    # generate provisioning XML
    $provXml = Get-ProvisioningXml -CollectionAlias $rdvmColl.Alias -Job "Patch" -VhdType $vhdType -BaseVmLocation $exportPath `
                    -SmbShare $provProps.SMBLocation -LocalVmCreationPath $provProps.LocalVmLocation -LocalGoldCachePath $provProps.LocalGoldVmLocation `
                    -RunVmFromSmb $provProps.RunFromSMB -EnableVmStreaming $provProps.SMBStreaming `
                    -Prefix $provProps.NamePrefix -StartIndex $provProps.NameStartIndex -Domain $provProps.VmDomain -OU $provProps.VmDomain -HostList $VmDistribution `
                    -HighlyAvailable $rdvmColl.IsHA
    if(-not $provXml)
    {
        Write-Error(Get-ResourceString FailedToSaveVmCollProp)
        try
        {
            $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
        }
        catch
        {
            Write-Verbose ("Failed to clear export in progress")
            #ignore error
        }
        return
    }

    #update Rollback
    try
    {
        if($DisableVirtualDesktopRollback)
        {
            $rdvmColl.RollbackEnabled = $false
        }
        else
        {
            $rdvmColl.RollbackEnabled = $true
        }
        $putRes = $rdvmColl.Put();
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if( -not $putRes )
    {
        Write-Error (Get-ResourceString ModifyRollbackEnableFailed $errMsg)
        #non-fatal error, very unlikey, do not return
    }

    #Call Schedule Patch
    try
    {
        #get rdvmColl again
        $coll = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"

        $out = $coll.SchedulePatch([System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($StartTime.ToUniversalTime()), `
             [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($ForceLogoffTime.ToUniversalTime()), $provXml)
    }
    catch
    {
        Write-Error ($_.Exception.Message)
    }
    try
    {
        $coll.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
    }
    catch
    {
        Write-Verbose ("Failed to clear export in progress")
        #ignore error
    }

    if( (-not $out) -or (0 -ne $out.ReturnValue) )
    {
        Write-Error(Get-ResourceString FailedToScheduleVmCollRecreate)
        return
    }

    Write-Verbose (Get-ResourceString ScheduledVmCollRecreate )
}


# .ExternalHelp RemoteDesktop.psm1-help.xml
function Get-VirtualDesktop {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254095")]
[OutputType("Microsoft.RemoteDesktopServices.Management.RDVirtualDesktop[]")]
param(
    [Parameter(Mandatory=$false, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string[]]
    $CollectionName,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    $collAliasToNameMap=@{}

    if($CollectionName -eq $null)
    {
        $CollectionName="*"
    }

    # Get all the collections and prepare Name TO Alias map
    try
    {
        $rdvmCollections = Get-WmiObject -Class Win32_RDMSVirtualdesktopCollection -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
            -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop
    }
    catch
    {
        Write-Error ($_.Exception.Message)
        return
    }
    
    foreach($Collection in $CollectionName)
    {
        $WildCard = New-Object System.Management.Automation.WildcardPattern -ArgumentList $Collection, IgnoreCase
        
        # Virtual Desktops that are not part of any collection will also be included if $Collection matches or equal to an empty string
        if($WildCard.IsMatch(""))
        {
            if(-not $collAliasToNameMap.ContainsKey(""))
            {
                $collAliasToNameMap.Add("", "")
            }
        }

        $WildCardMatches = ($rdvmCollections | Where-Object { $WildCard.IsMatch($_.Name) })

        if($null -ne $WildCardMatches)
        {
            foreach($match in $WildCardMatches)
            {
                if(-not $collAliasToNameMap.ContainsKey($match.Alias))
                {
                    $collAliasToNameMap.Add($match.Alias, $match.Name)
                }
            }
        }
        else
        {
            if(-not [System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($Collection))
            {
                if(-not [String]::IsNullOrEmpty($Collection))
                {
                    Write-Error (Get-ResourceString SpecifiedVmCollNotFound $Collection)
                }
            }
            else
            {
                Write-Verbose (Get-ResourceString WrnWildcardNoMatches $Collection)
            }
        }
    }

    $wmiQuery = "Select * from Win32_RDMSVirtualDesktop"
    try
    {
        $VirtualDesktops = Get-WmiObject -Namespace "root\cimv2\rdms" -Query $wmiQuery -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
            | Where-Object { $collAliasToNameMap.ContainsKey($_.CollectionAlias) } `
            | ForEach-Object {
                            New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktop `
                                -ArgumentList $_.Name, $collAliasToNameMap[$_.CollectionAlias], $_.HostName,  $_.VMState, $_.Status
                         } `
            | Sort CollectionName,virtualDesktopName

        if($VirtualDesktops.Count -ne 0)
        {
            $VirtualDesktops
        }
        else
        {
            Write-Verbose (Get-ResourceString NoVirtualDesktopsFound)
        }
    }
    catch
    {
        Write-Error (Get-ResourceString GetVirtDesktopFailed $_.Exception.Message)
    }
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function Remove-VirtualDesktopFromCollection {
[CmdletBinding(SupportsShouldProcess=$true,
HelpURI="http://go.microsoft.com/fwlink/?LinkId=254096")]
param(
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true)]
    [string[]]
    $VirtualDesktopName,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory=$false)]
    [switch]
    $Force
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #validate each of the VM specified exists and is member of specified collection
    if( -not (Test-CollectionMemberVmList -CollectionAlias $rdvmColl.Alias -VirtualDesktopList $VirtualDesktopName -ConnectionBroker $ConnectionBroker))
    {
        return
    }

    $promptMsg = (Get-ResourceString RemoveVmsFromCollMsg)

    if( -not ($PSCmdlet.ShouldProcess( $promptMsg, $promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }

    if( (-not $Force) -and (-not $PSCmdlet.ShouldContinue($promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }

    #remove virtual desktop from collection
    foreach($vm in $VirtualDesktopName)
    {
        try
        {
            $result = $rdvmColl.RemoveVirtualDesktop($vm)
        }
        catch 
        {
            Write-Error ($_.Exception.Message)
        }

        if (($result) -and ($result.ReturnValue -eq 0))
        {
            Write-Verbose (Get-ResourceString VirtualDesktopRemoved $vm, $CollectionName)
        }
        else
        {
            Write-Error (Get-ResourceString FailedToRemoveVirtualDesktop $vm, $CollectionName)
        }
    }

    #for managed collections, clean up the vm by running provisioning job
    if( $rdvmColl.IsManaged )
    {
        #verify if there is another job already running
        if(-not (Test-ScheduleProvisioningJob ($rdvmColl)))
        {
            return
        }

        $provXml = Get-ProvXmlForExistingCollection -RdvmColl $rdvmColl -Type $rdvmColl.Type -JobType "Delete" -VmList $VirtualDesktopName `
                        -ConnectionBroker $ConnectionBroker
        if(-not $provXml)
        {
            return
        }

        #start provisioning job
        try
        {
            $workflowSession = New-PSSession -ConfigurationName Microsoft.Windows.ServerManagerWorkflows -EnableNetworkAccess

            Invoke-Command -Session $workflowSession -ArgumentList @($rdvmColl.Alias, $provXml, $ConnectionBroker) `
            {
                param($alias, $provXml, $ConnectionBroker)
                RDManagement\Start-RDVMRollout -Alias $alias -ProvisioningXml $provXml -ManagementServer $ConnectionBroker
            } | Out-Null
            # TODO: error handling
        }
        catch 
        {
            Write-Error (Get-ResourceString DeleteVirtualDesktopFailed)
        }
        finally
        {
            if($workflowSession)
            {
                Remove-PSSession -Session $workflowSession
            }
        }
    }
}

#Gets current provisioning job state, also updates the latest job state by retrieving report once

# .ExternalHelp RemoteDesktop.psm1-help.xml
function 
Get-VirtualDesktopCollectionJobStatus{
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254097")]
[OutputType("Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopCollectionJobStatus")]
param(
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                        -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #verify it is managed
    if(-not ($rdvmColl.IsManaged))
    {
        Write-Error (Get-ResourceString FailedToGetStatusNotManaged)
        return
    }

    try
    {
        $res = $rdvmColl.GetInt32Property("CollectionState")
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }
    if(-not $res)
    {
        Write-Error (Get-ResourceString FailedToGetCollectionState $errMsg)
        return
    }
    if( 0 -ne $res.ReturnValue )
    {
        Write-Error (Get-ResourceString FailedToGetCollectionState $res.ReturnValue)
        return        
    }
    $status = $res.Value

    $res = $null

    #see if collection state is exportInProgress.
    if(Test-ExportInProgress($rdvmColl))
    {
        try
        {
            $getStrPropRes = Invoke-WmiMethod -Name GetStringProperty -ArgumentList "ExportInProgress" -InputObject $rdvmColl `
                        -ErrorAction Stop
            $exportInProgress = $getStrPropRes.Value 
        }
        catch
        {
            Write-Verbose ("Failed to get exportInProgress property")
        }
        $jobStatus = New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopCollectionExportStatus `
                -ArgumentList $CollectionName, $exportInProgress
    }
    # else, for patch schedule\cancelled status, retrieve patch schedule details
    elseif (($status -eq  [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UPDATE_SCHEDULED) -or
        ($status -eq  [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UPDATE_CANCELLED))
    {
        try
        {
            $patchProp = $rdvmcoll.GetPatchProperties();
        }
        catch 
        {
            Write-Error $_.Exception.Message
        }
        if(-not $patchProp)
        {
            Write-Error (Get-ResourceString FailedToGetRecreateProps)
            return $false
        }

        $startTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($patchProp.StartTime)
        $logoffTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($patchProp.ForceLogOffTime)
        $jobStatus = New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopCollectionUpdateJobStatus `
        -ArgumentList $CollectionName, $status, $startTime, $logoffTime
    }
    #for all other status , parse the report
    else
    {
        #get JOB ID, refresh JOB report
        try
        {
            $res = $rdvmColl.GetStringProperty("ProvisioningJobId")
            $jobGuid = $res.Value

            $getReportRes = Invoke-WmiMethod -Class Win32_RDMSVirtualDesktopCollection -Name ProvisioningJobGetReport -ComputerName $ConnectionBroker `
                        -Namespace 'root\cimv2\rdms' -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                        -ArgumentList ($jobGuid) 
        }
        catch
        {
            $errMsg = $_.Exception.Message
        }
        if(-not $res)
        {
            Write-Error (Get-ResourceString FailedToGetCollectionJobId $errMsg)
            return
        }
        if( 0 -ne $res.ReturnValue )
        {
            Write-Error (Get-ResourceString FailedToGetCollectionJobId $res.ReturnValue)
            return        
        }
        if((-not $getReportRes) -or (0 -ne $getReportRes.returnValue))
        {
            Write-Error (Get-ResourceString FailedToGetJobReport)
            if($errMsg)
            {
                Write-Error $errMsg
                return
            }
            if(0 -ne $getReportRes.returnValue)
            {
                Write-Error $getReportRes.returnValue
            }            
            return
        }
        #retrieve collection state again
        try
        {
            $res = $rdvmColl.GetInt32Property("CollectionState")
        }
        catch
        {
            $errMsg = $_.Exception.Message
        }
        if(-not $res)
        {
            Write-Error (Get-ResourceString FailedToGetCollectionState $errMsg)
            return
        }
        if( 0 -ne $res.ReturnValue )
        {
            Write-Error (Get-ResourceString FailedToGetCollectionState $res.ReturnValue)
            return        
        }
        $status = $res.Value


        try
        {
            $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
        }
        catch
        {
            Write-Verbose ("Failed to clear export in progress")
            #ignore error
        }

        try
        {
            $jobReport = New-Object Microsoft.RemoteDesktopServices.Management.Cmdlets.RolloutJobReport

            # parse job report
            $jobReport.ParseReportAndUpdateVMStatus($getReportRes.JobReportXml)

            # create Job status object
            $jobStatus = New-Object  Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopCollectionJobStatus -ArgumentList `
                $CollectionName, $status, $jobReport.JobStartTime, $jobReport.JobLastModifiedTime, $jobReport.TotalRequested, `
                ($jobReport.PercentComplete.ToString() + '%'), $jobReport.TotalFailed, $jobReport.VmStatusMap.ToArray()
        }
        catch
        {
            Write-Error ((Get-ResourceString FailedToGetJobReport) + " " + $_.Exception.Message)
            return
        }
    }
    
    return ($jobStatus)
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function
Stop-VirtualDesktopCollectionJob{
[CmdletBinding(SupportsShouldProcess=$true,
HelpURI="http://go.microsoft.com/fwlink/?LinkId=254098")]
param(
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker, 

    [Parameter(Mandatory=$false)]
    [switch]
    $Force

)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                        -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #verify it is managed and shared
    if(-not ($rdvmColl.IsManaged))
    {
        Write-Error (Get-ResourceString FailedToGetStatusNotManaged)
        return
    }

    #confirm
    $promptMsg = (Get-ResourceString StopProvJobMsg)
    if( -not ($PSCmdlet.ShouldProcess( $promptMsg, $promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }
    if( (-not $Force) -and (-not $PSCmdlet.ShouldContinue($promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }

    $jobStatus = Get-RDVirtualDesktopCollectionJobStatus $CollectionName -ConnectionBroker $ConnectionBroker
    if($null -eq $jobStatus)
    {
        return
    }
    $status = $jobStatus.Status

    if( ($status -eq  [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::CREATE_VIRTUAL_DESKTOP_INPROGRESS) -or 
        ($status -eq  [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::ADD_VIRTUAL_DESKTOP_INPROGRESS) -or 
        ($status -eq  [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UPDATE_VIRTUAL_DESKTOP_INPROGRESS) )
    {
        #get job id and cancel it
        try
        {
            $res = $rdvmColl.GetStringProperty("ProvisioningJobId")
            $jobGuid = $res.Value
            $cancelResult =  Invoke-WmiMethod -Class Win32_RDMSVirtualDesktopCollection -Name ProvisioningJobCancel -ComputerName $ConnectionBroker `
                     -Namespace 'root\cimv2\rdms' -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                     -ArgumentList ($jobGuid) 
        }
        catch
        {
            $errMsg = $_.Exception.Message            
        }
        if(-not $res)
        {
            Write-Error (Get-ResourceString FailedToGetCollectionJobId $errMsg)
            return
        }
        if( 0 -ne $res.ReturnValue )
        {
            Write-Error (Get-ResourceString FailedToGetCollectionJobId $res.ReturnValue)
            return        
        }
        if(-not $cancelResult)
        {
            Write-Error (Get-ResourceString FailedToCancelCollJob $errMsg)
            return
        }
        if( 0 -ne $cancelResult.ReturnValue )
        {
            Write-Error (Get-ResourceString FailedToCancelCollJob $res.ReturnValue)
            return        
        }
        #refresh state
        $jobStatus = Get-RDVirtualDesktopCollectionJobStatus $CollectionName -ConnectionBroker $ConnectionBroker
    }
    elseif($status -eq  [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UPDATE_SCHEDULED)
    {
        #cancel scheduled patch
        try
        {
            $cancelResult = $rdvmColl.CancelPatch()
        }
        catch
        {
            $errMsg = $_.Exception.Message            
        }
        if(-not $cancelResult)
        {
            Write-Error (Get-ResourceString FailedToCancelCollJob $errMsg)
            return
        }
        if( 0 -ne $cancelResult.ReturnValue )
        {
            Write-Error (Get-ResourceString FailedToCancelCollJob $res.ReturnValue)
            return    
        }

        #refresh state
        $jobStatus = Get-RDVirtualDesktopCollectionJobStatus $CollectionName -ConnectionBroker $ConnectionBroker
    }
    else
    {
        Write-Error (Get-ResourceString ErrCollectionJobCancelInvalidState $status.ToString())
        return
    }
    return $jobStatus
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function Set-PersonalVirtualDesktopAssignment{
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254099")]
param(
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [string]
    $User,
    
    [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [string]
    $VirtualDesktopName,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker 
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #verify it is personal
    if( 1 -eq $rdvmColl.Type)
    {
        Write-Error (Get-ResourceString PDAssignErrorNotPDPool)
        return
    }

    #verify user name and get domain and user name
    $sid = ConvertTo-Sid($user)
    if(-not $sid)
    {
        Write-Error (Get-ResourceString InvalidUserName)
        return
    }
    $separators=[char[]]('\')
    $userParts = $User.Split($separators)
    $userDomain = $userParts[0]
    $userName = $userParts[1]

    $newPD = $null

    #a virtual desktop name is specified, verify it
    try
    {
        $newPD= Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                -Filter "Name='$VirtualDesktopName'"
    }
    catch 
    {
        Write-Error ($_.Exception.Message)
    }

    if($null -eq $newPD)
    {
        Write-Error (Get-ResourceString FailedToFindVirtualDesktop $VirtualDesktopName)
        return
    }

    if([string]::Compare($rdvmColl.Alias, $newPD.CollectionAlias, $true))
    {
        Write-Error(Get-ResourceString VirtDesktopNotAMemeberOfSpecifiedPool $VirtualDesktopName)
        return
    }

    if(-not ([System.String]::IsNullOrEmpty($newPD.UserDomain) -or [System.String]::IsNullOrEmpty($newPD.UserName)))
    { 
        Write-Error (Get-ResourceString UserAlreadyAssigned $newPD.UserDomain, $newPD.UserName)
        return
    }

    if( [Microsoft.RemoteDesktopServices.Management.VirtualDesktopProvisioningStatus]::FAILED.value__ -eq $newPD.Status )
    {
        Write-Error (Get-ResourceString FailedToFindVirtualDesktop $VirtualDesktopName)
        return;
    }


    #call static method "Win32_RDMSVirtualDesktop::GetVirtualDesktopAssignedToUser"
    try
    {
        $out = Invoke-WmiMethod -Class Win32_RDMSVirtualDesktop -Name GetVirtualDesktopAssignedToUser -ArgumentList ($rdvmColl.Alias,$userDomain,$userName) `
             -ComputerName $ConnectionBroker -Namespace "root\cimv2\rdms" `
             -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop 
    }
    catch
    {
        #ignore error
    }


    $alreadyAssigned = ( ($null -ne $out) -and (0 -eq $out.ReturnValue) -and (-not [string]::IsNullOrEmpty($out.VMName)))

    #if already assigned a desktop, get the desktop instance and clear assignment
    if( $alreadyAssigned )
    {
        try
        {
            $vmName = $out.VMName
	    $remRes = Invoke-WmiMethod -Class Win32_RDMSVirtualDesktop -Name RemoveUserAssignment -ArgumentList ($vmName) `
             -ComputerName $ConnectionBroker -Namespace "root\cimv2\rdms" `
             -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop
        }
        catch
        {
            Write-Error ($_.Exception.Message)
        }
        if( (-not $remRes) -or (0 -ne $remRes.ReturnValue))
        {
            Write-Error (Get-ResourceString FailedToRemovePDAssignment)
            return
        }
    }

    #get the desktop instance for Admin specified desktop
    # and call SetUserAssignment 
    try
    {
        $setRes = $newPD.SetUserAssignment($userName, $userDomain)
    }
    catch 
    {
        Write-Error ($_.Exception.Message)
    }

    if((-not $setRes) -or (0 -ne $setRes.ReturnValue))
    {
        Write-Error (Get-ResourceString FailedToSetPDAssignment)
    }
    else
    {
        Write-Verbose (Get-ResourceString SetPDAssignSuccess $User,$VirtualDesktopName,$CollectionName )
    }
}


# .ExternalHelp RemoteDesktop.psm1-help.xml
function Remove-PersonalVirtualDesktopAssignment{
[CmdletBinding(DefaultParameterSetName="RemoveByUser", SupportsShouldProcess=$true,
HelpURI="http://go.microsoft.com/fwlink/?LinkId=254100")]
param(
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true, ParameterSetName="RemoveByUser")]
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true, ParameterSetName="RemoveByDesktop")]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true, Position=1, ValueFromPipelineByPropertyName=$true, ParameterSetName="RemoveByUser")]
    [string]
    $User,
    
    [Parameter(Mandatory=$true, Position=1, ValueFromPipelineByPropertyName=$true, ParameterSetName="RemoveByDesktop")]
    [string]
    $VirtualDesktopName,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker, 

    [Parameter(Mandatory=$false)]
    [switch]
    $Force
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #verify it is personal
    if( 1 -eq $rdvmColl.Type)
    {
        Write-Error (Get-ResourceString PDAssignErrorNotPDPool)
        return
    }

    if( $PSBoundParameters["User"] )
    {
        #verify user name and get domain and user name
        $sid = ConvertTo-Sid($user)
        if(-not $sid)
        {
            Write-Error (Get-ResourceString InvalidUserName)
            return
        }
        $separators=[char[]]('\')
        $userParts = $User.Split($separators)
        $userDomain = $userParts[0]
        $userName = $userParts[1]

        #call static method "Win32_RDMSVirtualDesktop::GetVirtualDesktopAssignedToUser"
        try
        {
            $out = Invoke-WmiMethod -Class Win32_RDMSVirtualDesktop -Name GetVirtualDesktopAssignedToUser -ArgumentList ($rdvmColl.Alias,$userDomain,$userName) `
                    -ComputerName $ConnectionBroker -Namespace "root\cimv2\rdms" `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop
        }
        catch 
        {
            Write-Error ($_.Exception.Message)
        }

        $alreadyAssigned = (($null -ne $out) -and (0 -eq $out.ReturnValue) -and (-not [string]::IsNullOrEmpty($out.VMName)))

        #if not already assigned a desktop, return
        if( -not $alreadyAssigned )
        {
            Write-Error (Get-ResourceString PDAssignErrorNoCurrentAssignment)
            return
        }

        $vmName = $out.VMName
    }
    elseif( $PSBoundParameters["VirtualDesktopName"] )
    {
        try
        {
            $currentPD = Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$VirtualDesktopName'"
        }
        catch 
        {
            Write-Error ($_.Exception.Message)
        }

        if($null -eq $currentPD)
        {
            Write-Error (Get-ResourceString FailedToFindVirtualDesktop $VirtualDesktopName)
            return
        }
        if( [string]::Compare( $currentPD.CollectionAlias, $rdvmColl.Alias, $true))
        {
            Write-Error (Get-ResourceString VirtDesktopNotAMemeberOfSpecifiedPool $VirtualDesktopName)
            return
        }
        if( -not ($currentPD.UserName) -or -not($currentPD.UserDomain) )
        {
            Write-Error (Get-ResourceString SpecifiedPDNotAssigned)
            return
        }
        $vmName = $currentPD.Name
        $userDomain = $currentPD.UserDomain
        $userName = $currentPD.UserName

    }
    else
    {
        Write-Verbose ('Unexpected')
        return
    }

    #confirm
    $promptMsg = (Get-ResourceString RemovePDAssignmentMsg ($userDomain + '\' + $userName), $vmName)
    if( -not ($PSCmdlet.ShouldProcess( $promptMsg, $promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }
    if( (-not $Force) -and (-not $PSCmdlet.ShouldContinue($promptMsg, (Get-ResourceString PromptCaption))))
    {
        return
    }

    try
    {
	$remRes = Invoke-WmiMethod -Class Win32_RDMSVirtualDesktop -Name RemoveUserAssignment -ArgumentList ($vmName) `
             -ComputerName $ConnectionBroker -Namespace "root\cimv2\rdms" `
             -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop
    }
    catch
    {
        Write-Error ($_.Exception.Message)
    }
    if((-not $remRes) -or (0 -ne $remRes.ReturnValue))
    {
        Write-Error (Get-ResourceString FailedToRemovePDAssignment)
        return
    }
    Write-Verbose (Get-ResourceString RemovePDAssignSuccess)
}


# .ExternalHelp RemoteDesktop.psm1-help.xml
function
Get-PersonalVirtualDesktopAssignment{
[CmdletBinding(DefaultParameterSetName="GetByCollection",
HelpURI="http://go.microsoft.com/fwlink/?LinkId=254101")]
[OutputType("Microsoft.RemoteDesktopServices.Management.RDPersonalVirtualDesktopAssignment[]")]
param(
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true, ParameterSetName="GetByCollection")]
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true, ParameterSetName="GetByUser")]
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true, ParameterSetName="GetByDesktop")]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true, ParameterSetName="GetByUser")]
    [string]
    $User,
    
    [Parameter(Mandatory=$true, ParameterSetName="GetByDesktop")]
    [string]
    $VirtualDesktopName,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)

    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #verify it is personal
    if( 1 -eq $rdvmColl.Type)
    {
        Write-Error (Get-ResourceString PDAssignErrorNotPDPool)
        return
    }

    if( $PSBoundParameters["User"] )
    {
        Write-Verbose ( 'Getting PD assignmet by user name: ' + $User )

        #verify user name and get domain and user name
        $sid = ConvertTo-Sid($user)
        if(-not $sid)
        {
            Write-Error (Get-ResourceString InvalidUserName)
            return
        }
        $separators=[char[]]('\')
        $userParts = $User.Split($separators)
        $userDomain = $userParts[0]
        $userName = $userParts[1]

        #call static method "Win32_RDMSVirtualDesktop::GetVirtualDesktopAssignedToUser"
        try
        {
            $out = Invoke-WmiMethod -Class Win32_RDMSVirtualDesktop -Name GetVirtualDesktopAssignedToUser -ArgumentList ($rdvmColl.Alias,$userDomain,$userName) `
                 -ComputerName $ConnectionBroker -Namespace "root\cimv2\rdms" `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop
        }
        catch 
        {
            Write-Error ($_.Exception.Message)
        }
        $pdAssigned = (($null -ne $out) -and (0 -eq $out.ReturnValue) -and (-not [string]::IsNullOrEmpty($out.VMName)))

        #if not already assigned a desktop, return
        if( -not $pdAssigned )
        {
            Write-Verbose (Get-ResourceString PDAssignErrorNoCurrentAssignment)
            return
        }

        return (New-Object Microsoft.RemoteDesktopServices.Management.RDPersonalVirtualDesktopAssignment `
                                        -ArgumentList $out.VMName, $User)
    }
    elseif ( $PSBoundParameters["VirtualDesktopName"] )
    {
        Write-Verbose ('Getting PD assignmet by Virtual Desktop name: ' + $VirtualDesktopName )
        try
        {
            $pd = Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$VirtualDesktopName'"
        }
        catch 
        {
            Write-Error ($_.Exception.Message)
        }

        if($null -eq $pd)
        {
            Write-Error (Get-ResourceString FailedToFindVirtualDesktop $VirtualDesktopName)
            return
        }
        if( [string]::Compare( $pd.CollectionAlias, $rdvmColl.Alias, $true))
        {
            Write-Error (Get-ResourceString VirtDesktopNotAMemeberOfSpecifiedPool $VirtualDesktopName)
            return
        }
        if( -not ($pd.UserName) -or -not($pd.UserDomain) )
        {
            Write-Verbose (Get-ResourceString SpecifiedPDNotAssigned)
            return
        }

        $userAccount= $pd.UserDomain + '\' + $pd.UserName

        return (New-Object Microsoft.RemoteDesktopServices.Management.RDPersonalVirtualDesktopAssignment `
                                        -ArgumentList $pd.Name, $userAccount)
    }
    else
    {
        Write-Verbose ('Getting all the PD assignments in the collection: ' + $CollectionName )
        $alias=$rdvmColl.Alias
        try
        {
            $assignedPDs = Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "CollectionAlias='$alias' and UserDomain != null and UserName != null"
        }
        catch
        {
            Write-Error ($_.Exception)
        }
        if(-not $assignedPDs)
        {
            Write-Verbose (Get-ResourceString NoAssignedPDFoundInCollection)
            return
        }
        foreach($pd in $assignedPDs)
        {
            $userAccount= $pd.UserDomain + '\' + $pd.UserName
            New-Object Microsoft.RemoteDesktopServices.Management.RDPersonalVirtualDesktopAssignment `
                                        -ArgumentList $pd.Name, $userAccount
        }
    }
}


# .ExternalHelp RemoteDesktop.psm1-help.xml
function
Export-PersonalVirtualDesktopAssignment {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254102")]
param(
    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true)]
    [string]
    $Path,
    
    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)

    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #verify it is personal
    if( 1 -eq $rdvmColl.Type)
    {
        Write-Error (Get-ResourceString PDAssignErrorNotPDPool)
        return
    }

    #make sure file does not exist
    if((Test-FilePath -Path $Path -ErrorAction SilentlyContinue))
    {
        Write-Error ('File already exists')
        return
    }

    #make sure the folder exists
    if(-not (Test-FilePath -Path $Path -Parent))
    {
        Write-Error (Get-ResourceString InvalidPath $Path)
        return
    }

    #get all the assignments in the collection and export to csv file
    Get-RDPersonalVirtualDesktopAssignment $CollectionName | Export-Csv $Path
    return
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function
Import-PersonalVirtualDesktopAssignment {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254103")]
param(

    [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true)]
    [string]
    $Path,
    
    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)

    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if (-not (Test-RemoteDesktopDeployment -RDManagementServer $ConnectionBroker))
    {
        Write-Error (Get-ResourceString DeploymentDoesNotExist $ConnectionBroker)
        return
    }

    #verify that collection exists
    try
    {
        $rdvmColl = Get-WmiObject Win32_RDMSVirtualDesktopCollection -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$CollectionName'"
    }
    catch
    {
        $errMsg = $_.Exception.Message
    }

    if($null -eq $rdvmColl)
    {
        Write-Error (Get-ResourceString SpecifiedVmCollNotFound $CollectionName)
        if($errMsg)
        {
            Write-Error $errMsg
        }
        return
    }

    #verify it is personal
    if( 1 -eq $rdvmColl.Type)
    {
        Write-Error (Get-ResourceString PDAssignErrorNotPDPool)
        return
    }

    #make sure file exists
    if(-not (Test-FilePath -Path $Path))
    {
        Write-Error (Get-ResourceString InvalidPath $Path)
        return
    }
    
    #import assignments to the collection
    import-csv $Path | `
    ForEach-Object{
        Remove-RDPersonalVirtualDesktopAssignment $CollectionName -VirtualDesktopName $_.VirtualDesktopName -force -ErrorAction SilentlyContinue
        Remove-RDPersonalVirtualDesktopAssignment $CollectionName -User $_.User -force -ErrorAction SilentlyContinue
        $_ | Set-RDPersonalVirtualDesktopAssignment $CollectionName 
    }
    #return the assignments
    Get-RDPersonalVirtualDesktopAssignment $CollectionName
}


# .ExternalHelp RemoteDesktop.psm1-help.xml
function Set-VirtualDesktopConcurrency {
[CmdletBinding(DefaultParameterSetName="Default",
HelpURI="http://go.microsoft.com/fwlink/?LinkId=254104")]
param(
    [Parameter(Mandatory=$true, ParameterSetName="Default", Position=0)]
    [ValidateRange(1,100)]
    [int]
    $ConcurrencyFactor,

    [Parameter(Mandatory=$false, ParameterSetName="Default", Position=1)]
    [string[]]
    $HostServer,

    [Parameter(Mandatory=$true, ParameterSetName="Allocation", Position=0)]
    [Hashtable]
    $Allocation,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory=$false)]
    [ValidateRange(1,100)]
    [int]
    $BatchSize = 10
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    switch ($PsCmdlet.ParameterSetName)
    {
        "Default"
        {
            if($null -eq $HostServer)
            {
                $HostServer = "*"
            }

            $HostList = (Get-ValidRdvhFromArray -ConfigurationValue $ConcurrencyFactor -HostServer $HostServer -ConnectionBroker $ConnectionBroker)
            foreach($Server in $HostServer)
            {
                if((-not [System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($Server)) -and (-not $HostList.ContainsKey($Server)))
                {
                    Write-Error (Get-ResourceString InvalidRdvhRoleNotFoundGeneric $Server)
                }
            }
        }
         
        "Allocation"
        {
            $HostList = (Get-ValidRdvhFromHashtable -Allocation $Allocation -ConnectionBroker $ConnectionBroker)
            foreach($Server in $Allocation.Keys)
            {
                if(-not $HostList.ContainsKey($Server))
                {
                    Write-Error (Get-ResourceString InvalidRdvhRoleNotFoundGeneric $Server)
                }
            }
        }
    }
    
    $JobList = @()
    $ErrInvalidConcurrencyRange = Get-RawResourceString ErrInvalidConcurrencyRange
    $ErrInvalidConcurrencyType = Get-RawResourceString ErrInvalidConcurrencyType
    $ErrSettingConcurrencyFactor = Get-RawResourceString ErrSettingConcurrencyFactor

    foreach($VMHAHost in $HostList.Keys)
    {
        Write-Verbose (Get-ResourceString SettingConcurrencyFactor $VMHAHost)
        $JobList += Start-Job  -scriptblock `
        {
            try
            {
                $RemoteRegistry=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $args[0])
                $VMHAParamKey=$RemoteRegistry.CreateSubKey("SYSTEM\\CurrentControlSet\\Services\\VmHostAgent\\Parameters")
                if($null -ne $args[4])
                {
                    if($args[4].GetType().FullName -eq "System.Int32")
                    {
                        if(($args[4] -ge 1) -and ($args[4] -le 100))
                        {
                            $VMHAParamKey.SetValue("Concurrency", $args[4])
                            $true
                        }
                        else
                        {
                            throw ($args[1] -f $args[4], $args[0])
                        }
                    }
                    else
                    {
                        throw ($args[2] -f $args[4].GetType().FullName, $args[0])
                    }
                }
                elseif($null -ne $VMHAParamKey.GetValue("Concurrency"))
                {
                    $VMHAParamKey.DeleteValue("Concurrency")
                    $false
                }
            }
            catch
            {
                throw ($args[3] -f $args[0], $_.Exception.Message)
            }
        } -ArgumentList $VMHAHost,$ErrInvalidConcurrencyRange,$ErrInvalidConcurrencyType,$ErrSettingConcurrencyFactor,$HostList[$VMHAHost] -Name $VMHAHost

        if($JobList.Length -ge $BatchSize)
        {
            if($JobList.Length -gt 1)
            {
                Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
            }
            else
            {
                Write-Verbose (Get-ResourceString WaitingForJobToComplete)
            }

            $WaitResult = Wait-Job -Job $JobList

            foreach($job in $JobList)
            {
                if($job.State -eq 'Failed')
                {
                    Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
                }
                else
                {
                    $retVal = Receive-Job $job
                    if($retVal)
                    {
                        Write-Verbose (Get-ResourceString SettingConcurrencyFactorSuccess $job.Name)
                    }
                    else
                    {
                        Write-Verbose (Get-ResourceString DeletingConcurrencyFactorSuccess $job.Name)
                    }
                }
            }
            
            $JobList = @()
        }
    }

    if($JobList.Length -gt 0)
    {
        if($JobList.Length -gt 1)
        {
            Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
        }
        else
        {
            Write-Verbose (Get-ResourceString WaitingForJobToComplete)
        }
        
        $WaitResult = Wait-Job -Job $JobList
    
        foreach($job in $JobList)
        {
            if($job.State -eq 'Failed')
            {
                Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
            }
            else
            {
                $retVal = Receive-Job $job
                if($retVal)
                {
                    Write-Verbose (Get-ResourceString SettingConcurrencyFactorSuccess $job.Name)
                }
                else
                {
                    Write-Verbose (Get-ResourceString DeletingConcurrencyFactorSuccess $job.Name)
                }
            }
        }
    }
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function Get-VirtualDesktopConcurrency {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254105")]
[OutputType("Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopConcurrency[]")]
param(
    [Parameter(Mandatory=$false, Position=0)]
    [string[]]
    $HostServer,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory=$false)]
    [ValidateRange(1,100)]
    [int]
    $BatchSize = 10
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    if($null -eq $HostServer)
    {
        $HostServer = "*"
    }

    $HostList = (Get-ValidRdvhFromArray -ConfigurationValue 0 -HostServer $HostServer -ConnectionBroker $ConnectionBroker)
    foreach($Server in $HostServer)
    {
        if((-not [System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($Server)) -and (-not $HostList.ContainsKey($Server)))
        {
            Write-Error (Get-ResourceString InvalidRdvhRoleNotFoundGeneric $Server)
        }
    }

    $JobList = @()
    $ValidResults = @()

    $ErrReadingConcurrencyFactor = Get-RawResourceString ErrReadingConcurrencyFactor 

    foreach($VMHAHost in $HostList.Keys)
    {
        $JobList += Start-Job  -scriptblock `
        {
            try
            {
                $RemoteRegistry=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $args[0])
                $VMHAParamKey=$RemoteRegistry.CreateSubKey("SYSTEM\\CurrentControlSet\\Services\\VmHostAgent\\Parameters")
                $retVal = $VMHAParamKey.GetValue("Concurrency")
                $retVal
            }
            catch
            {
                throw ($args[1] -f $args[0], $_.Exception.Message)
            }
        } -ArgumentList $VMHAHost,$ErrReadingConcurrencyFactor -Name $VMHAHost


        if($JobList.Length -ge $BatchSize)
        {
            if($JobList.Length -gt 1)
            {
                Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
            }
            else
            {
                Write-Verbose (Get-ResourceString WaitingForJobToComplete)
            }

            $WaitResult = Wait-Job -Job $JobList

            foreach($job in $JobList)
            {
                if($job.State -eq 'Failed')
                {
                    Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
                }
                else
                {
                    $retVal = Receive-Job $job
                    $ValidResults += New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopConcurrency -ArgumentList $job.Name,$retVal
                }
            }
            
            $JobList = @()
        }
    }

    if($JobList.Length -gt 0)
    {
        if($JobList.Length -gt 1)
        {
            Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
        }
        else
        {
            Write-Verbose (Get-ResourceString WaitingForJobToComplete)
        }
        
        $WaitResult = Wait-Job -Job $JobList

        foreach($job in $JobList)
        {
            if($job.State -eq 'Failed')
            {
                Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
            }
            else
            {
                $retVal = Receive-Job $job
                $ValidResults += New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopConcurrency -ArgumentList $job.Name,$retVal
            }
        }
    }

    $ValidResults
}


# .ExternalHelp RemoteDesktop.psm1-help.xml
function Enable-VirtualDesktopADMachineAccountReuse {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254106")]
param(
    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    $brokerList = Get-RDServer -ConnectionBroker $ConnectionBroker -Role RDS-CONNECTION-BROKER

    try
    {
        foreach ($broker in $brokerList) {

            Write-Debug ("Broker = " + $broker.Server)

            $RemoteRegistry=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $broker.Server)
            $brokerKey=$RemoteRegistry.CreateSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Terminal Server\\Session Broker")
            $brokerKey.SetValue("ReuseVmAdAccount", 1, [Microsoft.Win32.RegistryValueKind]::DWord)
        }
    }
    catch
    {
        Write-Error (Get-ResourceString ErrSettingReuseVmAdAccount $broker.Server, $_.Exception.Message)
    }
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function Disable-VirtualDesktopADMachineAccountReuse {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254107")]
param(
    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    $brokerList = Get-RDServer -ConnectionBroker $ConnectionBroker -Role RDS-CONNECTION-BROKER

    try
    {
        foreach ($broker in $brokerList) {

            Write-Debug ("Broker = " + $broker.Server)

            $RemoteRegistry=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $broker.Server)
            $brokerKey=$RemoteRegistry.CreateSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Terminal Server\\Session Broker")
            if($null -ne $brokerKey.GetValue("ReuseVmAdAccount"))
            {
                $brokerKey.DeleteValue("ReuseVmAdAccount")
            }
        }
    }
    catch
    {
        Write-Error (Get-ResourceString ErrSettingReuseVmAdAccount $broker.Server, $_.Exception.Message)
    }
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function Test-VirtualDesktopADMachineAccountReuse {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254108")]
param(
    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ([string]::IsNullOrEmpty($ConnectionBroker))
    {
        Write-Error (Get-ResourceString InvalidFqdn $ConnectionBroker)
        return
    }

    try
    {
        $RemoteRegistry=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $ConnectionBroker)
        $brokerKey=$RemoteRegistry.CreateSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Terminal Server\\Session Broker")
        return ([bool]$brokerKey.GetValue("ReuseVmAdAccount"))
    }
    catch
    {
        Write-Error (Get-ResourceString ErrReadingReuseVmAdAccount $ConnectionBroker, $_.Exception.Message)
    }
}

##############################################################
######################Helper functions #######################
##############################################################

function Get-ProvXmlForExistingCollection
{
param(
    [Parameter(Mandatory=$true, Position=0)]
    [Wmi]
    $RdvmColl,

    [Parameter(Mandatory=$true)]
    [int]
    $Type,

    [Parameter(Mandatory=$true)]
    [string]
    $JobType,

    [Parameter(Mandatory=$false, Position=3)]
    [ref]
    $MasterVmLocation,

    [Parameter(Mandatory=$false)]
    [string]
    $CustomMasterVmLocation,

    [Parameter(Mandatory=$false)]
    [hashtable]
    $VmDistribution,

    [Parameter(Mandatory=$false)]
    [string[]]
    $VmList,

    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory = $false)]
    [bool]
    $CheckStoragePerms = $false
)
    $Alias=$rdvmColl.Alias

    # get provisioning properties
    try
    {
        $collProps = Get-WmiObject Win32_RDMSCollectionProperties -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Alias='$Alias'"
    }
    catch
    {
        Write-Error ($_.Exception.Message)
    }
    if( -not $collProps )
    {
        Write-Error (Get-ResourceString VmCollectionPropNotFound)
        return $null
    }

    try
    {
        $provProps=$collProps.GetProvisioningProperties()
    }
    catch 
    {
        Write-Error ($_.Exception.Message)
    }
    if(-not $provProps)
    {
        Write-Error (Get-ResourceString VmCollectionProvPropNotFound)
        return $null
    }

    $vhdType = "DiffDisk"
    if(1 -ne $Type)
    {
        $vhdType = "Clone"
    }
    
    $baseVmLocation=""
    if([string]::IsNullOrEmpty($CustomMasterVmLocation))
    {
        if(($MasterVmLocation))
        {
            $MasterVmLocation.Value = $provProps.MasterVmLocation
        }
        $baseVmLocation = $provProps.MasterVmLocation
    }
    else
    {
        $baseVmLocation = $CustomMasterVmLocation;
    }

    # Make sure there is permission for each host on the share
    if($CheckStoragePerms)
    {
        $centralStorage = ""
        $storageType = [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::LocalStorage
        if($rdvmColl.IsHA)
        {
            $storageType = [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::CentralSanStorage
            $centralStorage = $provProps.LocalVmLocation
        }
        elseif($provProps.RunFromSMB -and !$provProps.SMBStreaming)
        {
            $storageType = [Microsoft.RemoteDesktopServices.Management.VirtualDesktopStorageType]::CentralSmbShareStorage
            $centralStorage = $provProps.SMBLocation
        }

        if(-not (Set-StoragePermissions  -StorageType $storageType -CentralStoragePath $centralStorage `
                -LocalStoragePath $provProps.LocalVmLocation -VmTemplateStoragePath $provProps.LocalGoldVmLocation `
                -HostList $VmDistribution.Keys ))
        {
            return $null
        }

        #uvhd share perms, if UVHD is enabled
        $farmSettings = [xml]$rdvmColl.VmFarmSettings
        $node = $farmSettings.SelectSingleNode("/Farm/Metadata[@Name='UvhdShareUrl']")
        if($node)
        {
            $uvhdShare = $node.Attributes["Value"].Value
            if(-not (Set-ShareProvisioningPermissions -Path $uvhdShare -HostList $VmDistribution.Keys))
            {
                return $null
            }
        }
    }

    # generate provisioning XML
    $provXml = Get-ProvisioningXml -CollectionAlias $Alias -Job $JobType -VhdType $vhdType -BaseVmLocation $baseVmLocation `
                    -SmbShare $provProps.SMBLocation -LocalVmCreationPath $provProps.LocalVmLocation -LocalGoldCachePath $provProps.LocalGoldVmLocation `
                    -RunVmFromSmb $provProps.RunFromSMB -EnableVmStreaming $provProps.SMBStreaming `
                    -Prefix $provProps.NamePrefix -StartIndex $provProps.NameStartIndex -Domain $provProps.VmDomain -OU $provProps.VmDomain -HostList $VmDistribution `
                    -VmList $VmList -HighlyAvailable $rdvmColl.IsHA

    if(-not $provXml)
    {
        Write-Error (Get-ResourceString FailedToGetProvXml)
    }

    return $provXml
}

function Test-UnmanagedVmList{

Param(
    [Parameter(Mandatory=$true, Position=0)]
    [string[]]
    $VirtualDesktopList,

    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory=$false)]
    [System.Collections.Generic.List[string]]
    $UnmgdVmHostList
)

    if(-not ($VirtualDesktopList.Count))
    {
        Write-Error (Get-ResourceString ErrorEmptyVmList)
        return $false
    }
#Verify Each VM

    foreach($vmName in $VirtualDesktopList)
    {
        #exists in current VM list
        try
        {
            $vmObj = Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                -Filter "Name='$vmName'"
        }
        catch 
        {
            Write-Error ($_.Exception.Message)
        }

        if(-not ($vmObj))
        {
            Write-Error (Get-ResourceString FailedToFindVirtualDesktop $vmName)
            return $false
        }

        #verify the desktop is not a member of any pool
        if(-not ([string]::IsNullOrEmpty($vmObj.CollectionAlias)))
        {
            Write-Error (Get-ResourceString VirtDesktopAlreadyAMember $vmName, $vmObj.CollectionAlias)
            return $false
        }
        if($null -ne $UnmgdVmHostList)
        {
            if(-not $UnmgdVmHostList.Contains($vmObj.HostName))
            {
                $UnmgdVmHostList.Add($vmObj.HostName)
            }
        }
    }
    return $true
}

function Test-CollectionMemberVmList{

Param(
    [Parameter(Mandatory=$true)]
    [string]
    $CollectionAlias,

    [Parameter(Mandatory=$true, Position=0)]
    [string[]]
    $VirtualDesktopList,

    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker 
)

    if(-not ($VirtualDesktopList.Count))
    {
        Write-Error (Get-ResourceString ErrorEmptyVmList)
        return $false
    }
#Verify Each VM

    foreach($vmName in $VirtualDesktopList)
    {
        #exists in current VM list
        try
        {
            $vmObj = Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                -Filter "Name='$vmName'"
        }
        catch 
        {
            Write-Error ($_.Exception.Message)
        }

        if(-not ($vmObj))
        {
            Write-Error (Get-ResourceString FailedToFindVirtualDesktop $vmName)
            return $false
        }

        #verify it is  a member of specified coll
        if(([string]::Compare($vmObj.CollectionAlias,$CollectionAlias,$true)))
        {
            Write-Error (Get-ResourceString VirtDesktopNotAMemeberOfSpecifiedPool $vmName)
            return $false
        }
    }
    return $true
}

function Test-ManagedParam {

param(

    [Parameter(Mandatory=$true, Position=0)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true)]
    [int]
    [ValidateRange(1, 3)]
    $Type,

    [Parameter(Mandatory=$true)]
    [string]
    $VirtualDesktopTemplateName,

    [Parameter(Mandatory=$true)]
    [string]
    $VirtualDesktopTemplateHostServer,

    [Parameter(Mandatory=$false)]
    [string]
    $CustomUnattend=$null,

    [Parameter(Mandatory=$true)]
    [string]
    $VmNamePrefix,

    [Parameter(Mandatory=$true)]
    [hashtable]
    $VmDistribution,

    [Parameter(Mandatory=$true)]
    [int]
    $StorageType,

    [Parameter(Mandatory=$false)]
    [string]
    $CentralStoragePath,

    [Parameter(Mandatory=$false)]
    [string]
    $LocalStoragePath,

    [Parameter(Mandatory=$false)]
    [string]
    $VmTemplateStoragePath,

    [Parameter(Mandatory=$false)]
    [string]
    $UvhdSharePath,

    [Parameter(Mandatory=$false)]
    [int]
    $UvhdSize,
    
    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker
    
)
 
    # Validate VmDistribution, this call also Validate that all the host names specified in the distribution
    # are FQDN and are RDVH servers
    if( -not (Test-VmDistribution -VmDistribution $VmDistribution -ConnectionBroker $ConnectionBroker))
    {
        return $false
    }

    #validate that a collection with same VnName prefix does not exist
    try
    {
        $collProperties = Get-WmiObject Win32_RDMSCollectionProperties -Namespace root\cimv2\Rdms -ComputerName $ConnectionBroker `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop
    }
    catch 
    {
        #ignore  error
    }

    foreach( $collProperty in $collProperties)
    {
        try
        {
            $provProperties = $collProperty.GetProvisioningProperties()
        }
        catch 
        {
            #IGNORE ERROR
        }
        if(-not $provProperties)
        {
            continue
        }

        if(-not [string]::Compare($VmNamePrefix, $provProperties.NamePrefix, $true))
        {
            Write-Error (Get-ResourceString VmNamePrefixAlreadyExists $VmNamePrefix)
            return $false
        }
    }

    #Validate all the input storage paths
    switch($StorageType)
    {
        #local
        1
        {
            if($VmTemplateStoragePath)
            {
                if(-not (Test-LocalStoragePath -Path $VmTemplateStoragePath -HostList $VmDistribution.Keys))
                {
                    return $false
                }
            }
            if($LocalStoragePath)
            {
                if(-not (Test-LocalStoragePath -Path $LocalStoragePath -HostList $VmDistribution.Keys))
                {
                    return $false
                }
            }
        }
        #SMB
        2
        {
            if([string]::IsNullOrEmpty($CentralStoragePath))
            {
                Write-Error(Get-resourceString CentralStorageCanNotBeEmpty)
                return $false
            }
            if( -not (Test-SmbSharePath($CentralStoragePath)))
            {
                return $false
            }
            #if vm template storage path is specified
            #verify it is a share path
            if(-not ([String]::IsNullOrEmpty($VmTemplateStoragePath)))
            {
                if( -not (Test-SmbSharePath($VmTemplateStoragePath)))
                {
                    Write-Verbose ("VirtualDesktopTemplateStoragePath must have same format as that of CentralStoragePath")
                    return $false
                }
            }
        }
        #SAN
        3
        {
            #vierfy central storage path is not empty
            if([string]::IsNullOrEmpty($CentralStoragePath))
            {
                Write-Error(Get-ResourceString CentralStorageCanNotBeEmpty)
                return $false
            }
            if(-not (Test-ClusterStoragePath -Path $CentralStoragePath -HostList $VmDistribution.Keys))
            {
                return $false
            }

            #if vm template storage path is specified
            # verify that it is a cluster storage path
            if(-not ([String]::IsNullOrEmpty($VmTemplateStoragePath)))
            {
                if(-not (Test-ClusterStoragePath -Path $VmTemplateStoragePath -HostList $VmDistribution.Keys))
                {
                    Write-Verbose ("VirtualDesktopTemplateStoragePath must have same format as that of CentralStoragePath")
                    return $false
                }
            }
        }
    }

    # if custom unattend xml file is specified, validate
    if( -not [string]::IsNUllOrEMpty($CustomUnattend))
    {
        if(-not (Test-FilePath -Path $CustomUnattend))
        {
            Write-Error (Get-ResourceString InvalidUnattendFilePath)
            return $false
        }
        $unattend = New-Object xml

        try
        {
            $unattend.Load($CustomUnattend)
            $nsMgr = New-Object System.Xml.XmlNamespaceManager($unattend.NameTable)
            $nsMgr.AddNamespace("wcm", "http://schemas.microsoft.com/WMIConfig/2002/State");
            $nsMgr.AddNamespace("a", "urn:schemas-microsoft-com:unattend");
            $root=$unattend.DocumentElement
        }
        catch
        {
            Write-Error (Get-ResourceString FailedToLoadUnattendFile)
            return $false
        }

        #Make sure computer name and domain join nodes are not present
        $node = $root.SelectSingleNode("//a:*[translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='computername']",$nsMgr)
        if($node)
        {
            Write-Error (Get-ResourceString InvalidUnattendNoCompName)
            return $false 
        }
        $node = $root.SelectSingleNode("//a:*[translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='component' and `
                                              translate(@*,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='microsoft-windows-unattendedjoin']",$nsMgr)
        if($node)
        {
            Write-Error (Get-ResourceString InvalidUnattendNoDomainJoin)
            return $false 
        }
        #todo:?make sure skip OOBE screen settings are in place?
    }

    #validate the master VM
    if(-not(Test-MasterVm   -VirtualDesktopTemplateName $VirtualDesktopTemplateName `
                        -VirtualDesktopTemplateHostServer $VirtualDesktopTemplateHostServer `
                        -ConnectionBroker $ConnectionBroker))
    {
        return $false
    }

    return $true
}


function Test-VmDistribution
{
param(
    [Parameter(Mandatory=$true)]
    [hashtable]
    $VmDistribution,

    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker 
)
    #validate host and vmcount list
    if( 0 -ge $VmDistribution.Count ) 
    {
        Write-Error (Get-ResourceString SpecifyVmAllocation)
        return $false
    }

    #get list of all VMHosts in the deployment
    $vmHostServerList = Get-RDServer -Role RDS-VIRTUALIZATION -ConnectionBroker $ConnectionBroker
    
    $vmHostNameList = New-Object System.Collections.Generic.List[string]
    foreach($hostName in $vmHostServerList)
    {
        $vmHostNameList.Add($hostName.Server.ToLower([System.Globalization.CultureInfo]::CurrentCulture))
    }

    # Validate each host is fqdn and is an added VMHost
    foreach($hostName in $VmDistribution.Keys)
    {
        if(-not (Test-Fqdn($hostName)))
        {
            Write-Error (Get-ResourceString InvalidRdvhFqdn $hostName)
            return $false
        }
        if(-not ($vmHostNameList.Contains($hostName.ToLower([System.Globalization.CultureInfo]::CurrentCulture))))
        {
            Write-Error (Get-ResourceString InvalidRdvhRoleNotFound $hostName)
            return $false
        }
    }
    #???TODO: Do we need to make sure that the computer is part of a cluster? 
    # Should we allow using non-clustered host with SAN storage?

    return $true
}


function Test-MasterVm{

param(

    [Parameter(Mandatory=$true)]
    [string]
    $VirtualDesktopTemplateName,

    [Parameter(Mandatory=$true)]
    [string]
    $VirtualDesktopTemplateHostServer,

    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker 
    
)

    #Verify the host is FQDN and is among the deployment RDS-Virtualization hosts
    if(-not (Test-Fqdn($VirtualDesktopTemplateHostServer)))
    {
        Write-Error (Get-ResourceString InvalidRdvhFqdn $VirtualDesktopTemplateHostServer)
        return $false
    }

    $vmHostServerList = Get-RDServer -Role RDS-VIRTUALIZATION -ConnectionBroker $ConnectionBroker    
    $vmHostNameList = New-Object System.Collections.Generic.List[string]
    foreach($hostName in $vmHostServerList)
    {
        $vmHostNameList.Add($hostName.Server.ToLower([System.Globalization.CultureInfo]::CurrentCulture))
    }
    if(-not ($vmHostNameList.Contains($VirtualDesktopTemplateHostServer.ToLower([System.Globalization.CultureInfo]::CurrentCulture))))
    {
        Write-Error (Get-ResourceString InvalidRdvhRoleNotFound $VirtualDesktopTemplateHostServer)
        return $false
    }
   
    try
    {
        $masterVm = Get-WmiObject Win32_TsVm -Namespace root\cimv2\TerminalServices -ComputerName $VirtualDesktopTemplateHostServer `
                -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                -Filter "VmName='$VirtualDesktopTemplateName'"
    }
    catch 
    {
        Write-Error ($_.Exception.Message)
    }

    if($null -eq $masterVm)
    {
        Write-Error (Get-ResourceString FailedToFindMasterVm $VirtualDesktopTemplateName)
        return $false
    }

    try
    {
        $outParams = $masterVm.QueryOfflineInformation()
    }
    catch [System.Management.Automation.MethodInvocationException]
    {        
        $errMsg = Get-ErrorMsgFromErrCode($_.Exception.InnerException.ErrorCode)
    }
    catch 
    {
        $errMsg = $_.Exception.Message
    }

    if ($errMsg)
    {
        Write-Error (Get-ResourceString FailedToGetMasterVmInfo $errMsg)
        return $false
    }

    if (0 -ne $outParams.ReturnValue)
    {
        $errMsg = Get-ErrorMsgFromErrCode($outParams.ReturnValue)
        Write-Error (Get-ResourceString FailedToGetMasterVmInfo $errMsg)
        return $false
    }
    
    if($outParams.SysPrepImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE')
    {
        Write-Error (Get-ResourceString ErrorMasterVmNotGeneralized)
        return $false
    }
    return $true
}

function Get-ErrorMsgFromErrCode($errCode)
{
    try
    {
        if($errCode -lt 0)
        {
            $errCode = [UInt32]('0x'+('{0:X}'-f $errCode))
        }
        $errCodeToMsg = New-Object Microsoft.RemoteDesktopServices.Management.Cmdlets.RolloutJobReport
        return $errCodeToMsg.GetMessageForError($errCode)
    }
    catch
    {
        return $errCode
    }
}

function Get-ExportRoot
{

param(
    [Parameter(Mandatory=$true)]
    [int]
    $StorageType,

    [Parameter(Mandatory=$false)]
    [string]
    $CentralStoragePath,

    [Parameter(Mandatory=$false)]
    [string]
    $VmTemplateStoragePath,


    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker

)

    if(1 -eq $StorageType)
    {
        try
        {
            $outParam = Invoke-WmiMethod -class win32_RdmsDeploymentSettings -name GetExportPath `
            -Namespace root\cimv2\rdms -ComputerName $ConnectionBroker -ErrorAction Stop -Authentication PacketPrivacy 
        }
        catch 
        {
            Write-Error ($_.Exception.Message)
        }
        if( (-not $outParam) -or ([String]::IsNullOrEmpty($outParam.DirectoryPath)))
        {
            Write-Error (Get-ResourceString ExportRootNotFound)
            return $null
        }
        if(-not (Test-SmbSharePath -Path $outParam.DirectoryPath -ErrorAction SilentlyContinue))
        {
            Write-Error (Get-ResourceString ExportRootInvalid $outParam.DirectoryPath)
            return $null
        }

        $exportRoot = $outParam.DirectoryPath
    }
    elseif(-not([string]::IsNullOrEmpty($VmTemplateStoragePath)))
    {
        $exportRoot=$VmTemplateStoragePath
    }
    else
    {
        $exportRoot=$CentralStoragePath
    }

    Write-Verbose ("Selected ["+$exportRoot+"] for exporting template")
    return $exportRoot
}


function Get-UNCPath{
param(
    [Parameter(Mandatory=$true)]
    [string]
    $Path,

    [Parameter(Mandatory=$true)]
    [string]
    $Hostname
)
    $uncPath = $Path
    $uri = New-Object System.Uri($Path)
    if (-not $uri.IsUnc)
    {
        $Path = $path.Replace(':', '$');
        $uncPath = '\\' + $HostName + '\' + $Path
    }

    return $uncPath
}

function New-VMExportLocation{
param(
    [Parameter(Mandatory=$true)]
    [string]
    $ExportRoot,

    [Parameter(Mandatory=$true)]
    [string]
    $Alias,
    
    [Parameter(Mandatory=$true)]
    [string]
    $VirtualDesktopTemplateHostServer
)

    $uncPath=Get-UNCPath -Path $ExportRoot -Hostname $VirtualDesktopTemplateHostServer

    #if the export location already exists, create new path
    $i=0
    do
    {
        $i++
        $childPath=$Alias+"\IMGS\__"+$i
    }while([System.IO.Directory]::Exists($uncPath+"\"+$childPath) -or `
           [System.IO.File]::Exists($uncPath+"\"+$childPath))
    
    #create directory
    try
    {
        $dir = [System.IO.Directory]::CreateDirectory(($uncPath+"\"+$childPath))
    }
    catch 
    {
        Write-Error (Get-resourceString FailedToCreateFolder ($uncPath+"\"+$childPath), $_.Exception.Message)
        return $null
    }

    #return created path
    return ([string]($ExportRoot+"\"+$childPath))
}

#gets the "Netbios" name using WMI
function Get-NtAccount{
param(
    [Parameter(Mandatory=$true)]
    [string]
    $HostName
)
    try
    {
        $compObject = Get-WmiObject Win32_ComputerSystem -ComputerName $HostName -ErrorAction Stop
    }
    catch 
    {
        Write-Error (Get-ResourceString FailedToGetComputerObject $HostName, $_.Exception.Message)
        return $null
    }
    return ($compObject.Domain+'\'+$compObject.Name+'$')
}

function Set-PermissionsOnShare{

param(

    [Parameter(Mandatory=$true)]
    [string]
    $Path,

    [Parameter(Mandatory=$true)]
    [string]
    $Account

)
    $accSid = ConvertTo-Sid($Account)
    if($null -eq $accSid)
    {
        Write-Error (Get-ResourceString InvalidAccount $Account)
        return $false
    }

    $rights = [System.Security.AccessControl.FileSystemRights]::FullControl
    $dirInfo = New-Object System.IO.DirectoryInfo($Path)
    $dirSecurity = $dirInfo.GetAccessControl()

    $currAccessRules = $dirSecurity.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier])
    foreach($rule in $currAccessRules)
    {
        if([string]::Compare($rule.IdentityReference.Value, $accSid.Value, $true))
        {
            continue
        }
        $maskedRights = ($rule.FileSystemRights -band $rights)
        if($maskedRights -eq $rights)
        {
            return $true
        }
    }
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($Account, $rights, `
                ([System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit), `
                [System.Security.AccessControl.PropagationFlags]::None, [System.Security.AccessControl.AccessControlType]::Allow)
    $modified = $false
    $dirSecurity.ModifyAccessRule([System.Security.AccessControl.AccessControlModification]::Reset, $accessRule, [ref]$modified)
    try
    {
        $dirInfo.SetAccessControl($dirSecurity)
    }
    catch 
    {
        Write-Error (Get-ResourceString FailedToSetFolderPermission $Path,$_.Exception.Message)
        return $false
    }
    return $true
}

function Set-ShareProvisioningPermissions {
param(
    [Parameter(Mandatory=$true)]
    [string]
    $Path,

    [Parameter(Mandatory=$true)]
    [string[]]
    $HostList
    )
    $nwAccPermSet = $false
    $nwAcc = ConvertTo-NtAccount('S-1-5-20')
    $pathUri = New-Object System.Uri($Path)

    foreach($host in $HostList)
    {
        $uncPath = Get-UNCPath -Path $Path -Hostname $host
        $shareHostName = (New-Object System.Uri($uncPath)).Host

        $hostAcc = Get-NtAccount($host)
        if(-not $hostAcc)
        {
            return $false
        }
        $account =  $hostAcc

        $shareHostAcc = Get-NtAccount($shareHostName)
        if(-not $shareHostName)
        {
            return $false
        }

        #if the nt account of host on UNC path matches hostName, use nwSid else Use the Host account
        if(-not ([string]::Compare($hostAcc, $shareHostAcc, $true)))
        {
            $account = $nwAcc
            $nwAccPermSet = $true;
            #if the original path is UNC, it implies it is a loopback SMB or Scale-OUT SMB
            # set permission for machine account as well
            if($pathUri.IsUnc)
            {
                if( -not (Set-PermissionsOnShare -Path $uncPath -Account $hostAcc))
                {
                    return $false
                }
            }
        }
        #set permissions on the UNC path
        if( -not (Set-PermissionsOnShare -Path $uncPath -Account $account))
        {
            return $false
        }

        #if path is UNC, set smb permissions on the share as well
        if($pathUri.IsUnc)
        {
            [Microsoft.RemoteDesktopServices.Common.CommonUtility]::GrantFullSmbPermissions($pathUri.Host, $pathUri.Segments[1], (ConvertTo-Sid($account)))
        }
    }
    # always make sure that network service is granted perms on the SMB share
    # for loopback SMB and scale out SMB
    if(-not($nwAccPermSet) -and $pathUri.IsUnc)
    {
        #set permissions on the UNC path
        if( -not (Set-PermissionsOnShare -Path $Path -Account $nwAcc))
        {
            return $false
        }
    }

    return $true
}

function Set-StoragePermissions{

param(
    [Parameter(Mandatory=$true)]
    [int]
    $StorageType,

    [Parameter(Mandatory=$false)]
    [string]
    $CentralStoragePath,

    [Parameter(Mandatory=$false)]
    [string]
    $LocalStoragePath,

    [Parameter(Mandatory=$false)]
    [string]
    $VmTemplateStoragePath,

    [Parameter(Mandatory=$true)]
    [string[]]
    $HostList
    
)

    Write-Verbose "Setting up permissions on Storage"

    switch($StorageType)
    {
        1
        {
            if( $true -ne [System.String]::IsNullOrEmpty($LocalStoragePath) )
            {
                if(-not (Set-ShareProvisioningPermissions -Path $LocalStoragePath -HostList $HostList))
                {
                    return $false
                }
            }
            if( $true -ne [System.String]::IsNullOrEmpty($VmTemplateStoragePath) )
            {
                if(-not (Set-ShareProvisioningPermissions -Path $VmTemplateStoragePath -HostList $HostList))
                {
                    return $false
                }
            }
        }
        2
        {
            if( $true -ne [System.String]::IsNullOrEmpty($CentralStoragePath) )
            {
                if(-not (Set-ShareProvisioningPermissions -Path $CentralStoragePath -HostList $HostList))
                {
                    return $false
                }
            }
            if( $true -ne [System.String]::IsNullOrEmpty($VmTemplateStoragePath) )
            {
                if(-not (Set-ShareProvisioningPermissions -Path $VmTemplateStoragePath -HostList $HostList))
                {
                    return $false
                }
            }
        }
        3
        {
            if( $true -ne [System.String]::IsNullOrEmpty($CentralStoragePath) )
            {
                if(-not (Set-ShareProvisioningPermissions -Path $CentralStoragePath -HostList $HostList))
                {
                    return $false
                }
            }
            if( $true -ne [System.String]::IsNullOrEmpty($VmTemplateStoragePath) )
            {
                if(-not (Set-ShareProvisioningPermissions -Path $VmTemplateStoragePath -HostList $HostList))
                {
                    return $false
                }
            }
        }
    }
    return $true
}


function Get-ProvisioningXml {

param (

    [Parameter(Mandatory=$true)]
    [string]
    $CollectionAlias,

    [Parameter(Mandatory=$true)]
    [string]
    $Job,

    [Parameter(Mandatory=$true)]
    [string]
    $VhdType,

    [Parameter(Mandatory=$true)]
    [string]
    $BaseVmLocation,

    [Parameter(Mandatory=$false)]
    [string]
    $SmbShare="",

    [Parameter(Mandatory=$false)]
    [string]
    $LocalVmCreationPath="",

    [Parameter(Mandatory=$false)]
    [string]
    $LocalGoldCachePath="",

    [Parameter(Mandatory=$true)]
    [int]
    $RunVmFromSmb,

    [Parameter(Mandatory=$true)]
    [int]
    $EnableVmStreaming,

    [Parameter(Mandatory=$true)]
    [string]
    $Prefix,

    [Parameter(Mandatory=$true)]
    [int]
    $StartIndex,

    [Parameter(Mandatory=$true)]
    [string]
    $Domain,
    
    [Parameter(Mandatory=$false)]
    [bool]
    $HighlyAvailable=$false,

    [Parameter(Mandatory=$false)]
    [string]
    $OU,

    [Parameter(Mandatory=$false)]
    [string]
    $CustomUnattend=$null,

    [Parameter(Mandatory=$false)]
    [hashTable]
    $HostList=@{},

    [Parameter(Mandatory=$false)]
    [string[]]
    $VmList=[string[]]''
    
)

    $unattendXml=
                "<unattend xmlns='urn:schemas-microsoft-com:unattend' xmlns:wcm='http://schemas.microsoft.com/WMIConfig/2002/State'>"+
                    "<settings pass='oobeSystem'>"+
                        "<component name='Microsoft-Windows-Shell-Setup' publicKeyToken='31bf3856ad364e35' language='neutral' versionScope='nonSxS' processorArchitecture='x86'>"+
                            "<OOBE>"+
                                "<HideEULAPage>true</HideEULAPage>"+
                                "<SkipMachineOOBE>true</SkipMachineOOBE>"+
                                "<SkipUserOOBE>true</SkipUserOOBE>"+
                                "<ProtectYourPC>3</ProtectYourPC>"+
                                "<NetworkLocation>Work</NetworkLocation>"+
                            "</OOBE>"+
                        "</component>"+
                        "<component name='Microsoft-Windows-Shell-Setup' publicKeyToken='31bf3856ad364e35' language='neutral' versionScope='nonSxS' processorArchitecture='amd64'>" +
                            "<OOBE>" +
                                "<HideEULAPage>true</HideEULAPage>" +
                                "<SkipMachineOOBE>true</SkipMachineOOBE>" +
                                "<SkipUserOOBE>true</SkipUserOOBE>" +
                                "<ProtectYourPC>3</ProtectYourPC>" +
                                "<NetworkLocation>Work</NetworkLocation>" +
                            "</OOBE>" +
                        "</component>" +
                    "</settings>" +
                "</unattend>"

    if( -not ([System.string]::IsNullOrEmpty($CustomUnattend)))
    { 
        #assuming the XML has already been validated
        $unattendXml = Get-Content $CustomUnattend
    }

    $action="Stop"
    if ($Job -eq "delete")
    {
        $action = "Continue"
    }

    $hyperVHostList=""
    $i=0
    Foreach ($key in $HostList.Keys)
    {
        $hyperVHost = "<rdvp:HyperVHost>"+
                        "<rdvp:Name>"+$key+"</rdvp:Name>"+
                        "<rdvp:NumberVms>"+$HostList[$key]+"</rdvp:NumberVms>"+
                      "</rdvp:HyperVHost>"
        $hyperVHostList+=$hyperVHost
        $i++
    }

    $selectedVmList='<rdvp:SelectedVms>'
    foreach($vm in $VmList)
    {
        $selectedVmList+=('<rdvp:SelectedVm>'+$vm+'</rdvp:SelectedVm>')
    }
    $selectedVmList+='</rdvp:SelectedVms>'
    $HA="No"
    if($HighlyAvailable)
    {
        $HA="Yes"
    }


    $provXmlTemplate=
           "<?xml version='1.0' encoding='utf-16'?>"+
            "<rdvp:RDVProvisioning xmlns:rdvp='http://www.microsoft.com/rdv/2010/05/'>"+
                "<rdvp:ProvisionPoolRequest>"+
                    "<rdvp:Job>"+
                        "<rdvp:Action>"+$Job+"</rdvp:Action>"+
                        "<rdvp:OnError>"+$action+"</rdvp:OnError>"+
                    "</rdvp:Job>"+
                    "<rdvp:Pool>"+
                        "<rdvp:Name>"+$CollectionAlias+"</rdvp:Name>"+
                        "<rdvp:VhdType>"+$VhdType+"</rdvp:VhdType>"+
                        "<rdvp:Mark_HA>"+$HA+"</rdvp:Mark_HA>"+
                    "</rdvp:Pool>"+
                    "<rdvp:Vm>"+
                        "<rdvp:BaseVmLocation>"+$BaseVmLocation+"</rdvp:BaseVmLocation>"+
                        "<rdvp:SMBShare>"+$SmbShare+"</rdvp:SMBShare>"+
                        "<rdvp:LocalVmCreationPath>"+$LocalVmCreationPath+"</rdvp:LocalVmCreationPath>"+
                        "<rdvp:LocalGoldCachePath>"+$LocalGoldCachePath+"</rdvp:LocalGoldCachePath>"+
                        "<rdvp:RunVMsFromSMB>"+$RunVmFromSmb+"</rdvp:RunVMsFromSMB>"+
                        "<rdvp:EnableVmStreaming>"+$EnableVmStreaming+"</rdvp:EnableVmStreaming>"+
                        "<rdvp:NamingPrefix>"+$Prefix+"</rdvp:NamingPrefix>"+
                        "<rdvp:NamingStartIndex>"+$StartIndex+"</rdvp:NamingStartIndex>"+
                        "<rdvp:Domain>"+$Domain+"</rdvp:Domain>"+
                        "<rdvp:OU>"+$OU+"</rdvp:OU>"+
                        "<rdvp:UnattendXml>"+
                        "<![CDATA["+
                        $unattendXml+
                        "]]>"+
                        "</rdvp:UnattendXml>"+
                    "</rdvp:Vm>"+
                    "<rdvp:HyperVHosts>"+
                        $hyperVHostList+
                    "</rdvp:HyperVHosts>"+
                    $selectedVmList+
                "</rdvp:ProvisionPoolRequest>"+
            "</rdvp:RDVProvisioning>"

    return $provXmlTemplate
}


function Test-SmbSharePath{

param(
    [Parameter(Mandatory=$true)]
    [string]
    $Path
)

    #verify it is a UNC path
    $uri = New-Object System.Uri($Path)
    if (-not ($uri.IsUnc))
    {
        Write-Error (Get-ResourceString InvalidSmbSharePath $Path)
        return $false
    }
    #verify
    if(-not (Test-Path -Path $Path -ErrorAction SilentlyContinue))
    {
        Write-Error (Get-ResourceString UnreacahbleSmbSharePath $Path)
        return $false
    }
    return $true
}


function Test-LocalStoragePath{

param(
    [Parameter(Mandatory=$true)]
    [string]
    $Path,

    [Parameter(Mandatory=$true)]
    [string[]]
    $HostList
)

    #verify it is a local drive path
    $uri = New-Object System.Uri($Path)
    if ($uri.IsUnc)
    {
        Write-Error (Get-ResourceString InvalidLocalStoragePath $Path)
        return $false
    }

    #verify path exists on each server
    foreach($hostName in $HostList)
    {
        $uncPath=Get-UNCPath -Path $Path -Hostname $hostName
        if(-not (Test-Path -Path $uncPath -ErrorAction SilentlyContinue))
        {
            Write-Error (Get-ResourceString UnreachableVmhostLocalPath $Path, $hostName)
            return $false
        }
    }
    return $true
}

function Test-ClusterStoragePath{
param(
    [Parameter(Mandatory=$true)]
    [string]
    $Path,

    [Parameter(Mandatory=$true)]
    [string[]]
    $HostList
)
    #verify that central path is in cluster storage format
    $subParts = $Path.Split(([char[]]('\')))
    if( (4 -gt $subParts.Count) -or
        ([string]::Compare($subParts[1],"clusterstorage",$true)))
    {
        Write-Error (Get-resourceString InvalidSanStoragePath)
        return $false
    }

    #verify it is reachable on each host
    if(-not (Test-LocalStoragePath -Path $Path -HostList $HostList))
    {
        return $false
    }
    return $true
}

function Test-ScheduleProvisioningJob{

param(
    [Parameter(Mandatory=$true)]
    [System.Management.ManagementBaseObject]
    $rdvmColl
)

    $jobStatus = Get-RDVirtualDesktopCollectionJobStatus -CollectionName $rdvmColl.Name -ConnectionBroker $ConnectionBroker

    if(-not $jobStatus)
    {
        return $false
    }

    if( ($jobStatus.Status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UNKNOWN) -and
        ($jobStatus.Status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UPDATE_FAILED) -and
        ($jobStatus.Status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UPDATE_CANCELLED) -and        
        ($jobStatus.Status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::JOB_COMPLETED) -and
        ($jobStatus.Status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::JOB_ABORTED) )
    {
        Write-Error (Get-ResourceString ErrProvJobAlreadyScheduled)
        return $false
    }

    #see if there is another job running at backend somehow
    try
    {
        $out = Invoke-WmiMethod -Class Win32_RDMSVirtualDesktopCollection -Name ProvisioningEnumerateJobs -ComputerName $ConnectionBroker `
                -Namespace 'root\cimv2\rdms' -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                -ArgumentList ($rdvmColl.Alias,0) 
    }
    catch 
    {
        Write-Error $_.Exception.Message
    }
    if((-not $out) -or (0 -ne $out.ReturnValue))
    {
        Write-Error (Get-ResourceString ErrFailedToQueryRunningJob)
        return $false
    }
    if( ($out.JobGuids.Count -gt 0) )
    {
        Write-Error (Get-ResourceString  ErrProvJobAlreadyScheduled)
        return $false
    }

    return $true
}

function Get-ValidRdvhFromArray {
param(
    [Parameter(Mandatory=$true)]
    [int]
    $ConfigurationValue,

    [Parameter(Mandatory=$true)]
    [string[]]
    $HostServer,

    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker
)

    $VirtualizationHosts = Get-RDServer $ConnectionBroker -Role RDS-VIRTUALIZATION
    $HostList = New-Object Hashtable([StringComparer]::InvariantCultureIgnoreCase)

    foreach($Server in $HostServer)
    {
        $WildCard = New-Object System.Management.Automation.WildcardPattern -ArgumentList $Server, IgnoreCase
        $WildCardMatches = ($VirtualizationHosts | Where-Object { $WildCard.IsMatch($_.Server) })

        if($null -ne $WildCardMatches)
        {
            foreach($match in $WildCardMatches)
            {
                if(-not $HostList.ContainsKey($match.Server))
                {
                    $HostList.Add($match.Server, $ConfigurationValue)
                }
            }
        }
        else
        {
            if([System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($Server))
            {
                Write-Verbose (Get-ResourceString WrnWildcardNoMatches $Server)
            }
        }
    }

    return $HostList
}

function Get-ValidRdvhFromHashtable {
param(
    [Parameter(Mandatory=$true)]
    [Hashtable]
    $Allocation,

    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker
)

    $VirtualizationHosts = Get-RDServer $ConnectionBroker -Role RDS-VIRTUALIZATION
    $HostList = New-Object Hashtable([StringComparer]::InvariantCultureIgnoreCase)

    foreach($Server in $Allocation.Keys)
    {
        $match = ($VirtualizationHosts | Where-Object { $Server.ToLower() -eq $_.Server.ToLower() })

        if($null -ne $match)
        {
            $HostList.Add($match.Server, $Allocation[$Server])
        }
    }

    return $HostList
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function Set-VirtualDesktopIdleCount {
[CmdletBinding(DefaultParameterSetName="Default",
HelpURI="http://go.microsoft.com/fwlink/?LinkId=254109")]
param(
    [Parameter(Mandatory=$true, ParameterSetName="Default", Position=0)]
    [int]
    $IdleCount,

    [Parameter(Mandatory=$false, ParameterSetName="Default", Position=1)]
    [string[]]
    $HostServer,

    [Parameter(Mandatory=$true, ParameterSetName="Allocation", Position=0)]
    [Hashtable]
    $Allocation,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory=$false)]
    [ValidateRange(1,100)]
    [int]
    $BatchSize = 10
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ($null -eq $ConnectionBroker)
    {
        return
    }

    switch ($PsCmdlet.ParameterSetName)
    {
        "Default"
        {
            if($null -eq $HostServer)
            {
                $HostServer = "*"
            }

            $HostList = (Get-ValidRdvhFromArray -ConfigurationValue $IdleCount -HostServer $HostServer -ConnectionBroker $ConnectionBroker)
        }
         
        "Allocation"
        {
            $HostList = (Get-ValidRdvhFromHashtable -Allocation $Allocation -ConnectionBroker $ConnectionBroker)
        }
    }

    $JobList = @()
    $ErrInvalidIdleVmCountType = Get-RawResourceString ErrInvalidIdleVmCountType
    $ErrSettingIdleVmCount = Get-RawResourceString ErrSettingIdleVmCount

    foreach($VMHAHost in $HostList.Keys)
    {
        $JobList += Start-Job  -scriptblock `
        {
            try
            {
                $RemoteRegistry=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $args[0])
                $VMHAParamKey=$RemoteRegistry.CreateSubKey("SYSTEM\\CurrentControlSet\\Services\\VmHostAgent\\Parameters")
                if($null -ne $args[3])
                {
                    if($args[3].GetType().FullName -eq "System.Int32")
                    {
                        $VMHAParamKey.SetValue("IdleVmCount", $args[3])
                        $true
                    }
                    else
                    {
                        throw ($args[1] -f $args[3].GetType().FullName, $args[0])
                    }
                }
                elseif($null -ne $VMHAParamKey.GetValue("IdleVmCount"))
                {
                    $VMHAParamKey.DeleteValue("IdleVmCount")
                    $false
                }
            }
            catch
            {
                throw ($args[2] -f $args[0], $_.Exception.Message)
            }
        } -ArgumentList $VMHAHost,$ErrInvalidIdleVmCountType,$ErrSettingIdleVmCount,$HostList[$VMHAHost] -Name $VMHAHost

        if($JobList.Length -ge $BatchSize)
        {
            if($JobList.Length -gt 1)
            {
                Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
            }
            else
            {
                Write-Verbose (Get-ResourceString WaitingForJobToComplete)
            }

            $WaitResult = Wait-Job -Job $JobList

            foreach($job in $JobList)
            {
                if($job.State -eq 'Failed')
                {
                    Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
                }
                else
                {
                    $retVal = Receive-Job $job
                    if($retVal)
                    {
                        Write-Verbose (Get-ResourceString IdleVmCountSetSuccess $job.Name)
                    }
                    else
                    {
                        Write-Verbose (Get-ResourceString IdleVmCountDeleteSuccess $job.Name)
                    }
                    
                }
            }
            
            $JobList = @()
        }
    }


    if($JobList.Length -gt 0)
    {
        if($JobList.Length -gt 1)
        {
            Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
        }
        else
        {
            Write-Verbose (Get-ResourceString WaitingForJobToComplete)
        }
        
        $WaitResult = Wait-Job -Job $JobList
    
        foreach($job in $JobList)
        {
            if($job.State -eq 'Failed')
            {
                Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
            }
            else
            {
                $retVal = Receive-Job $job
                if($retVal)
                {
                    Write-Verbose (Get-ResourceString IdleVmCountSetSuccess $job.Name)
                }
                else
                {
                    Write-Verbose (Get-ResourceString IdleVmCountDeleteSuccess $job.Name)
                }
            }
        }
    }
}

# .ExternalHelp RemoteDesktop.psm1-help.xml
function Get-VirtualDesktopIdleCount {
[CmdletBinding(HelpURI="http://go.microsoft.com/fwlink/?LinkId=254110")]
[OutputType("Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopIdleCount[]")]
param(
    [Parameter(Mandatory=$false, Position=0)]
    [string[]]
    $HostServer,

    [Parameter(Mandatory=$false)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory=$false)]
    [ValidateRange(1,100)]
    [int]
    $BatchSize = 10
)
    $ConnectionBroker = Initialize-Fqdn($ConnectionBroker)
    if ($null -eq $ConnectionBroker)
    {
        return
    }

    if($null -eq $HostServer)
    {
        $HostServer = "*"
    }

    $HostList = (Get-ValidRdvhFromArray -ConfigurationValue 0 -HostServer $HostServer -ConnectionBroker $ConnectionBroker)

    $JobList = @()
    $ValidResults = @()

    $ErrReadingIdleVmCount = Get-RawResourceString ErrReadingIdleVmCount

    foreach($VMHAHost in $HostList.Keys)
    {
        $JobList += Start-Job  -scriptblock `
        {
            try
            {
                $RemoteRegistry=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $args[0])
                $VMHAParamKey=$RemoteRegistry.CreateSubKey("SYSTEM\\CurrentControlSet\\Services\\VmHostAgent\\Parameters")
                $retVal = $VMHAParamKey.GetValue("IdleVmCount")
                $retVal
            
            }
            catch
            {
                throw ($args[1] -f $args[0], $_.Exception.Message)
            }
        } -ArgumentList $VMHAHost,$ErrReadingIdleVmCount -Name $VMHAHost

        if($JobList.Length -ge $BatchSize)
        {
            if($JobList.Length -gt 1)
            {
                Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
            }
            else
            {
                Write-Verbose (Get-ResourceString WaitingForJobToComplete)
            }

            $WaitResult = Wait-Job -Job $JobList

            foreach($job in $JobList)
            {
                if($job.State -eq 'Failed')
                {
                    Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
                }
                else
                {
                    $retVal = Receive-Job $job
                    $ValidResults += New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopIdleCount -ArgumentList $job.Name,$retVal
                }
            }
            
            $JobList = @()
        }
    }

    if($JobList.Length -gt 0)
    {
        if($JobList.Length -gt 1)
        {
            Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
        }
        else
        {
            Write-Verbose (Get-ResourceString WaitingForJobToComplete)
        }
        
        $WaitResult = Wait-Job -Job $JobList

        foreach($job in $JobList)
        {
            if($job.State -eq 'Failed')
            {
                Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
            }
            else
            {
                $retVal = Receive-Job $job
                $ValidResults += New-Object Microsoft.RemoteDesktopServices.Management.RDVirtualDesktopIdleCount -ArgumentList $job.Name,$retVal
            }
        }
    }

    $ValidResults
}


function Set-VirtualDesktopHostSetting {
param(
    [Parameter(Mandatory=$true)]
    [string]
    $CollectionName,

    [Parameter(Mandatory=$true)]
    [string]
    $Name,

    [Parameter(Mandatory=$true)]
    [string]
    $Value,

    [Parameter(Mandatory=$true)]
    [string]
    $ConnectionBroker,

    [Parameter(Mandatory=$false)]
    [ValidateRange(1,100)]
    [int]
    $BatchSize = 10
)

    $VirtualizationHosts = Get-RDServer $ConnectionBroker -Role RDS-VIRTUALIZATION

    $JobList = @()
    $ErrSettingHostConfiguration = Get-RawResourceString ErrSettingHostConfiguration

    foreach($VMHAHost in $VirtualizationHosts)
    {
        $JobList += Start-Job  -scriptblock `
        {
            try
            {
                $RemoteRegistry=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $args[0])
                $VMHAParamKey=$RemoteRegistry.CreateSubKey("SYSTEM\\CurrentControlSet\\Services\\VmHostAgent\\Parameters" + "\\" + $args[4])
                $VMHAParamKey.SetValue($args[2], $args[3])            
            }
            catch
            {
                throw ($args[1] -f $Name, $args[0], $_.Exception.Message)
            }
        } -ArgumentList $VMHAHost.Server,$ErrSettingHostConfiguration,$Name,$Value,$CollectionName -Name $VMHAHost.Server

        if($JobList.Length -ge $BatchSize)
        {
            if($JobList.Length -gt 1)
            {
                Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
            }
            else
            {
                Write-Verbose (Get-ResourceString WaitingForJobToComplete)
            }

            $WaitResult = Wait-Job -Job $JobList

            foreach($job in $JobList)
            {
                if($job.State -eq 'Failed')
                {
                    Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
                }
                else
                {
                    $retVal = Receive-Job $job
                }
            }
            
            $JobList = @()
        }       
    }    

    if($JobList.Length -gt 0)
    {
        if($JobList.Length -gt 1)
        {
            Write-Verbose (Get-ResourceString WaitingForBatchToComplete $JobList.Length)
        }
        else
        {
            Write-Verbose (Get-ResourceString WaitingForJobToComplete)
        }
        
        $WaitResult = Wait-Job -Job $JobList
    
        foreach($job in $JobList)
        {
            if($job.State -eq 'Failed')
            {
                Write-Error ($job.ChildJobs[0].JobStateInfo.Reason.Message)
            }
            else
            {
                $retVal = Receive-Job $job
            }
        }
    }

    return $true
}

function Test-ExportInProgress{
param(
    [Parameter(Mandatory=$true)]
    [System.Management.ManagementBaseObject]
    $rdvmColl
)
    
    if( ($status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UNKNOWN) -and
        ($status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UPDATE_FAILED) -and
        ($status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::UPDATE_CANCELLED) -and        
        ($status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::JOB_COMPLETED) -and
        ($status -ne [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::JOB_ABORTED) )
    {
        return $false
    }
    try
    {
        $getExpRes =  Invoke-WmiMethod -Name GetStringProperty -ArgumentList "ExportInProgress" -InputObject $rdvmColl `
                        -ErrorAction Stop
    }
    catch 
    {
        Write-Verbose ("Failed to get exportInProgress property")
    }
    if( $getExpRes -and 
        $getExpRes.ReturnValue -eq 0 -and
        -not [string]::IsNullOrEmpty($getExpRes.Value)
        )
    {
        $exportInProgress = $getExpRes.Value
    }
    else
    {
        return $false
    }

    #make sure that virtual machine is being exported, by querying hyper-V job
    try
    {
        $templateDesktopObj = Get-WmiObject -Class "Win32_RDMSVirtualDesktop" -Namespace "root\cimv2\rdms" -ComputerName $ConnectionBroker `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "Name='$exportInProgress'"

        $templateVmObj = Get-WmiObject -Class "MSVM_ComputerSystem" -Namespace "root\virtualization\V2" -ComputerName $templateDesktopObj.HostName `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop `
                    -Filter "ElementName='$exportInProgress'"
        $hvTemplateJobList = [wmi[]](Get-WmiObject -Query ('Associators of {'+$templateVmObj.__RELPATH+'} where ResultClass=Msvm_ConcreteJob') `
                    -Namespace "root\virtualization\V2" -ComputerName $templateDesktopObj.HostName `
                    -Authentication PacketPrivacy -Impersonation Impersonate -ErrorAction Stop)
        foreach($hvJobs in $hvTemplateJobList)
        {
            if($hvJobs.JobType -eq 62)#62 is export job
            {
                return $true
            }
        }        
    }
    catch
    {
        Write-Verbose ("failed to retrieve the hyperV export job for the tempalte VM:"+$exportInProgress)
    }
    #clear the exportInProgress as no export job found associated with the templateName
    try
    {
        $rdvmColl.SetStringProperty("ExportInProgress",[string]::Empty)|Out-Null
    }
    catch
    {
        Write-Verbose ("Failed to clear export in progress")
        #ignore error
    }
    return $false
}


Youez - 2016 - github.com/yon3zu
LinuXploit