Wednesday, November 30, 2016

Tuesday, November 22, 2016

Random Numbers and File Testing

Just a little test script. 
Random numbers and File object test....
Goes Nowhere Does Nothing


$random = get-random -maximum 20
echo $random
if ($random -like "?8")
    {
    echo "bang"
    }
if ($random -eq 2)
    {
    echo "2 ME"
    }

$WinUpdLog = "C:\Windows\WindowsUpdate.log"
$values = Get-ItemProperty $WinUpdLog

if ($values.Length -le 1500000)
    {
    echo "Low"
    }
else
    {echo "High"}

Monday, November 21, 2016

Delete a registry key queried from WMI





$key = "hklm:\software\microsoft\windows\currentversion\appx"

$LastUserSID = get-wmiobject -namespace root\CIMV2 -Class Win32_UserProfile | select LastUseTime,SID | sort lastusetime -Descending | select -expand SID -first 1

$target = $key+"\"+$LastUserSID

Remove-Item $target -Recurse



Use Remove-ItemProperty to target individual values for removal

Remove-ItemProperty -Path "HKLM:\Software\SmpApplication" -Name "SmpProperty"

Friday, November 18, 2016

The power of the group - oh and arrays


To find the number of occurrences of items in an array, the group object is fantastic.

Say you have a variable containing an array with a big list of names which are in the array multiple times.  This command returns how many times each name occurs in the array.  Easy or what?

#Create the Array Variable
$ArrayVariable = New-Object System.Collections.Generic.List[System.Object]

#Define a bunch of groups
$GroupsList = get-adgroup -filter {name -like "GROUP-G-*Bentley*" -or name -like "GROUP-G-*Microstation*"} | select -expand name | sort name

#Put all the users in all the group into an array
foreach ($Group in $GroupsList)

    {
    $GroupUsers = Get-ADGroupMember $Group | where {$_.objectclass -eq "user"} | select -expand samaccountname

    foreach ($GroupUser in $GroupUsers)
        {
        $AllFoundUsersArray.Add($GroupUser)
        echo $GroupUser
        }
     }


#Display the number of groups each user is in.
$ArrayVariable | group | sort count -Descending | select name,count

Output:

Name            Count
----               ----
John              5
Alice             4
Bob              3
Tim               2
Baltar           1

Now check out this neat trick.  Reverse the array order :)

[array]::Reverse($ArrayVariable)



Monday, November 7, 2016

The size limit for this request was exceeded


get-adgroupmember is unable to return results when the number of members is very high.  To work around this use the get-adobject cmdlet with a LDAPFilter as follows

Get-ADObject -LDAPFilter "(memberOf=CN=MyGroupName,OU=OUNAME1,OU=OUNAME2,OU=OUNAME3,DC=xxx,DC=xxx,DC=local)" | where {$_.ObjectClass -eq "user"} | measure



Use this get-adobject string to get the objects like groups in an OU

Get-ADObject -Filter {Name -like "*"} -Searchbase 'OU=UserAccounts,DC=Fabrikam,DC=com'

https://technet.microsoft.com/en-us/library/dd391882%28v=ws.10%29.aspx