Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Tuesday, June 26, 2007

OS Performance monitoring (PowerShell)




Another useful powershell code snippet, monitoring the OS performance...


Code Snippet:

  1. $computer = "."
  2. $perf = get-wmiobject -class "Win32_PerfFormattedData_PerfOS_System" -computer $computer
  3. $uptime=$perf.SystemUpTime/3600
  4. write-host "Performance Stats: "
  5. write-host "Processes", $perf.Processes
  6. write-host "Threads", $perf.Threads
  7. write-host "System Up Time `(hours`)", $uptime.tostring("00.0")
  8. write-host "Alignment Fixups/sec", $perf.AlignmentFixupsPersec.tostring("###,##0.0")
  9. write-host "Context Switches/sec", $perf.ContextSwitchesPersec.tostring("###,##0.0")
  10. write-host "Exception Dispatches/sec", $perf.ExceptionDispatchesPersec.tostring("###,##0.0")
  11. write-host "File Control Bytes/sec", $perf.FileControlBytesPersec.tostring("###,##0.0")
  12. write-host "File Control Operations/sec", $perf.FileControlOperationsPersec.tostring("###,##0.0")
  13. write-host "File Data OperationsPersec", $perf.FileDataOperationsPersec.tostring("###,##0.0")
  14. write-host "File Read Bytes/sec", $perf.FileReadBytesPersec.tostring("###,##0.0")
  15. write-host "File Read Operations/sec", $perf.FileReadOperationsPersec.tostring("###,##0.0")
  16. write-host "File Write Bytes/sec", $perf.FileWriteBytesPersec.tostring("###,##0.0")
  17. write-host "File Write Operations/sec", $perf.FileWriteOperationsPersec.tostring("###,##0.0")
  18. write-host "Floating Emulations/rsec", $perf.FloatingEmulationsPersec.tostring("###,##0.0")
  19. write-host "Percent Registry Quota Used", $($perf.PercentRegistryQuotaInUse/100).tostring("P")
  20. write-host "Processor Queue Length", $perf.ProcessorQueueLength
  21. write-host "System Calls Persec", $perf.SystemCallsPersec
  22. write-host ""


Sample Output:
Performance Stats:
Processes 66
Threads 573
System Up Time (hours) 24.8
Alignment Fixups/sec 0.0
Context Switches/sec 9,857.0
Exception Dispatches/sec 0.0
File Control Bytes/sec 0.0
File Control Operations/sec 0.0
File Data OperationsPersec 1,232.0
File Read Bytes/sec 3,943,044.0
File Read Operations/sec 1,232.0
File Write Bytes/sec 0.0
File Write Operations/sec 0.0
Floating Emulations/rsec 0.0
Percent Registry Quota Used 9.00 %
Processor Queue Length 0
System Calls Persec 51752



If at first you fail, call it version 1.0

Software Inventory in a jiffy (Powershell)

WMIC and WSH are good enough to perform software inventory collection... but the coding's just a little bit longer...

Well to query wmi is not a big task, it's formatting the output that will get your code extra few lines of unnecessary vbtab's and vbcrlf's.

Install Windows Power Shell, then it will take only three (3) lines of codes to get it running and the output is as lovely as a well defined tabular echo's on your console.

Code Snippet:

  1. #Software Inventory
  2. $computer = "."
  3. $prod = Get-WmiObject -class "win32_product" -computer $computer
  4. $prod | sort name | ft Name, Version, Vendor, Installdate -a
Just save it as *.ps1 then execute it with powershell...

If at first you fail, call it version 1.0

Monday, June 18, 2007

ADSI Kixtart UDF for Citrix login

Having a mixed mode environment gave us a lot of hassle when logging in to NT and querying group membership in AD... Specially in our case, we have nested OU's...

Ifmember.exe is useful for this problem but it does cause a slight delay in the login process and the users are complaining on the slow login session, some couldn't wait and cancels the connection... catastrophic experience ends up as a global complain... hmmm... some people are just impatient...

