Posted by: Preetam | January 29, 2007

Hotfix by Powershell

Script is actually not about Hotfix but more about formatting. How you customize the format of output.

$Hotfix=Get-WmiObject Win32_quickfixengineering
$Bulk=@()
foreach($hotf in $hotfix) {

if($hotf.hotfixid -like “KB*”) {
$Bulk += $Hotf
}
}

$Bulk format-table @{Label=”HotFixID” Expression={$_.HotFixID}},
@{Label=”InstalledBy”Expression={$_.InstalledBy}},
@{Label=”InstalledOn”Expression={$_.InstalledOn}},
@{Label=”Descr” Expression={$_.Description}} -autosize

The way you customize label and more important use of expression

To get more on this, I have two CSV files and my goal is to append data from both these files. I have imported here CSV but I ‘m appending this csv file based on some critiera and that criteria here is Name.

Contents of Name-NC.csv

Name,NC
Shilpa,1
Paatu,1
Anju,2
Mom,3
Papa,3
Preetam,1

Contents of Name-City.csv

Name,City,Age
Preetam,Sng,30
Shilpa,Sng,26
Paatu,Ah,33
Anju,Kh,38
Mom,Ah,56
Papa,Ah,66

So I will check Name in Name-NC and append all the data if the name is present in Name-City.csv.

$NC=import-csv Name-NC.csv
$NCT=import-csv Name-City.csv
$BT =@()
$CT =@()
$Tot =@()
foreach ($Name in $NC) {
# write-host $Name.name `t $Name.NC
$CT =$Name.NC
$BT =$NCT where {$_.name -eq $Name.name}
$BT format-table @{Label=”Name” Expression={$_.name}}, @{Label=”City” Expression={$_.city}},
@{Label=”Age”Expression={$_.Age}},
@{Label=”Printer”Expression={$Name.NC}} }

above code I’ve mark it as bold. actually I got the whole data (again based on name)from one file and only got one detail from other file(Name-city) . Hope you would be able to use this funda somewhere.

Output:

Name City Age Printer
—- —- — ——-
Shilpa Sng 26 1

Name City Age Printer
—- —- — ——-
Paatu Ah 33 1

Name City Age Printer
—- —- — ——-
Anju Kh 38 2

Name City Age Printer
—- —- — ——-
Mom Ah 56 3

Name City Age Printer
—- —- — ——-
Papa Ah 66 3

Name City Age Printer
—- —- — ——-
Preetam Sng 30 1

Technorati tags:

IceRocket tags:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: