Showing posts with label oneliners. Show all posts
Showing posts with label oneliners. Show all posts

Tuesday, January 5, 2010

Determining OS Bit-Level

With x64 computing quickly overtaking 32-bit environments a vast need has been created to know what bit-level on which a given OS is running.  Welcome back PowerShell and WMI

ONELINER

(gwmi win32_computersystem).SystemType
 code: copy : expand : collapse

EXPLANATION
Opening the Win32_ComputerSystem class gives us very useful information such as the mfg model, computer name, domain,...the list goes on.  What we are looking for is the OS architecture (bit-level).  We're able to retrieve this throught the SystemType property.  On a x64 OS it will return "x64-based PC", and "X86-based PC" for a 32-bit OS.

Wednesday, December 2, 2009

GetMotherboardModel


ONELINER

gwmi win32_computersystem | select Manufacturer,Model | fl "
 code: copy : expand : collapse

EXPLANATION
Using WMI (of course) we can pull a great amount of information about the physical computer equipment. Items such as amount of memory, computer name... many others. We're interested in getting the motherboard model and manufacturer. Lets gather all information, and only display these two items using select. For a full list of what is available, try this: gwmi win32_computersystem | select *

ALIASES
GWMI = Get-WMIObject

Tuesday, December 1, 2009

GetLogicalDisk

PowerShell can grab any logical drive from a remote computer using WMI and return a specific type.

ONELINER

gwmi Win32_LogicalDisk -Computername "%Remote_Computer%" -Filter "DriveType=3"

 code: copy : expand : collapse

EXPLANATION
Using Get-WMIObject we will connect to the WMI Class Win32_LogicalDisk and filter for only Local Disk (DriveType=3).

NOTE
You can filter by any drive type as listed below 0 = Unknown
1 = No Root Directory
2 = Removable Disk
3 = Local Disk
4 = Network Drive
5 = Compact Disc
6 = RAM Disk

ALIASES
GWMI = Get-WMIObject

Tuesday, October 6, 2009

KillProcess

Who needs to kill processes on occasion?  Everyone, right?  Well, I do at least.  Here comes a oneliner.

ONELINER

gps | ? { $_.Name -match "%APPNAME%" } | % { $_.Kill() }

PEICE BY PEICE
gps
Alias for Get-Process.  Gathers all processes currently running on the local machine.  This can also be used to kill remote processes by adding -Computer %COMPUTER% after gps.

? { $_.Name -match "%APPNAME%" }
Input the app name to kill in place of %APPNAME%.  This will only return applications with the given name.
NOTE:  %APPNAME% must not contain the file extension.  i.e. EXCEL instead of EXCEL.EXE

% { $_.Kill() }
The Kill method of Get-Process shuts down the particular instance that is being handled.  Therefore, this will kill each instance that is passed through the pipe.

ALIASES
gps = Get-Process
? = Where-Object
% = ForEach-Object

Thursday, September 17, 2009

Random number generator

PowerShell does not have a native randomizer, but it's a good thing that PowerShell does rely on .NET.  .NET, however, can be tricky to deal with some times.  In this case, it is not.  System.Random is a very easy to use class.  We simply ask for the Next number.

ONELINER

0..9) | % { Write-Host (New-Object System.Random).next(10) ; Start-Sleep -Milliseconds 1 }


PEICE BY PEICE
(0..9)
writes back through the pipe 0-9 sequentially