So to be able to execute an ADSI query through kix login script the function below can be inserted anywhere in the login script to perform InGroup query... or this can fully replace the built in InGroup function in kixtart.


Code Snippet:

  1. Function fnInGroupAD($sGroup,Optional $bComputer)
  2. Dim $objSys,$objTarget,$aMemberOf,$sMemberOf
  3. $objSys = CreateObject("ADSystemInfo")
  4. $objTarget = GetObject("LDAP://"+Iif($bComputer,$objSys.ComputerName,$objSys.UserName))
  5. $aMemberOf = $objTarget.GetEx("memberOf")
  6. For Each $sMemberOf in $aMemberOf
  7. If InStr($sMemberOf,"CN="+$sGroup+",")
  8. $fnInGroupAD = Not 0
  9. Exit
  10. EndIf
  11. Next
  12. $fnInGroupAD = NOT 1
  13. EndFunction

If at first you fail, call it version 1.0

Saturday, June 16, 2007

Double System State Backup

We have a scheduled System State backup running on our Web Server.

To save the extra cost of software and hardware backups, I wrote a script that copies the System State backup to folders named after the date the backup was performed.

With this, it's easier for us to restore backups for particular days without having too much hassels.

Code Snippet:

'---------------------------------------------
' System State Backup Configuration
' Logfile naming, Source and Destination folders
'---------------------------------------------

  1. Const ForReading = 1, ForWriting = 2, ForAppending = 8
  2. Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
  3. Dim SysBakName, CurDate, SysBakSrc, SysBakRoot, mssg
  4. Dim hTime, mTime, sTime, nTime
  5. 'System State Backup Filename
  6. SysBakName = ""
  7. 'System State Backup Source Path
  8. SysBakSrc = ""
  9. 'System State Backup Destination Root
  10. SysBakRoot = ""
  11. 'System State Log File
  12. SysBakLog = ""
  13. hTime = Hour(Time)
  14. mTime = Minute(Time)
  15. sTime = Second(Time)
  16. if hTime < 10 then
  17. hTime = "0" & hTime
  18. end if
  19. if hTime = "00" then
  20. hTime = "24"
  21. end if
  22. if mTime < 10 then
  23. mTime = "0" & mTime
  24. end if
  25. if sTime < 10 then
  26. sTime = "0" & sTime
  27. end if
  28. nTime = hrs & hTime & ":" & mTime & ":" & sTime


'----------------------------------------
' System State Backup Modules
'----------------------------------------


  1. Sub BackDest(CurDate)
  2. Select Case CurDate
  3. Case "MON"
  4. BakDestination = "Monday"
  5. Case "TUE"
  6. BakDestination = "Tuesday"
  7. Case "WED"
  8. BakDestination = "Wednesday"
  9. Case "THU"
  10. BakDestination = "Thursday"
  11. Case "FRI"
  12. BakDestination = "Friday"
  13. Case "SAT"
  14. BakDestination = "Saturday"
  15. Case "SUN"
  16. BakDestination = "Sunday"
  17. Case Else
  18. mssg = "Unable to determine backup destination path!"
  19. WriteMssg mssg
  20. End Select
  21. CopyBak BakDestination
  22. End Sub

Start the backup process:

  1. Sub StartBackup()
  2. CurDate = (FormatDateTime(Date(),1))
  3. CurDate = UCase(Trim(CurDate))
  4. CurDate = Left(CurDate, "3")
  5. BackDest CurDate
  6. End Sub
  7. Sub CopyBak(BakDestination)
  8. Dim filesys, SysBakFile, SysBakSrcFull
  9. SysBakSrcFull = SysBakSrc & "\" & SysBakName
  10. Set filesys = CreateObject("Scripting.FileSystemObject")
  11. Set SysBakFile = filesys.GetFile(SysBakSrcFull)
  12. BakDestination = SysBakRoot & "\" & BakDestination &amp; "\"
  13. SysBakFile.Copy(BakDestination)
  14. mssg = SysBakSrcFull & " copied to " & _
  15. BakDestination & SysBakName &amp;amp;amp;amp;amp; _
  16. " - " & nTime
  17. WriteMssg mssg
  18. End Sub
