Windows, Scripting, Virtualization, Cloud Computing - tricks for getting around in the world of Technology

Windows, Scripting, Virtualization, Cloud Computing - tricks for getting around in the world of Technology


  • Category Archives Windows 7
  • How to use KMS server across Active Directory Forests

    Recently I made a slight career change and also with it came a small challenge. We were given 2 Active Directory Forests, one was actively being used and the other one had very few users, but was going to get much larger very quick. People were also deploying Windows 7, Windows 2008 R2 and Office 2010. All of these products canuse KMS license keys, which basically means you have one Key Management Service Server in your organization to which all Windows and other MS products refer to for validating their license periodically. That server must have a valid Windows License, which gets activated to the Internet, then you just enable that server as a KMS host Server. Enough with the background, you can read more about deploying a KMS server here.

    Now back to the problem at hand. Since the 2 organizations to whom the 2 forests belonged to had pretty loose security requirements, we wanted to save us the hassle of creating and managing a second KMS server, and just using the KMS server we had available. KMS is also not so restrictive when it comes to accepting license validation requests.

    KMS clients have 2 ways in which they determine where the KMS service is located (i will use contoso.com as the “main” forest for this example):

    • Specify it manually using a built in windows command line script. For example to specify the kms.contoso.com server for a machine just run from administrator command prompt this command:
    cscript %windir%\system32\slmgr.vbs /skms kms.contoso.com:1688
    • Windows uses DNS to determine the KMS servers (pretty much like Windows does to determine which servers offer AD Authentication). When KMS host is installed in creates an SRV record in the DNS in _.tcp.contoso.com. This record looks like this:

    ServiceName: _vlmcs

    Port: 1688 (default)

    Host offering the service: kms.contoso.com

    The Final srv record looks like this: _vlmcs._tcp.contoso.com

    As you can see there is not so much rocket science in the way a KMS host is published in DNS. Also there is no requirement that the computer trying to validate a license against KMS be joined to a domain. All the computer needs does is a srv DNS query to determine where the KMS licensing host is. Based on this information it talks to KMS and validates the licenses.

    So to make sure computers in forest rivnet.org, for example, can find KMS in DNS do following:

    1. Create a new A Record for the IP address of the KMS server kms.contoso.com, in rivnet.org DNS, for example kms.rivnet.org

    2. Create a new SRV Type record in _tcp.rivnet.org DNS, with following details

    ServiceName: _vlmcs

    Port: 1688 (default)

    Host offering the service: kms.rivnet.org

    The Final srv record looks like this: _vlmcs._tcp.rivnet.org

    You can do all this by using dnscmd (available in Windows7/2008) run this command:

    dnscmd <DNSServerName> /RecordAdd <ZoneName> _vlmcs._tcp.<ForestName> SRV 0 100 1688

    3. Test from a client computer that the SRV record is available in DNS, by running this on a command prompt:

    nslookup -type=srv _vlmcs._tcp.rivnet.org

    You should get an output that points to the DNS record you created in step 1.

    4. Test the client computer can validate his license to the KMS host by running this command from an elevated command prompt:

    cscript %windir%\system32\slmgr.vbs /ato
    
    cscript %windir%\system32\slmgr.vbs /dli

    There should be a line like this:

    KMS machine name from DNS: kms.rivnet.org:1688

    And that’s it with using KMS from any other forest in your own AD. In short:

    1. Add Host record for KMS host

    2. Add SRV record for KMS host

    3. Attempt activation, verify activation was done using KMS host.

    Share

  • Log Battery and Power Levels using Powershell

    Posted on by Ionut Nica

    This is a let’s say lighter post, I came up while trying to compare battery life of my laptop and some buddies of mine. I wanted to know, how fast my battery depleted using different settings, use profile and power saving modes. Then I did some digging around Microsoft’s MSDN site, and I found some interesting WMI classes, that apparently provide a lot of “power related data”. I also wanted to have a way to log this data, and that’s how I ended up learning how to create a new event-log file and write data to it to use that as a log. So this is what I will try to show: get power related data and write it to the Event-Log.

    “Energy” Related WMI Classes

    Here are a few interesting classes I stumbled upon. Some of them are only available under Windows7 probably also Vista, but I’m not sure.

    • WmiMonitorBrightness – gives information about monitor brightness. For example these line give the max. “value” and current value of brightness
    $MaxBrightness = get-wmiobject -class WmiMonitorBrightness -Namespace root/wmi).level | measure-object -Maximum).maximum
    $CrtBrightness = "{0:P0}" -f ((get-wmiobject -class WmiMonitorBrightness -Namespace root/wmi).CurrentBrightness/$MaxBrightness)
    • Win32_PowerPlan – provides information and identifiers about the powerplans defined. In this class ALL powerplans are defined, and just the active plan has an “IsActive” flag attached it, here’s how to get it:
    $powerplan = (Get-WmiObject -Class win32_powerplan -Namespace 'root/cimv2/power' | where {$_.IsActive -eq $true}).ElementName
    • Win32_Processor – gets information about the CPU (I was interested in the CPU load for statistical purposes). This one was pretty easy to find, the value was written in plain sight. Take a look:
    $cpu = (Get-WmiObject Win32_Processor).LoadPercentage
    • Win32_Battery – Provides information about the battery itself (estimated time, remaining load, power status). Running “Get-WmiObject -Class Win32_Battery | gm” take a closer look at these members:
      • BatteryStatus – this will toggle between ’1′ meaning on Battery and ’2′ meaning on AC Power
      • EstimatedRuntime – this will be the number of minutes running on battery, as the OS estimates it, and if you get a very high value (tens of thousands) when you plug the AC Power, it means the battery is charging
      • EstimatedChargeRemaining – percentage-wise representation of battery charge remaining

    Powershell + Event-Log “101″

    I used this battery and power experiment to learn more about working and writing data to the Event-Log. I wanted to create a new “Event-Log” in Windows (windows 7 as you probably know allows for a lot of application logs) and then write events to it. Then at any point you can export the Event-log to csv. The following creates an Event-Log, with the name “BatteryMonitor” from the Information category (for my uses “Source” was not needed but it is a required parameter:

    New-EventLog -Source BattMon -LogName BatteryMonitor -CategoryResourceFile Information

    You can also check if an Event-log is created exists you can use this scriptlet (the answer lies in WMI this time, I didn’t find a cmdlet that does it faster):

    (get-wmiobject -class "Win32_NTEventlogFile" | where {$_.LogFileName -like 'BatteryMonitor'} | measure-object ).count -eq '1'

    Finally here’s how to write to the event-log, a new event. This bit I used in a script to mark the execution of the script in the event-log:

    Write-EventLog -LogName BatteryMonitor -Source BattMon -EventID 65533 -Message 'Starting new Execution of BatteryCharge Monitor Script. The script will pump here CSV values. Values are listed in this order, as CSV: PowerPlan,PowStatMsg,ChargeRemMsg,RemTimeMsg,RAM,CPU,CrtBrightness' -EntryType Information -ComputerName $env:computername -ErrorAction:SilentlyContinue

    So that is about it, as usual I tried to tie all of these scriptlets into a usable script, you can download it from here.

    Share


  • dinamic_sidebar 4 none

©2012 Ionut Nica Entries (RSS) and Comments (RSS)  Raindrops Theme  
View in: Mobile | Standard