Thursday, September 20, 2018

PowerShell script to reset service account password and restart services on multiple servers.

Need: You need to reset the password for a service account on one or more services on multiple servers at the same time, and restart the impacted services.

Prerequisite: Create a txt file with all the servers that are using a specific service account for one or more services.  Create a column header of "servers" and just paste all the servers names below.You can compile this file from my earlier blogpost here.

Script:

$newpass = "ReallyLongPasswordYouWouldHateTyping"
Import-csv C:\data\computers.txt|foreach-object {
$server = $_.servers
$services = get-wmiobject win32_service -ComputerName $server |?{$_.Startname -like "*svcaccountname*"}
foreach($service in $services)
{$service.change($null,$null,$null,$null,$null,$null,$null,$newpass)
get-service $service.name -ComputerName $server|stop-service
start-sleep -s 30
get-service $service.name -ComputerName $server|start-service
}}

Tuesday, August 7, 2018

PowerShell script to find all services using domain credentials

Whether it's changing a service account password, or looking for admins running service accounts under their own credentials, this script will come in handy. If you put a list of computer names or IP addresses in a servers.txt file, you can grab all services that have an account configured with your domain name or upn.

Import-csv C:\temp\servers.txt|foreach-object{
get-wmiobject win32_service -ComputerName $_.Server |?{$_.Startname -like "*Domain*" -or $_.Startname -like "*UPNSuffix*"}|select PSComputerName,Name,Started,State,StartName,Description|export-csv C:\temp\services.csv -append -notypeinformation
}
Results:

Thursday, May 3, 2018

User Home Drive Report


I was tasked with creating a report on our user home drives of information on every file, such as name, size, modified date, file extension, etc. They also wanted to include information on the user, including things like their name, location, department, and title. Since the home drives are created with the username, it made it pretty easy.

Get-ChildItem "\\fileserver\users$" |Where-Object {$_.psiscontainer -eq $true}|Select-Object -First 10|ForEach-Object{
 Get-ChildItem $_.FullName -Recurse -ErrorAction SilentlyContinue|Where-Object {$_.psiscontainer -eq $false}|Foreach-Object{
  $EID = $_.FullName
  $EID = $EID.Trim("\\\\fileserver\\users$\\")
  $EID = $EID.Substring(0,6)
  $User = (get-aduser $EID -Properties GivenName,Surname,Title,StreetAddress,Department,HomeDirectory)
  $UName = $User.GivenName + " " + $User.Surname
  $Server=$_.FullName.Trim("\\\\")
  $Server=$Server.Split('\\')[0]
 
$props = @{
    EmployeeID = $EID
    UserName = $UName
    UserTitle = $User.Title
    UserLocation = ($User.StreetAddress|Select-Object -First 1)
    UserDepartment = $User.Department
    UserHomeDirectory = $User.HomeDirectory
    File = $_.FullName
    FileServer = $Server
    Extension = $_.Extension
    CreationTime = $_.CreationTime
    LastWriteTime = $_.LastWriteTime
    Name = $_.Name
    SizeInKB = [math]::Round($_.Length/1024,2)
}
  $Export = new-object psobject -Property $props 
  $Export|export-csv -append C:\temp\First10.csv -NoTypeInformation
  $User = $null
}
}

Notes: This was made with the assumption that the employeeID is a fixed length of 6 characters, so you may need to adjust for your environment. Also, if you have a large number of users or files, you will need to break the data into more manageable sizes. I did this using the Select-Object command.

Thursday, March 1, 2018

Free up disk space on 2008/R2 Servers

It's no secret that 2008/R2 servers do a horrible job with the WinSxS folder growing ridiculously out of control. Microsoft's stance is, "Deal with it." They have options for newer operating systems, but 2008/R2 is not so blessed. If you have a physical server, or other reasons the disk is unable to be easily expanded, you can be desperately low on free disk space, while WinSxS consumes over 20GB. Here's what I've found that helps, with no reboots required:

First, try to clean up the files left over from a Service Pack installation with the following command:
DISM.exe /online /Cleanup-Image /SPSuperseded

That can sometimes buy you a few GB, but if you need more, consider compressing the WinSxS folder, which can free up many GBs, as you can see in this example. Since compression doesn't delete anything, and these files are seldom referenced, it should result in no negative impact upon the server.



Step 1 - Stop services and backup ACL
sc stop msiserver
sc stop TrustedInstaller
sc config msiserver start= disabled
sc config TrustedInstaller start= disabled
icacls "%WINDIR%\WinSxS" /save "%WINDIR%\WinSxS.acl" /t

Step 2 - Take ownership
takeown /f "%WINDIR%\WinSxS" /r

Step 3 - Grant permissions
icacls "%WINDIR%\WinSxS" /grant "Domain\Username":(F) /t
*This would be the username you have used to login to the server, or you can always grant it to a group of which your account is a member, like Domain Admins.

Step 4 - Compress files
compact /s:"%WINDIR%\WinSxS" /c /a /i *

Step 5 - Restore permissions
icacls "%WINDIR%\WinSxS" /setowner "NT SERVICE\TrustedInstaller" /t
icacls "%WINDIR%" /restore "%WINDIR%\WinSxS.acl"

Step 6 - Delete ACL backup and start services
del "%WINDIR%\WinSxS.acl"
sc config msiserver start= demand
sc config TrustedInstaller start= demand