≡ Menu

Powershell: Check laptop battery status

In this post, I will show you how to pragmatically query battery status in laptops using powershell. It is important to check batter status before performing some critical tasks like patch updates or service pack updates via some automation(s). If the laptop charging drains down in middle of the upgrade, it can corrupt the operating system.

You can use below one liner to check the percentage of charging left in the battery:

(Get-WmiObject -Class Win32_Battery).estimatedchargeremaining

Similarly, if you want to how many more minutes this charging comes, you can use below code:

(Get-WmiObject -Class Win32_Battery).EstimatedRunTime

Hope this helps and you will have crash free upgrades on laptops.

{ 3 comments }

Add color to your powershell Codes

I always to wanted to make my powershell codes look beautiful in my blog but I didn’t find any tool which can help me in this regard. I used Syntax Highlighter for some days but my blog readers expressed concerns in using the code that I generated using Syntax Highlighter . So I removed it completely and ran my blog with just black and white code for some days.

I am a follower of RaviKanth chagati blog and I liked the way he colorizes his powershell code in posts. I approached him for help and he is kind enough to offer. Surprisingly, the solution is within powershell region and I no need to use any external tools to add color to my code.

I thought of making a post about this so that other bloggers who post about powershell like me can also benefit from this.

So, let us go ahead and see how we can add color to powershell code.

Launch your “Powershell Integrated Scripting Environment” Console which we use for writing powershell code. You quickly launch it by running “powershell_ise” from RUN command.

Once opened, import ISEPACK powershell module and execute the code in ISE by pressing “F5”. Now you will notice “Add-ons” tab in Powershell_ise.

Now paste your working code(or write directly in powershell_ise editor) in editor and press “Ctrl+Alt+Shift+C”(it does nothing but triggering copy-coloredHTML function in isepack module) and this will generate HTML equivalent of the code you have written in powershell ISE and make the HTML code available in clipboard. You can post paste operation anywhere to see the HTML code.

If you like GUI mode, you can do the code generation by triggering copy-coloredHTML function like shown below.

Once the code is available, let us move to content manger ( I use “wordpress”). In HTML portion of new post form, paste the HTML code that you generated and switch to Visual tab to view the preview.

I did above yesterday for 3 posts and output is very nice. You can view them below.

https://techibee.com/powershell/powershell-change-monitor-brightness/1022

https://techibee.com/sysadmins/powershell-minimize-all-windows/1017

https://techibee.com/scripting/powershell-get-cpu-architecture-on-windows-7-computer/1012

{ 0 comments }

Powershell: Change monitor brightness

Recently I came across a method using which you can control the monitor brightness. This method uses the WMI class named “WMIMonitorBrightnessMethods” which lies under rootwmi. This WMI classes offers various other functions using which you can manage various parameters of monitor brightness

Here is the example code:

function Set-MonitorBrighness {
[CmdletBinding()]
param (
[ValidateRange(0,100)]
[int]$brightness
)            

$mymonitor = Get-WmiObject -Namespace rootwmi -Class WmiMonitorBrightnessMethods
$mymonitor.wmisetbrightness(5,$brightness)
}
Set-MonitorBrighness -brightness 1

 

This sets the monitor brightness level to 1%. The first parameter of the “wmisetbrightness” function is timeout which defines the time it takes to switch to the brightness one you have set.

{ 2 comments }

PowerShell: Minimize all windows

Have you ever got a requirement to minimize all windows on the desktop using powershell script? It is helpful when you want to give a pop-up message to user which should draw their immediate attention.

I came across this little tip while exploring shell.application com object. It has other useful functions like undominimizeall, cascade windows, and many other explorer functions.

$shell = New-Object -ComObject "Shell.Application"
$shell.minimizeall()

You can also undo minimize all windows by using below code.

$shell = New-Object -ComObject "Shell.Application"
$shell.undominimizeall()

To see the clear functionality, add some delay between minimizeall() function and undominimizeall() function.

$shell = New-Object -ComObject "Shell.Application"
$shell.MinimizeAll()
start-sleep -Seconds 5
$shell.undominimizeall()

Hope this helps… You can explore more functions of shell.application object by using below code snippet.

New-Object -ComObject "Shell.Application" | gm | select Name, MemberType
{ 6 comments }

I wrote on the similar topic before. My earlier attempt was to determine OS architecture but this time, I am going to talk about querying CPU architecture using powershell code. It is needless to explain the difference between these two things. 🙂

So, below is the code which helps you determine CPU architecture of a computer.

function Get-CPUArchitecture {
if (($ENV:Processor_Architecture -eq "x86" -and (test-path env:PROCESSOR_ARCHITEW6432)) -or ($ENV:Processor_Architecture -eq "AMD64")) {
write-host "Detected 64-bit CPU architecture"
} elseif ($ENV:Processor_Architecture -eq 'x86') {
write-host "Detected 32-bit CPU architecture"
} else {
write-host "Unable to determine CPU architecture"
}
}

Hope this helps…

{ 5 comments }

Group Policy Guide for Beginners

Are you new to Group policies? Looking for a document to get basics? Well Microsoft has recently released a document which helps beginners to understand basics of GPOs. This document explains various components of GPOs and their usage.

You can download this document from here

{ 0 comments }

