{"id":114,"date":"2008-10-15T06:16:00","date_gmt":"2008-10-15T13:16:00","guid":{"rendered":"http:\/\/www.jasemccarty.com\/blog\/2008\/10\/organizational-units-and-virtual-machines.html"},"modified":"2008-10-15T06:16:00","modified_gmt":"2008-10-15T13:16:00","slug":"organizational-units-and-virtual-machines","status":"publish","type":"post","link":"https:\/\/www.jasemccarty.com\/blog\/organizational-units-and-virtual-machines\/","title":{"rendered":"Organizational Units, and Virtual Machines"},"content":{"rendered":"<p>I got an e-mail from a forum buddy yesterday asking about a quick and easy way to change the OU that a deployed virtual machine (or any machine for that matter) belongs to.<\/p>\n<p>When machines are added to an Active Directory domain, they are automatically joined to the Computers OU.  Well, actually, Computers is a Container, but that&#8217;s semantics.<\/p>\n<p>Group Policy Objects can&#8217;t be applied to the Computers container, and as a result, they cannot be leveraged on computers in that container.  That is, with the exception of a domain wide GPO.<\/p>\n<p>So what is a Windows\/VM admin supposed to do when deploying 100+ VDI Desktops, 10 new guests, or the like, and they want to ensure that they have the deployed systems in the right OU?<\/p>\n<p>I worked on part of the puzzle last night, and felt pretty good about the work I had done.  Then when I awoke this morning, a couple more concerns came to mind.<\/p>\n<p>Well, there&#8217;s 2 ways to tackle this problem (one secure and inflexible, another insecure and flexible).<\/p>\n<p>Let&#8217;s start with the insecure one:<br \/>\n<span style=\"font-weight: bold;\">Create a local script or executable that will move the Computer to a new OU<\/span><br \/>\nPros:<br \/>\n1.  Easy to use, can be very flexible, depending on how coded<br \/>\n2.  Add the ability to accept command line parameters to change the target OU<br \/>\nDifferent OU paths could be implemented by different .bat files in the RunOnce registry key.<br \/>\nOne .bat for one path, another for a different one, etc.<br \/>\n3.  Is run from the deployed system, and can be pretty much automated.<br \/>\n4.  Is run dynamically, and automatically is run as new templates are deployed<\/p>\n<p>Cons:<br \/>\n1.  The script cannot make changes to Active Directory, unless the logged in account has AD privileges.<br \/>\n2.  To run the script as a non-authorized user, the script would have to be run with an alternate user&#8217;s credentials<br \/>\n3.  Is there any secure way to store this process where it cannot be exploited?<\/p>\n<p><span style=\"font-weight: bold;\">Create an admin script\/exe that a privileged user runs outside of the VM<br \/>\n<\/span>Pros<span style=\"font-weight: bold;\">:<br \/>\n<\/span>1.  The script is only run by an authorized user, outside of the VM<br \/>\n2.  Leveraging existing tools, and an administrative user&#8217;s security can be better protected<\/p>\n<p>Cons:<br \/>\n1. Manual<br \/>\n2. Someone has to know to run it<\/p>\n<p><span style=\"font-weight: bold;\">So is there a way to have your cake and eat it too?<\/span><br \/>\nIn a perfect world, yes.  In the real world, not without some finagling.<\/p>\n<p><span style=\"font-weight: bold;\">An imperfect method, a local script with elevated privileges:<\/span><br \/>\nI did write a vbscript that will handle the move (minus the elevated credentials piece), but decided to turn it into an executable using <a style=\"font-weight: bold;\" href=\"http:\/\/www.autoitscript.com\/\">AutoITScript<\/a>.   After downloading the AutoITScript package, and installing it, save the script below as an .au3 file, and use the AutoITScript package to compile it.<\/p>\n<p>Here&#8217;s the AutoITScript:<\/p>\n<blockquote><p>#cs &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>AutoIt Version: 3.2.12.1<br \/>\nAuthor:         Jase McCarty<\/p>\n<p>Script Name: moveou.au3<br \/>\nScript Function:<br \/>\nTake a computer out of the Computer Org Unit,<br \/>\nand place it in a specific target Org Unit<\/p>\n<p>Syntax: moveou.exe &#8220;OU=Target,DC=domain,DC=com&#8221;<\/p>\n<p>#ce &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n#Include<\/p>\n<p>; Setup constants for writing to the<br \/>\n; event log<br \/>\nGlobal Const $SUCCESS = 0<br \/>\nGlobal Const $ERROR =1<\/p>\n<p>; Setup our variables so we can have<br \/>\n; space to work<br \/>\nDim $WshShell,$oMyError,$sComputer<br \/>\nDim $sTargetDN,$sRootDSE,$sDomain<br \/>\nDim $sCompLongDN,$sCompShortDN<br \/>\nDim $objCont,$sErrorSet<\/p>\n<p>; Create a Windows Shell object,<br \/>\n; so we can write to the event log<br \/>\n$WshShell = ObjCreate(&#8220;WScript.Shell&#8221;)<\/p>\n<p>; Create an AutoIT Error handler,<br \/>\n; for error trapping<br \/>\n$oMyError = ObjEvent(&#8220;AutoIt.Error&#8221;, &#8220;ComError&#8221;)<\/p>\n<p>; Grab the local computer name<br \/>\n$sComputer = @ComputerName<\/p>\n<p>; Grab the destination Org Unit&#8217;s<br \/>\n; Distinguished Name<br \/>\n$sTargetDN = &#8220;LDAP:\/\/&#8221; &amp; $Cmdline[1]<\/p>\n<p>; Grab the DefaultNamingContext so we have<br \/>\n; DC=company,DC=com or whatever<br \/>\n$sRootDSE = ObjGet(&#8220;LDAP:\/\/RootDSE&#8221;)<br \/>\n$sDomain = $sRootDSE.Get(&#8220;DefaultNamingContext&#8221;)<\/p>\n<p>; Set the Short and Long Distinguished Name values<br \/>\n$sCompShortDN = &#8220;CN=&#8221; &amp; $sComputer<br \/>\n$sCompLongDN = &#8220;LDAP:\/\/CN=&#8221; &amp; $sComputer &amp; &#8220;,CN=Computers,&#8221; &amp; $sDomain<\/p>\n<p>; Load the Target Org Unit as an object<br \/>\n$objCont = ObjGet($sTargetDN)<\/p>\n<p>; Move the Computer into the Target Org Unit<br \/>\n$objCont.MoveHere($sCompLongDN, $sCompShortDN)<\/p>\n<p>; If we were successful, write to the event log as such<br \/>\nIf $sErrorSet &lt;&gt; 1 Then<br \/>\n$WshShell.LogEvent ($SUCCESS, $sComputer &amp; &#8221; moved to the &#8221; &amp; $sTargetDN &amp; &#8221; organizational unit&#8221;)<br \/>\nEndIf<\/p>\n<p>;COM Error function<br \/>\nFunc ComError()<br \/>\n; If we weren&#8217;t successful, write to the event log as such<br \/>\n$WshShell.LogEvent ($ERROR, &#8220;Error running MoveOU script&#8221; )<br \/>\n$sErrorSet = 1<br \/>\nReturn 0<br \/>\nEndFunc<\/p><\/blockquote>\n<p>Again, this doesn&#8217;t handle the elevated credentials piece.  After the script is compiled into an exe, it would be run from the command line like this:<\/p>\n<blockquote><p>moveou.exe &#8220;OU=Destination Org Unit,&#8221;<\/p><\/blockquote>\n<p>And yes, the following comma is necessary.<\/p>\n<p>Now on to the elevated credentials piece.<br \/>\nTo elevate credentials, one must use a package like &#8220;runas&#8221;, a custom AutoITScript, or possibly <a style=\"font-weight: bold;\" href=\"http:\/\/www.joeware.net\/freetools\/tools\/cpau\/index.htm\">CPAU<\/a> from <a style=\"font-weight: bold;\" href=\"http:\/\/www.joeware.net\/\">joeware.net<\/a>.<\/p>\n<p>To do this with &#8220;runas&#8221;, there is one caveat.  The password cannot be passed as part of a script.<\/p>\n<p>To do this with CPAU, a batch file with the following contents would have to be used:<\/p>\n<blockquote><p>CPAU -u user [-p password] -ex &#8220;moveou.exe &#8216;&#8221;OU=Destination Org Unit,'&#8221;<\/p><\/blockquote>\n<p>And the destination org unit has double quotes on the outside, and single quotes on the inside.<\/p>\n<p>To do this with AutoITScript, another au3\/exe would have to be created that can elevate the credentials.  Here&#8217;s one that would do that for you:<\/p>\n<blockquote><p>;moveelevated.au3<\/p>\n<p>Runas($CmdLines[1],$CmdLines[2],$CmdLines[3],0,&#8221;Drive:PathTomoveou.exe &#8221; &amp; $CmdLines[4])<\/p><\/blockquote>\n<p>This would be run as<\/p>\n<blockquote><p>moveelevated.exe username domain password &#8216;&#8221;OU=Destination Org Unit,'&#8221;<br \/>\n<span style=\"font-style: italic;\">*Disclaimer&#8230; I haven&#8217;t tested this, but it should work.<\/span><\/p><\/blockquote>\n<p>Two of these three options (not runas, due to the password limitation) could be put in a RunOnce registry key, and would accomodate the move once the guest has been joined to the domain, and rebooted.<\/p>\n<p>If used with a VMware VirtualCenter&#8217;s Customization Specification, this line could be put in a runonce instruction.  The data (including the password) would be stored in the VC database, and not be sent across the network, except through the customization package dropoff during a guest deployment.<\/p>\n<p><span style=\"font-weight: bold;\">A better method, being run by an admin (on the back end):<\/span><br \/>\n1.  Using DSADD (before the guest is added to the domain)<br \/>\nA preferred method, would be to add the computer accounts into Active Directory, in the appropriate OU beforehand, and as a result when the new machine is added to AD, it will already be in the correct OU.<\/p>\n<p>This can easily be done using the dsadd command.<\/p>\n<blockquote><p><tt><\/tt>dsadd computer &#8220;cn=ComputerName,ou=Test Servers, dc=domain,dc=com&#8221;<\/p><\/blockquote>\n<p>Or it can be put in a batch file that will accept a parameter<\/p>\n<blockquote><p><tt><\/tt>dsadd computer &#8220;cn=%1,ou=Test Servers, dc=domain,dc=com&#8221;<\/p><\/blockquote>\n<p>Which would be run like this:<\/p>\n<blockquote><p>addcomputer.bat ComputerName<\/p><\/blockquote>\n<p>This method is very easy if you know the name of the computer you wish to add.  And keep in mind, this would have to be run before the guest is joined to the domain.<\/p>\n<p>2.  Using the previously mentioned moveou.exe (after the guest is on the domain)<br \/>\nBy modifying the moveou.exe script to accept the computername as a command line parameter, an administrator could run the command with the destination OU, as well as the computername, and have the script move the computer to the desired OU.<\/p>\n<blockquote><p>moveou.exe &#8220;OU=Destination Org Unit,&#8221; ComputerName<\/p><\/blockquote>\n<p>As before, the admin would have to do this manually.<\/p>\n<p>3.  Use the existing Active Directory Users &amp; Computers interface.<br \/>\nI shouldn&#8217;t have to say anything about this.<\/p>\n<p><span style=\"font-weight: bold;\">How VMware could help us out:<\/span><br \/>\nNow, if VMware could add a piece to the deployment process, where after a machine is run, a command could be run (outside of the guest) with the rights of the VirtualCenter user that deployed the machine.<\/p>\n<p>Mass deployment of guests and assignment to an appropriate OU is somewhat problematic right now.  Until we get a little extra functionality from VirtualCenter, we&#8217;ll have to continue the hacking.<\/p>\n<p>Update, I forgot to mention&#8230; There&#8217;s a PDF on <a style=\"font-weight: bold;\" href=\"http:\/\/viops.vmware.com\/home\/docs\/DOC-1103;jsessionid=159C667B39DEA2B29F30A9637BEFB1B7\">viops.vmware.com<\/a> by Chris Skinner, that also addresses this issue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I got an e-mail from a forum buddy yesterday asking about a quick and easy way to change the OU that a deployed virtual machine &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-114","post","type-post","status-publish","format-standard","hentry","category-virtualization"],"_links":{"self":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":0,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}