Script Dumpster: Find default AD containers for Users and Computers

The Problem

Have you ever created a new user or computer object, but you’re unable to find them in the Default Computers/Users container? Perhaps someone moved the default location to another Organizational Unit [i.e. to make sure GPO’s applied correctly] .

In this case a quick and simple script will check which containers are configured for this task and it will check if they’re using default values or not.

The Script

<#
	.NOTES
	===========================================================================
	 Created with: 	SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.95
	 Created on:   	11/3/2015 13:22
	 Created by:   	Robert Prüst
	 Organization: 	powershellpr0mpt.com
	 Filename:      Find-DefaultUserComputerContainer.ps1
	===========================================================================
	.DESCRIPTION
		A description of the file.
#>
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
[array]$locations = Get-ADObject -Filter { isCriticalSystemObject -eq $true } | Where { ($_.objectclass -eq 'container' -or $_.objectclass -eq 'organizationalUnit') -and (-not ($_.DistinguishedName -like '*ForeignSecurityPrincipals,DC*')) -and (-not ($_.DistinguishedName -like '*CN=System,DC*')) -and (-not ($_.DistinguishedName -like '*Domain Controllers,DC*')) -and (-not ($_.DistinguishedName -like '*ForeignSecurityPrincipals*')) }


foreach ($location in $locations)
{

	$DistinguishedName = $location.DistinguishedName
	if ($location.ObjectClass -eq 'OrganizationalUnit')
	{
		$DefaultFolder = $false
	}
	else
	{
		$DefaultFolder = $true
	}
	$properties = @{
		'Location' = $DistinguishedName;
		'DefaultFolder' = $DefaultFolder;
	}
	$obj = New-Object -TypeName PSObject -Property $properties
	$obj
}

Lab: Configure PowerShell WebAccess for management

Now that I have my Lab configured and set up to accept remoting from my Client machine, I want to set up a small Hyper-V lab onto this Host.

Since my goal is to manage as much as possible through PowerShell, my current setup will run into the following problem: I can remote into my lab host, but due to single-hop remoting, it is not recommended to daisy chain sessions.

In case you DO want this, you can look at the following articles that will give you more insight on multihop remoting. A small insight on what is required:

Script Dumpster: Online-ADComputers

The Problem

Today I ran into something simple….but I just wanted to get it solved through PowerShell and make the solution re-usable.

We had found a machine in Active Directory which didn’t turn up in the physical inventory check… And worse of all, the machine was active!

The Script

This script simply checks if machines with a specific name [or in a specific OU] is online at the moment. If it’s online, it will try and figure out who’s actually logged on to the machine.

Script Dumpster: Merge-CSV

The Problem

A customer required the inventory of product keys for various products on all network systems. I had used a tool which scanned all machines and nicely produced a .csv file for each machine it had scanned.

Ideally I wanted to have a single .csv file which I could then filter against, either using Excel or PowerShell.

The Function

Check the examples notes on how to use the function.

function Merge-CSV{
    <#
            .SYNOPSIS
                Easily merge multiple .csv files into one

            .DESCRIPTION
                Easily merge multiple .csv files into one using splatting to a temporary Output variable
                and adding the content of each .csv file to this Output variable.
                Once each .csv file has been processed, the Output variable is written to a new .csv file.

            .PARAMETER SourceFiles
                Enter paths to multiple .csv files which you would like to merge

            .PARAMETER OutputFile
                Enter filename/path for the merged file

            .NOTES
                Name: Merge-CSV.ps1
                author: Robert Prüst
                DateCreated: 12-06-2015

            .EXAMPLE
                $Licenses = Get-ChildItem -Path c:\Licenses\Export -Filter *.csv
                Merge-CSV -SourceFiles $Licenses -OutputFile c:\Licenses\Overview.csv
    #>

    [Cmdletbinding()]
    param (
        [array]$SourceFiles,
        [string]$OutputFile
    )
    $Output = @()
    foreach ($File in $SourceFiles){
        $TempFile = Import-Csv $File.FullName
        $Output += $TempFile
    }

    $Output | Export-CSV $OutputFile -NoTypeInformation

}

Happy scripting! 🙂

Script Dumpster

At my job I tend to have quiet and busy periods, just like everyone else.

At some times I even have very busy periods… like now. Because of that I tend to not have time or motivation to do any blogging, but that doesn’t mean I’m not doing anything productive.

Because of that I thought “why not just share the work I’m creating?”.

An idea was born….

Then it hit me..

Script Dumpster: Get-AutoStartService

The Problem

A frequent issue on servers is when services that are supposed to start simply haven’t started.

There’s a simple one-liner that can show you the services that meet these requirements for you:

Get-WmiObject -Class Win32_Service -Filter "Startmode = 'Auto' AND State = 'Stopped' AND ExitCode != 0"

Want to directly start up these services? Not a problem!