Thursday, May 29, 2014

Change server for installed printers

While changing our printserver we needed a way to update configuration on all machines in the domain.
First, the printers were migrated from old server to new with exactly the same configuration.

To update workstations, Configuration Baseline was created in SCCM.

Discovery script (returning something else than 0 = not compliant):
Get-WmiObject -Class Win32_Printer | where { $_.SystemName -like "*OLDSERVER*" } | Measure-Object | select -ExpandProperty Count

Remediation script (returning $true will mean failure in process):
$fail = $false
Get-WmiObject -Class Win32_Printer | where { $_.SystemName -like "*OLDSERVER*" } | select Name, Default | foreach {
    $newname = $($($_.name) -replace "OLDSERVER", "NEWSERVER")
    write-host "Replacing $($_.name) with $newname"
    #ADD NEW PRINTER
    (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection($newname)
    if ($($_.default)) {
        write-host "Above is default printer"
        $default = $newname
    }
   
    #WAIT AND CHECK FOR SUCCESS
    write-host "Waiting 30 seconds to check for success"
    Start-Sleep 30
    if ( $(Get-WmiObject -Class Win32_Printer | where { $_.Name -eq $newname } | Measure-Object | select -ExpandProperty Count) -gt 0 ) {
        write-host "SUCCESS, removing old"
        (New-Object -ComObject WScript.Network).RemovePrinterConnection($($_.name))
        write-host "Setting printer as default"
        if ($default) {
            (New-Object -ComObject WScript.Network).SetDefaultPrinter($default)
            $default = $null
        }
    } else {
        write-host "FAIL, keeping old"
        $fail = $true
    }
}
write-host "Next line is True if errors, False if no errors:"
return $fail


The scripts were made quite fast and are not optimized, it is not needed as this is only a temporary configuration baseline. After old printserver is decommissioned, this baseline will be disabled.

Could this be done in a single line of code? :)

Thursday, January 2, 2014

Get AD user groups with managers

Because we have implemented groups with managers in Active Directory, there is a need to take reports on user group memberships and also the owners of the groups the user belongs to.

I had a really long script for a while but I've got a bit better and pipe things shorter (should be a trademark), so here is my new attempt on this topic:
  1. $list = @()
  2. Get-ADUser -Filter "Name -like ""*""" -SearchBase "OU=Users,DC=contoso,DC=eu" -Properties name | % {
  3.     $name = $_.name
  4.     $_ | Get-ADPrincipalGroupMembership | % {
  5.         $line = New-Object PSObject
  6.         $line | Add-Member -MemberType NoteProperty -Name "Name" -Value $name
  7.         $line | Add-Member -MemberType NoteProperty -Name "Group" -Value $($_.name)
  8.         $line | Add-Member -MemberType NoteProperty -Name "Manager" -Value $($_ | Get-ADGroup -Properties managedBy | Select -ExpandProperty managedBy | Get-ADObject | Select -ExpandProperty Name)
  9.         $list = $list + $line
  10.     }
  11. }
  12. $list | export-csv "c:\temp\usergroups.csv" -Encoding Unicode 
Instead of 68 lines I now get things done in 10 12 lines (gave a little edit for better reports). The original script had some comments and extra stuff in it, but it wont be missed.

If anyone has good hints on creating PSObjects with many properties in one line, let me know in comments ;)