Category Archives: windows

List Files Last Modified 30 Days

How to list files last modified in the last 30 days via Powershell.  Run these commands in the directory containing the files to filter.

$lastModAge = (get-date).adddays(-30);
Get-ChildItem | Where { $_.LastWriteTime -gt $lastModAge }

NSLookup Sweep

I keep finding the need to find an open IP address for a new server, and I get tired of searching manually.  I am also not patient enough for a ping sweep, so I created a nslookup sweep with powershell. The nslookup sweep just looks through a subnet to find the first address without a dns entry. The next step just requires you to ping the address to insure it’s not being used.  Check out this post for a TCP Port Scanner in PowerShell.


$i = "1";
$subnet = "192.168.1";
while ( $i -lt 255 ) {
$ipaddr = "$subnet.$i"
$nsResult = nslookup $ipaddr 2>&1 | select-string "Name";
if ( $nsResult ) {
$i++;
}else{
echo "$ipaddr is available";
$i = "256";
}
}


PowerCLI and Storage

Below are some interesting sets of PowerCLI commands that will give you more information on your VM’s hard drives and your datastores.

The below will give you the total allocated hard drive space of all the VM’s in a cluster.

Get-Cluster "Cluster Name" | get-vm | Get-HardDisk | %{ $vmCapacity += $_.capacityKB }
$vmCapacity

The below will give you the Total Capacity, Free Space, and Usage of your datastores.

Get-Datastore | %{ $datastoreFreeSpace += $_.FreeSpaceMB }
Get-Datastore | %{ $datastoreTotalCapacity += $_.CapacityMB }
$datastoreUsageTotal = $datastoreTotalCapacity - $datastoreFreeSpace

write-host "Total Capacity: $datastoreTotalCapacity MBs"
write-host "Total Free Space: $datastoreFreeSpace MBs"
write-host "Total Usage: $datastoreUsageTotal MBs"


Activate Office 2010 with KMS Server

Step one, make sure you have a KMS server and you have set it up for Office 2010.  If not, check out my post on setting up your KMS server Office 2010.

Activating Office 2010 with your KMS Server

Open an elevated command prompt on your client with Office 2010 installed.

Change directory into your Office installation directory cd “C:\Program Files\Office 2010\”

Then change directory into office14 cd office14\

Now run ospp.vbs /sethst:kmsserver.yourdomain

The above command set’s your KMS Server (replace kmsserver.yourdomain with your server’s name)

Then run ospp.vbs /act

The above command activates your Office 2010 with your KMS server

Now close your command prompt, you are good to go.


Setup KMS Server for Office 2010

Log into your Windows Server 2008 R2 KMS Server or server to be.

Download KeyManagementServiceHost.exe file from the MS Office KMS Host License Pack page

Now run the KeyManagementServiceHost.exe file.

Enter your KMS Host Key when it asks.

Then just continue with activation when prompted.

More information on KMS Servers and Office 2010 can be found here.


Create EventLogs using Powershell

Create container for logs:

New-EventLog -logname 'Event Log Name' -source 'logsourcename'

Note: Must be run in elevated powershell window

Command to create event log:

Write-EventLog -computername localhost -logName 'Event Log Name' -EventID 5000 -Message "Hello World!" -EntryType Information -source logsourcename

Finding newly created event log:
Open Server Manager

Expand Diagnostics

Expand Event Viewer

Expand Applications and Services Logs

Click on the container that you named using the -logName parameter
Your Logs should be in the right pane.

PowerShell and DNS

Here is a quick one liner to show how you can pull DNS information and filter it using powershell.  Replace zonename with the name of the zone you want to pull information from and replace computername with the name of the computer you are searching for.

dnscmd.exe /zoneprint zonename | where{$_ -like "*computername*"}


Powershell Remove Unknown User Permission

Below is powershell code to remove unknown user permissions.  These permissions show up as a SID in the file or folder’s ACL.  These are created when a user is deleted but the users permissions remain as a SID in the ACLs.  This code is designed to recursively remove them.

$location = "\\server\share";
#Search recursivly through location defined;
get-childitem -r $location | foreach{
     $tempLocation = $_.FullName;
     #Get ACL for tempLocation;
     $acl = get-acl $tempLocation;
     #Get SID of unknown user from ACL;
     $acl.Access | where{
          $_.IdentityReference -like "*S-1*" -and $_.isinherited -like $false} | foreach{
          #Foreach SID purge the SID from the ACL;
          $acl.purgeaccessrules($_.IdentityReference);
          #Reapply ACL to file or folder with out SID;
          Set-Acl -AclObject $acl -path $tempLocation;
     }
}


PS Script Pull Event Log data

Below is a script I wrote to help identify computers that had a trust relationship issue with the Active Directory domain.  This script searches for an event on the domain controller that you run this on(please use caution).  It looks for an event that has an event ID of 5723, and that happened today.  Then it pulls the name of the computer identified in the event.  This script allows me to know and address the computers with issues before I get a support call.

#Get todays date;
$today = get-date -uformat "%m/%d/%Y"; 
#Create array for computers;
$cName = @();
#Pull events from system event log that are errors;
$events = Get-EventLog -log system -entrytype Error 
#Filter the events where eventID is 5723 and the time written is today;
$events = $events | where { 
 $_.eventID -eq 5723 -and $_.timewritten -like "$today*" 
}
#Select computer name property;
$events = $events | select ReplacementStrings;
#Add computer names to array;
$events | foreach { $cName += $_.replacementstrings[0];}; 
#Print array leaving out duplicates
$cName | select -uniq;

Schedule Task – Run batch file at Startup

Creating a scheduled task to run a batch file at startup is a very easy process, but if you miss a certain setting it can be very frustrating.  I have a quick how-to on creating a scheduled task that runs a batch file at startup, below.  This how-to was created using a Windows 2008 R2 Server, if you notice variations on other versions of Windows please comment to help others.  Thank you.

Type in a Name and Description and click “Next


Click the radio button for “When the computer starts” and click “Next

Make sure “Start a program” is marked and click “Next

Type in the location of the script under “Program/script” and the folder the script is in in the “Start in (optional):” textbox

Click “Next

Click “Finish