Windows 7 and Windows 2008 R2 are having real good amount of new GPO settings that IT administrators can manage on their Client and server computers. These includes, controlling the USB devices, configuring firewall rules, and many more. One can go through gpedit to get a understanding of all the settings but that is time consuming.

I came across these nice downloads from Microsoft which describes significance of each settings, their options, and the corresponding registry keys if any. Using this sheet you can quickly search for a policy and understand its functionality.

Download these reference guides from MS download center

Happy Learning….

{ 0 comments }

 

Backup Procedure:

  1. Go to Start -> Run and type “%appdata%MicrosoftSticky Notes” and Click “OK”
  2. This opens the location of Sticky Notes file, “StickyNotes.snt”, where all the sticky notes data is stored.
  3. Just take a copy of this file and save it somewhere safe.

Procedure to Restore:

Restoring Sticky notes is as good as copying back the file that you saved in safe location. Just copy the backed up file to “%appdata%MicrosoftStickyNotes” folder.

Hope this helps.

{ 0 comments }

Earlier I wrote a small code to get the CPU utilization of computer using powershell and performance counters. Recently a user posted a question to know the procedure to monitor CPU utilization and get a alert when CPU utilization exceeds a given threshold value.

So, I am writing this article to share the code for monitoring CPU utilization of a computer and report via email when it exceeds a given value.

At the beginning of the script there are a few variables which you need to adjust according to your requirement. They are self descriptive, so I feel no explanation is required. Also you should change the “From”, “To”, and “Server” parameters of “Send-MailMessage” cmdlet in script to receive email. Feel free to write to me if you have any questions.

Save the below code to a file and name it as “cpu-monitor.ps1” and run it on the computer you want. Script checks the CPU utilization for every 5 seconds for three times and reports if it finds CPU utilization is greater than given threshold value every time it measured.

$repeat_count = 3
$cpu_threshold = 85
$sleep_interval = 5
$hit = 0
foreach($turn in 1..$repeat_count) {
$cpu = (gwmi -class Win32_Processor).LoadPercentage
#write-host “CPU utilization is Currently at $cpu`%”
If($cpu -ge $cpu_threshold) {
$hit = $hit+1
}
start-sleep $sleep_interval
}

if($hit -eq 3) {
write-host “CPU utilization is over threshold”

Send-MailMessage –From cpumonitor@localdomain.com –To toaddress@yourdomain.com –Subject “CPU Utilization is more than $cpu_threshold`%” –Body “CPU Utilization is more than $cpu_threshold`%” –SmtpServer smtpserver.domain.com
} else {
write-host “CPU utilization is below threshold level”
}

{ 18 comments }

[Guest Post]

This guest post was provided by Lee Munson on behalf of GFI Software Ltd. GFI is a leading software developer that provides a single source for network administrators to address their network security, content security and messaging needs. More information: GFI vulnerability scanning software.

All product and company names herein may be trademarks of their respective owners.

Nowadays many companies will scan their corporate networks in order to identify security issues. A vulnerability scanner may be employed once per year or, preferably, more often, and can be run either in-house or by a third party.

Typically, the results that come back from the vulnerability scan are used to identify and rectify any security concerns, as well as to remain compliant with the company’s own internal policies and procedures.

The Regulations

There is a whole raft of regulations that either do, or could, have a significant effect on information processing and security. The key regulations for the USA, Europe and the United Kingdom –include:

  • Payment Card Industry Data Security Standard (PCI DSS)
  • Health Insurance Portability Act 1996 (HIPAA)
  • Sarbanes-Oxley Act 2002 (SOX)
  • Gramm-Leach-Bliley Act 1999 (GLBA)
  • Family Educational Rights And Privacy Act (FERPA)
  • The EU Data Protection Directive
  • The EU Directive On Privacy And Electronic Communications
  • The Computer Misuse Act 1990
  • UK Data Protection Act 1998

Many of the above regulations either require, or at the least imply the need for, regular vulnerability scanning across the organisation’s network.

The Consequences Of Non-Compliance

There are, of course, many possible penalties associated with non-compliance of the regulations listed above. The first such penalties that you would likely think of would be immediate, legal and financial in nature but there would also be a longer-term concern as well.

In business, reputation is everything, and a company that is not compliant with the regulations will suffer as a result, either through that knowledge becoming known to prospective partners and customers, or as a direct consequence of a breach occurring.

Vulnerability Scanning and Compliance

By using a vulnerability scanner – that also includes the ability to patch vulnerabilities and provide audits – on a frequent basis, a company can detect security threats before they can affect the network. This is especially important in an environment where hardware and users are changing regularly.

Network security is a fluid process that changes all the time with new threats emerging on a regular basis. A vulnerability scanner is an essential tool for combating these new threats as these would be updated regularly by their vendors in pretty much the same way that antivirus programs are updated with new virus definition files.

Another benefit to running regular vulnerability scans is that it helps with security audits and, therefore, helps you meet compliance with the regulations mentioned above.

In the future, the need for compliance is only going to grow due to the fact that there will undoubtedly be a raft of new regulations being released and, also, because the existing regulations will almost certainly begin to encompass more and more companies, regardless of their size.

A vulnerability scanner is your virtual security consultant and can aid your organisation to pass all the appropriate legal audits as well as your company’s own internal policies, protecting all your customers’, partners’ and employees’ data and privacy in the process.

 

{ 2 comments }