Write the process into a log file:
  1. Sub WriteMssg(mssg)
  2. Dim fso, f, ts
  3. Set fso = CreateObject("Scripting.FileSystemObject")
  4. If Not fso.FileExists(SysBaklog) then
  5. fso.CreateTextFile SysBakLog
  6. End If
  7. Set f = fso.GetFile(SysBakLog)
  8. Set ts = f.OpenAsTextStream(ForAppending, TristateUseDefault)
  9. ts.WriteLine mssg
  10. ts.Close
  11. 'mssg = ""
  12. End Sub
  13. Sub StartLog()
  14. Dim TimeNow
  15. TimeNow = (FormatDateTime(Date(),1))
  16. mssg = "System State Backup started on " & _
  17. TimeNow & " - " & nTime
  18. WriteMssg mssg
  19. End Sub

If at first you fail, call it version 1.0

Tuesday, June 05, 2007

1,000 eCPM can earn you only $0.01

Just a script that does nothing but loads a page repeatedly...
Hmmm... might be useful or not useful...
Can It be used to accumulate eCPM?

Totally up to you...

psURL = "Your desired url..."
psURL2 = "Second desired url..."
Dim x
Dim IE

x=0
Set IE = CreateObject("InternetExplorer.Application")

With IE
.menubar=0

.toolbar=0
.statusBar=0
.navigate psURL
.visible=0
.resizable=1
Do while .busy
Loop

Do While x < "desired amout of page loads"
WScript.Echo Now & " - Count Load: " & x
.navigate2 psURL
WScript.Echo Now & " - Loaded: " & psURL
Do while .busy
Loop

x =x + 1
WScript.Echo Now & " - 15sec sleep..."
WScript.Sleep(15000)
.navigate2 psURL2
WScript.Echo Now & " - Loaded: " & psURL2
Loop
End With




Thursday, May 24, 2007

SMTP using .NET Classes (Powershell)

A great Powershell code that you can use as a template for SMTP mail with authentication.

Easily extendible for Web development.


Scriptlet:
###############################################
$mail = new-object System.Net.Mail.MailMessage

#set sender email address
$mail.From = new-object System.Net.Mail.MailAddress("mymail@mydomain.com");

#set the recepient email address
$mail.To.Add("touser@domain.com");

#set the email subject
$mail.Subject = "";

#set the content

$mail.Body = "";

#send the message
$smtp = new-object System.Net.Mail.SmtpClient("mydomain.com");

#set the username and password properites on the SmtpClient for authentication
$smtp.Credentials = new-object System.Net.NetworkCredential("username", "password");

#send it
$smtp.Send(mail);


#Voila!
###############################################


Monday, May 21, 2007

Powershell Lynx Project

Those were the days of Lynx when you used to browse the web as text thru the console...

My previous blogs were about scripting with Yahoo api search services. Well, since blogging at work is not really encouraged, I have decided to create my powershell version of Lynx. Since I'm scripting most of the time due to adHoc script requests, my boss won't even realize that I'm actually blogging... hehehe...

For starters, a scriptlet that navigates IE:

$ie = New-Object -com InternetExplorer.Application
$ie.Visible=$true
$ie.MenuBar=$false
$ie.ToolBar=$false
$ie.StatusBar=$false
$ie.Navigate("http://26thgstreet.blogspot.com")


Visibility of IE can be set to false later after I finalized the function to get the HTMLDocument.Innertext and write it to the console.

I will aso have to create a function to accept console arguments to be posted to the HTMLDocument so that I can update my blogs...

Stay tuned for the script updates.



Free PowerShell Book

My msdn feeds shed some light on my powershell addiction after receiving the good news.

It's finally out, thanks to Frank Koch.


The subtitle is "An introduction to scripting technologies for people with no real background knowledge". Read more.

Get it here.