Advertisements

Archive for the ‘windows’ Tag

Set Owner with PowerShell: “The security identifier is not allowed to be the owner of this object”

I’ve written several PowerShell scripts to help customers adjust permissions to their directory structures when migrating from other file servers(Linux/Samba, Novell OES/Netware, etc).  Part of these scripts includes assigning ownership for the user.  While this tends to take a long time quotas and file reporting are worthless if the administrator that copied everything is assigned as the owner.

Recently I tried to adapt one of these scripts for a customer, but when I ran it it failed with the error: “The security identifier is not allowed to be the owner of this object”

A quick internet search found lots of people saying basically this can’t be done with PowerShell.  What!!! I know for a fact these scripts had worked before.  What’s the deal?   After a lot of testing and beating my head against the wall I figure out I was trying to do something different.  Previously I had run my scripts against the UNC path (eg. \\servername\share\directory), but this time I was trying to run it on a local directory using the drive path (E:\Share\Directory).

Could it be that simple? Yes.  I ran the command again using the UNC path and the script worked as it did before.

Here is an example script to set the owner of a directory or file to test the above:

function pathPrompt {

$tempPath = $null
$tempPath = Read-Host ‘Please enter the path of thedirectory (e.g. “\\file\vol1\users\example”‘
return $tempPath
}

$username=”exampleuser”
$domain=”domain”
$ID = new-object System.Security.Principal.NTAccount($domain, $username)

$path = pathPrompt

write-host $path

$acl = get-acl $path
$acl.SetOwner($ID)
set-acl -path $path -aclObject $acl

Save the above to a file with a .PS1 extension, change the $username and $domain variables, and run it (make sure you set-executionpolicy to unrestricted and PowerShell as administrator). It will prompt for a path. It will then write the path to powershell and then set the owner if it can.

Below is an example of running it against a local path and a UNC path.

Advertisements

Disabling RDP Network Level Authentication (NLA) remotely via the registry

So I logged into a server that was setup by another administrator using RDP to configure some software.  For whatever reason it is requesting a reboot, so I let it reboot before I start my work.  After the server comes back up I attempt to connect and get a “The connection cannot continue because the identity of the remote computer cannot be verified” error.

From experience I knew this means that Network Level Authentication (NLA) is enabled.  NLA is a nice security feature if you have an internal Certificate Authority and time to configure auto-enrollment, but most smaller organization opt for the “less secure” option.  Since I have no console level access I’d have to wait for an onsite technician to change it to allow for “less secure” connectivity.

But I can remote into another server on the same local network and connect to the registry.  A quick google search failed to identify the key/value to change so I did some digging and testing and found it.

To disable NLA remotely:

  1.  Open regedit on another computer on the same network.
  2. Under the File menu click “Connect Network Registry…”
  3. Enter your computer name and click Ok.  If this fails to connect you may be out of luck.
  4. Scroll down in the left pane to find the newly added server. Navigate to this Key:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp
  5. Find the value “SecurityLayer” and change the data to 0  (that is a zero).
  6. Voila, I was able to remote in without issue.

Mirroring SQL Databases

Recently I’ve been setting up mirroring of Microsoft SQL databases.  While the steps required are well documented on Technet (which is where I learned), they are spread over several pages.  There is no single list of steps so I decided to put one together documenting how I setup mirroring using SQL Server Management Studio.

What I’m not going to do is detail all the requirements or possible ways to set mirroring up.  For this information Technet is probably still your best resource.  So, the process below makes certain assumptions:

  1. You’ve already installed and configured the same version of SQL on two servers.
  2. These servers should have the drive setup so that the paths for database and logs files can match on both server. For example, if databases are stored at D:\SQLData on the primary server, this path needs to be available on the secondary server.
  3. These servers both members of the same domain.  This is not a hard requirement of mirroring, but it does make the process easier.  If your servers are in different domains see TechNet for the process of setting up security.
  4. The SQL server service is running as the same domain user on both servers. Again, not a hard requiremnet but it’s how I’ve done it.
  5. I am not including setting up a witness server or automatic failover.  For a configuration with these options, consult technet.

The basic process involves 3 steps, backing up from the primary, restoring to the secondary and configuring the mirroring.

Backup the database on the Primary server

  1. Open SQL Server Management Studio and connect as a user with appropriate permissions (probably sysadmin).
  2. Right-click on the database you wish to mirror and select Tasks->Back Up…
  3. Add a backup destination file with a name like database.bak.
  4. Leave the default options selected.
    1. Backup type: full
    2. Overwrite media:
    3. Backup to the existing media set
    4. Append to the existing backup set
  5. Click OK and let the backup run.
  6. Right-click on the database and again choose Tasks->Back Up…
  7. Change the Backup type: to Transaction log.
  8. Check that the destination file is the same.
  9. Click OK and let it backup again.


Restore the database on the Mirror (destination) server with “No Recovery”

  1. Copy the backup file “database.bak” to the Mirror server, preferably in the default SQL backup directory.
  2. Open SQL Server Management Studio and connect to the Mirror server as a user with appropriate permissions (probably sysadmin).
  3. Right-click the “Databases” object and select “Restore Databases.”
  4. Under “Source to restore” click the “From device” radio button, then click the browse button to the right.
  5. Click Add and select the backup file you copied from the Primary server. Click OK.
  6. Select both the full and transaction log backups you created earlier.
  7. Under the Destination for restore, use the drop down box to select the database name. This should match the database name on the primary server.
  8. Change to the Options page.
  9. Change the recovery state to “RESTORE WITH NORECOVERY” as shown below.
  10. Click OK to restore the database.

Configure Mirroring

  1. Return the SQL Management studio connected to the Primary server.
  2. Right-click on the database and choose Tasks->Mirror…
  3. Click the Configure Security button.
  4. Click next on the starting page.
  5. Choose whether or not to include a witness server instance. For this example we will choose no. Click next.
  6. Click Next on the Principal Server Instance page.
  7. Select or browse so the “Mirror server instance”  shows the name of the desired mirror server.
  8. Click the Connect button to the right. Enter credentials (if necessary) to connect again with sysadmin privileges.  Click connect.
  9. Enter the correct service accounts.
    1. I’ve always configured mirroring between SQL servers in the same domain, with SQL services running as the same user.  So I always enter something like the below.
  10. Click Next then Finish.
  11. You should then get a Success box. Click close.
  12. You will then be prompted with the below message. If you want mirror using the default Operating mode (High Safety without automatic failover) then click Start Mirror. For this example I will choose Do Not Start Mirroring.
  13. Change to the desired Operating Mode. (Below I’ve changed to High Performance which I believe requires SQL Enterprise)

  14. Click the “Start Mirroring” button.

  15. The Status should change to the below:

Congratulations, your database is now mirrored.

Notes:

  • With some backup software it may be necessary to recreate you backup jobs after configuring mirroring.

 

 

 

VBScript to create a My Documents Variable

I was recreating a login script for a client that mapped a drive to the location of their redirected My Documents.  Because they were in the middle of a migration the My Documents could be in a different spot depending on if a user had been migrated to a new environment or not. So I needed to be able to query the current location of the My Docs, much like the %userprofile% shows the path to the current profile.

Below is a chunk of VBScript I cobbled together from a variety of sources including ActiveXperts that reads the appropriate registry key value of the user that runs the script.  This chunk just outputs the path to the command line but the myDocsPath variable could also be used to map a drive.

(Please note that the line that starts “Set oReg=GetObject….” and the line below it need to be a single line)

—-Copy Everything Below—–

const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer &
 "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
strValueName = "Personal"
oReg.GetExpandedStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,myDocsPath
StdOut.WriteLine "Current My Docs path is: " & myDocsPath

 —-Copy Everything Above—–

Creating Lists of Installed Applications

Frequently while performing network assessments I need to assemble a list of all the programs installed on servers. Going to Add/Remove Programs and writing it down is a tedious process.  Installing applications like Belarc Advisor is a bit more intrusive then I want to be on another companies servers.  After some research today I found two solutions that work well:

For XP and Server 2003

http://www.billsway.com/vbspage/ShowScript.asp?tgt=txtfiles/InstalledPrograms.txt

The above contains the contents of a vbscript. You copy the contents into a text file and save it with a .vbs extension.  You can then double-click it. It will prompt you for a computer name or IP address. When it finishes running it generates a file that contains a list of all the programs installed.  It seems to work great with Windows XP and 2003, but only returns a partial list for Windows 2008. 

 For Vista, Windows 7, Server 2008 and 2008 R2

http://www.intowindows.com/how-to-get-list-of-installed-apps-in-vistawindows-7-without-any-software/

This link shows how to generate the list using wmi commands. It says it is for vista and windows 7, but seems to work on Server 2008 just fine. Unlike the vbscript, this must be run directly on the computer in question.

 Conclusion

Both these methods seem to work well, but they do generate files on the system that need to be copied off to be useful.  If you are using them on someone elses computers I would advise you get explicit permission before using these tool. 

Unrelated but also helpful.

If your assessment involves Dell servers, you might want to get the service tags for the servers.  Older Dells tend to put this sticker in inconvenient locations. Luckily you can query the servers for the tag using this handy vbscript.

http://lazynetworkadmin.com/content/view/13/6/

Advertisements