% { write-host (new-object System.Random).next(10)
Since we're not passing $_ to anything the numbers 0-9 dissappear, but allow us to continue for each number.  a new .NET object is created for the System.Random class.  Calling the .next method and passing it an int32, we will be returned a  nonnegative random number less than the specified maximum; in this case, that would be 10.

; Start-Sleep -Milliseconds 1
; essentially is a way to seperate individual commands on a single line.  Start-Sleep does just that.  It causes PoSh to sleep for the given count in milliseconds.  This can also be specified in seconds.  Most computers are so quick now that without this momentary - although unnoticeable - pause System.Random will not be able to create a new number.  The algorythem used is based on the current date and time counted to the nearest millisecond.

NOTE
This will generate 10 random numbers between 1 & 10.  To create more or fewer change the initial (0..9) to your specified range: ex (4..6) would generate 3 numbers by passing 4, then 5, then  to the host.  .next(10) will specify a number UP TO (not including) 10.  A specific range can be used for generation by seperating the numbers with a comma:ex .next(16,38) would generate a number between, but not including 16 & 38.

ALIASES
% = ForEach-Object

Tuesday, September 15, 2009

Trim a string to a specific number of characters

While creating AD groups I ran into the problem of having group names over 64 chars.  AD does not like this and errors during the creation.  So, what was my answer?  Trim the group name string to the 64 char limit.  Here is my one-liner to trimg a string down to n' chars.
 
ONELINER

"Your_Text_To_Trim" | % { $_.TrimEnd($_.Substring(10))}

PEICE BY PEICE
"Your_Text_To_Trim"
This you can substitute any string here.  That could be simple text or or a string variable.

| %
All piped elements (using the | is called piping) must have an operator to create a new object.  Foreach-Object (%) will create the $_ object that we will use

$_.TrimEnd
TrimEnd() is a string method for removing specific text from the end of a string.  This does take specific text, not wildcards.

$_.Substring(10)
Here is were we find the text to remove.  Substring() is another string method that extracts the characters after the given position.  In this case "To_Trim"

EXPANDED
$text = "Your_Text_To_Trim"
$substring = $text.Substring(10)
$trim = $text.TrimEnd($substring)
$trim
 code: copy : expand : collapse

This could be done through the expanded code.  Notice that we do not need to use the Foreach-Object cmdlet.  Once again, we had to use this to create the Object $_ to manipulate.

NOTE
When using the TrimEnd() method it will truncate all spaces and underscores if they are the last character of the string AFTER the trim is completed.  You will probably notice the output above would be "Your_Text" instead of "Your_Text_"

ALIASES
% = Foreach-Object

Monday, September 14, 2009

Get remote shares

Who doesn't need to check existing shares on a remote workstation or server?  The President?  Monks?  That creapy guy who always seems to be watching you leave after work?  They may not need to, but we in IT do.  Since PowerShell has given us direct access into WMI it is very simple.

ONELINER

GWMI win32_share -computer %COMPUTER_NAME% | ? {$_.name -notlike "*$"} | fl
 code: copy : expand : collapse

PEICE BY PEICE
GWMI win32_share -computer %COMPUTER_NAME%
Using PoSh native WMI through Get-WmiObject we are able to connect to the Win32_Share namespace:  the namespace containing...Network Share Configurations!!

? {$_.name -notlike "*$"}
We want to filter out all Admin shares.  Leave this section out to show all shares.

fl
Format the output as a list.  Descriptions could be truncated when output as a table.

ALIASES
GWMI = Get-WMIObject
? = Where-Object
fl = Format-List

Wednesday, September 9, 2009

Return the last n' lines from a text file.

I am a PoSh fan (PowerShell for those who are unfamiliar) and hence, have decided to challenge myself, to stretch my knowledge & ability, by posting daily one liners.  Here is my first:

In my world logging is king for any activity we do, but I hate opening file after file to read the last few lines.  Instead, I'd rather have PoSh do it for me:

EXPANDED

$list = Get-Content yourfile.txt
$Count = $list.Count
foreach ($line in $list) {
  $ReadCount = $line.ReadCount
  if ($ReadCount -gt ($Count -3) {
    $line
  }
}
CODE: [Copy:Expand]
PEICE BY PEICE
$list = Get-Content yourfile.txt
We need to open our text file and put each line into an object to parse through.

$Count = $list.Count
Count each line to find our total.

foreach ($line in $list)
Parse through each line.

$ReadCount = $line.ReadCount
Find the line number we are currently on.

if ($ReadCount -gt ($Count -3)
This line looks for the last 3 lines based on the line number and the total number of lines minus 3. Replace "3" with with the number of lines you'd like to grab.

$line
Writes the line to the console

ONELINER
foreach ($line in ($list = gc yourfile.txt)) { if ($line.ReadCount -gt ($list.Count - 3)) { $line } }
TaDa!! A oneliner to grab the last n' number of lines from a given text file.

Notice that $list = gc yourfile.txt is still in place. In order to count the lines we have to use the method .Count. Without defining $list there is no quick way to gather the total number of lines in the file.

ALIASES
gc = Get-Content