Powershell scripts you cant live without

Soldato
Joined
12 Jan 2006
Posts
5,610
Location
UK
For anything complicated with file and directory permissions I use SetACL.

When we migrated all of our home directories and file shares we used it to apply all the permissions we needed. I tested icacls, but it was far harder to get the granularity of permissions and inheritance we wanted.

I've just looked through the documentation as a refresher and it's pretty daunting. Bit worrying as I think another migration to 2012R2/2016 might be in my not too distant future.

Yeah it was a bit of a pain in the ass, but got it done in the end. I am sure there is the odd folder without this group for what I needed done one or two won't make a difference.
 
Associate
Joined
30 Jun 2017
Posts
1
Add all users in a parictular OU to a particular security group.

#Variables
$TargetGroup = “AD Security Group
$TargetOU = “OU=Users Accounts,DC=domain,DC=com

#Target user query
$UserAccounts = Get-ADUser -Filter * | ?{$_.DistinguishedName -like “*$TargetOU*” -and $_.Enabled -eq “True”}

ForEach($User in $UserAccounts)
{

$UsersName = $User.Name

#Check for group membership

$Membership = Get-ADGroup $TargetGroup | Get-ADGroupMember | ?{$_.Name -eq $UsersName}

if(!$Membership)
{

“Adding $UsersName to $TargetGroup”

Get-ADGroup $TargetGroup | Add-ADGroupMember -Members $User -Verbose

}
}
 

maj

maj

Soldato
Joined
19 Jul 2010
Posts
2,598
Location
Durham
We're wanting to migrate our pupil AD accounts so their home directory is their new username. At the moment we've given them their new accounts but their home directory is pointing to their old username. What I want is a powershell script that reads a CSV file I've created with their old and new username in that moves the files in their old directory to their new one based on the data in the spreadsheet. Something like:

Code:
Foreach user $user in $users (with $users being the CSV file).
{
\\fileserver\\share\$users.Old  to \\fileserver\share$\$users.New
}


Seems simple enough but just can't get it working. Any suggestions?
 
Soldato
Joined
24 Apr 2013
Posts
3,067
This one takes a input.txt to list all your SQL boxes and it then queries their reg keys and ties them to a friendly SQL version, then outs to a .CSV

Code:
# Get all server names from the saved txt file
$servers = get-content c:\sql.txt;
 
# Loop through each server
foreach ($server in $servers) {
 
    $out = $null;
 
    # Check if computer is online
    if (test-connection -computername $server -count 1 -ea 0) {
 
        try {
            # Define SQL instance registry keys
            $type = [Microsoft.Win32.RegistryHive]::LocalMachine;
            $regconnection = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $server) ;
            $instancekey = "SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL";
 
            try {
                # Open SQL instance registry key
                $openinstancekey = $regconnection.opensubkey($instancekey);
            }
            catch { $out = $server + ",No SQL registry keys found"; }
 
            # Get installed SQL instance names
            $instances = $openinstancekey.getvaluenames();
 
            # Loop through each instance found
            foreach ($instance in $instances) {
 
                # Define SQL setup registry keys
                $instancename = $openinstancekey.getvalue($instance);
                $instancesetupkey = "SOFTWARE\Microsoft\Microsoft SQL Server\" + $instancename + "\Setup";
 
                # Open SQL setup registry key
                $openinstancesetupkey = $regconnection.opensubkey($instancesetupkey);
 
                $edition = $openinstancesetupkey.getvalue("Edition")
 
                # Get version and convert to readable text
                $version = $openinstancesetupkey.getvalue("Version");
 
                switch -wildcard ($version) {
                    "12*" {$versionname = "SQL Server 2014";}
                    "11*" {$versionname = "SQL Server 2012";}
                    "10.5*" {$versionname = "SQL Server 2008 R2";}
                    "10.4*" {$versionname = "SQL Server 2008";}
                    "10.3*" {$versionname = "SQL Server 2008";}
                    "10.2*" {$versionname = "SQL Server 2008";}
                    "10.1*" {$versionname = "SQL Server 2008";}
                    "10.0*" {$versionname = "SQL Server 2008";}
                    "9*" {$versionname = "SQL Server 2005";}
                    "8*" {$versionname = "SQL Server 2000";}
                    default {$versionname = $version;}
                }
 
                # Output results to CSV
                $out =  $server + "," + $instancename + "," + $edition + "," + $versionname;
 
            }
 
        }
        catch { $out = $server + ",Could not open registry"; }       
 
    }
    else {
    $out = $server + ",Not online"
    }
 
    $out >> c:\sql.csv;
}
 
Soldato
Joined
24 Apr 2013
Posts
3,067
This one is quick for obtaining legacy exchange DN which is always a pain in the butt when working with users hidden from your GAL etc:

Code:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
 
$user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($ct, "DOMAIN\USERNAME")
$user.GetUnderlyingObject().legacyExchangeDN
 
Soldato
OP
Joined
18 Oct 2002
Posts
8,118
Location
The Land of Roundabouts
joke of the day email anyone? i was learning on interacting with websites and came up with this as a quick concept. Zero error handling so if the site ever changes it will likely break - Just edit the first few lines to suit.

Code:
##### Joke of the Day email
$smtpTo = "[email protected]"
$cc = @("[email protected]","[email protected]")
$smtpServer = "YourSMTPserver"
$smtpFrom = "Joke<[email protected]>"
$messageSubject = "▅ ▆ ▇ Joke Of The Day ▇ ▆ ▅"



$URI = “http://www.laughfactory.com/jokes/joke-of-the-day “
$HTML = Invoke-WebRequest -Uri $URI
$joke = ($HTML.ParsedHtml.getElementsByTagName(‘div’) | Where{ $_.className -eq 'joke-text’ } ).innerText -replace "Today's Joke", "" | select -first 1

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
  if($cc -ne $null) {
    foreach($person in $cc) {
    $message.CC.Add($person)
    }
  }

$message.Subject = $messageSubject    
$message.Body = $joke
$message.IsBodyHTML = $true   
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
 
Back
Top Bottom