Quantcast

Maximum PC

It is currently Sat Dec 27, 2014 9:32 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Powershell Scripts
PostPosted: Thu Jan 22, 2009 5:41 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
Coming out of a discussion we had in Alt.OS I volunteered to share a powershell script that I wrote along with some sample output. This is one of my first powershell scripts, actually it is one that I ported over from VB Script and tweaked a little. It is also one of the ones I use the most.

Powershell is becoming all the things that cmd.exe was not. In my opinion, and I usually don't like Microsoft in general, this product is a huge step in the right direction. The command shell has traditionally been very weak and for any true scripting to be done it needed to be written in a complex, and poorly documented, VB Script that interacted with the WMI functions in Windows. Powershell has simplified the syntax and allowed flexibility so that experienced scripters/programmers can be comfortable, while at the same time not alienate the beginning scripter.

The following takes the contents of a file and loops through the list of machine names giving a profile of each machine. This makes it very easy to make sure no machines get missed when service pack updates and when evaluating machines for attrition it is very handy to have this info on hand.

[code]
# Get a formatted profile of a pc

$arrComputer = Get-Content c:\scripts\computernames_.txt
#$arrComputer = (".")
Write-Host "======================================"

foreach ($pc IN $arrComputer) {
ping $pc -n 2 -w 1 > NULL
if ($?)
{
$objWMIProcessor = Get-WmiObject -Class Win32_Processor -Namespace root\CIMV2 -ComputerName $pc
#if ($? -eq "True") {break}
$objWMIComputer = Get-WmiObject -Class Win32_ComputerSystem -Namespace root\CIMV2 -ComputerName $pc
$objWMIDisk = Get-WmiObject -Class Win32_LogicalDisk -Namespace root\CIMV2 -ComputerName $pc
$objWMIOperatingSystem = Get-WmiObject -Class Win32_OperatingSystem -Namespace root\CIMV2 -ComputerName $pc
# the notation for formatting numbers comes from .NET
# "{0:N2}" can be broken down as follows:
# The initial 0 (the one that comes before the colon) represents the index number of the item to be formatted.
# For the time being, leave that at 0 and everything should work out just fine.
#
# The N represents the type of format to be applied; in this case, the N is short for Numeric.
#
# The second 0 (the one after the N) is known as the “precision specifier,â€


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2009 5:22 pm 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
Here is another quickie I wrote to do some testing. I am working on a project that involves retail scales (like the ones in your corner Deli) and a point of sale system. The barcodes on the packages will contain PLU data and an embedded price; our goal is to calculate the reduced price based on only having the regular retail and extended price information available.

Below is what I used to test over 50 labels, flow control can be turned on/off easily in this example.

Code:
#
# Take a final retail price and divide that by the retail ($3.89 for example)
# to obtain the product weight
# Conventionally round the weight and multiply by the promotional price
# to calculate the reduced retail
[float]$retailPrice = Read-Host "Retail Price:  "
[float]$promoPrice = Read-Host "Promo Price:   "
do
{
[float]$retail = Read-Host "Final Price? "
$weight = ($retail / $retailPrice)
Write-Host -NoNewline "Product Weight: "
Write-Host ("{0:N4}" -f $weight)
$fweight = ("{0:N2}" -f $weight)
Write-Host -nonewline "Mult Factor:    "
write-host $fweight
Write-Host -NoNewline "Final Price:    "
Write-host ("{0:N2}" -f ([float]$fweight * $promoPrice)) `n`n
#$another = Read-Host "Another Calculation?  "
}while ("y" -match "y")


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 9:22 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
This is some slick stuff!

I need to get my shit together and get WPS installed.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 11:26 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
This one sets the Job Title field in Active Directory equal to the Job Description field. Title shows in the Outlook address book but is not in the most convenient place in the profile.

This script uses the Active Directory management tools for power shell provided by Quest Software they are free to use.

These three lines will process everyone in the domain. It can be made to run one OU at a time by replacing "Get-QADUser" with $(Get-QADUser <OU name>) . I also threw in a -whatif so that if anyone runs it without thinking no harm, no foul.

Code:
ForEach ($user in Get-QADUser )
{
      Set-QADUser $user -ObjectAttributes @{Title = (Get-QADUser $user).Description} -whatif
}


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.