Windows Spotlight Wallpaper

Windows Spotlight has some nice wallpaper and I’d like to keep a couple for later use, so here’s the shortest Powershell script I could make to save them to my Pictures directory:

$src = "$env:LOCALAPPDATA\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets"

$dest = "$env:USERPROFILE\Pictures\Windows Spotlight"

Get-ChildItem "$src" | where {$_.length -gt "141160"} | foreach {cp "$src\$_" "$dest\$_.jpg"}

I only copy files greater than 141160 bytes because some files in that directory are not wallpapers and the largest non-wallpaper file is 141160 bytes. So it’s a good starting point.

Advertisements

If user exists?

Getting a simple check if an AD user exists has been a nightmare. I wanted something simple and straightforward and I finally found it.

$userobj = $(try {Get-ADUser $user} catch {$Null})
If ($userobj -ne $Null) {
   Write-Host "$user already exists" -foregroundcolor "green"
}
else {
   Write-Host "$user not found " -foregroundcolor "red"
}

Another options would be to use dsquery:

if (dsquery user -samid $user) {
   Write-Host"$($user.name) exists."
}
else {
   Write-Host"$($user.name) doesn't exist."
}

These are the simplest options I can find.

Powershell and SNMP

I’ve been trying to get all my servers snmp settings fixed and it’s been kind of a pain. Powershell doesn’t have any good cmdlets, but I’ve found a couple examples. This is very nice script, but doesn’t work on remote machines unless you do PS Remoting. (I need to bone up on my PS remoting now.)

I ended up using the following for the base of a script. I need only a very simple script so this looks for a specific registry key which will be the name of the CommunityString. Since mine is ‘Public’, I’m just looking for that.

function Get-RemoteRegistryValue ([string]$ComputerName, [string]$KeyPath, [string]$Value) {
    try {
        $Hive = [Microsoft.Win32.RegistryHive]::LocalMachine
        $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive, $ComputerName)
        $key = $reg.OpenSubKey($KeyPath)
    } catch {
        return "ERR"
    }
    return $key.GetValue($Value)
}
Get-RemoteRegistryValue -ComputerName $server -KeyPath 'SYSTEM\CurrentControlSet\Services\SNMP\Parameters\ValidCommunities' -Value 'Public'

Filter is finicky

Get-ADUser -Filter is really finicky. I finally figured out what I was doing wrong with the help of this wonderfully detailed Powershellish.com post.

Basically, I can’t use properties of variables with -Filter

This does NOT work:

foreach ($user in $userlist) {
  $ADUser = Get-ADUser -Filter{(Surname -eq $user.Surname) -and (GivenName -eq $user.GivenName)}
}

This does work:

foreach ($user in $userlist) {
  $ln = $user.Surname
  $fn = $user.GivenName
  $ADUser = Get-ADUser -Filter{(Surname -eq $ln) -and (GivenName -eq $fn)}
}

Find the DHCP Server

I had to find the dhcp server, but I can’t find any good method in Powershell. So I’m using Powershell to parse ipconfig /all.

$a = [string](ipconfig /all | findstr /C:"DHCP Server")
if ($a.Length -gt 0) {
$dhcpserver = $a.Substring($a.IndexOf("1"))
}

Which will take:

DHCP Server . . . . . . . . . . . : 10.10.4.2

And give you:

10.10.4.2

A List of Domain Controllers

This blog post had a simple solution for getting a list of domain controllers in Powershell.

Get-ADDomainController -Filter * | Select-Object name

Although for my script I ended up dropping the Select-Object name.

foreach ($DC in (Get-ADDomainController -Filter *)) {
  $DCName = $DC.Name
  $file = (Get-Item "\\$DCName\netlogon\file.txt").LastWriteTime
  Write-Output("$DCName :: $file")
}

I’m used this as a (very) quick and dirty way to diagnose an AD replication issue by comparing a file’s known modify datetime with copies in the other DC’s netlogon folder.