Powershell and HP Sim

I just ran across something that I found interesting.
If you use HP/Compaq servers and you have installed the Management agents,
You will find a Name Space called rootHPQ that has 363 classes associated with it.
 
Basically, every thing the HP Management agents know about the server is available.
 
I found it by using the Scriptomatic PowerShell Version by Ed Wilson.
Avilable here:
 
While I have not gone through every Class, I think this opens up a whole new world to us scripting maniacs.
 
Enjoy!

Short File and Path Names


I know you can quote your strings to get around long file names and even longer path names. It also helps with spaces in the names.
However, getting the quotes in the right places is often a pain, so I use short file names whenever I can. One of my customers was worried that a new file or directory could be created and change the number of my file or directory.

For example;

10/21/2008 12:55 PM <DIR> CONFIR~4 ConfirmTHEOrder
10/17/2008 09:39 AM <DIR> CONFIR~3 ConfirmTHEClub
10/17/2008 09:39 AM <DIR> CONFIR~2 ConfirmTHEClubUS
10/21/2008 09:25 AM <DIR> CONFIR~1 ConfirmTHEOrders

What would happen if another directory was created between THEOrder and ConfirmTHEOrders?
So I did a little looking around and found this.

Set objFile=objFSO.GetFile(strPath1)
strCMD1="Wscript " & objFile.ShortPath


That’s right, I put the fully quallified path in strPath1 and it returns the short path name.
Too cool.
Here is the sample I found by Jeffery Hicks for wrapping up powershell in a VBScript:

Dim objShell,objFSO,objFile
Set objShell=CreateObject("WScript.Shell")
Set objFSO=CreateObject("Scripting.FileSystemObject")
'enter the path for your PowerShell Script
'strPath="e:documents and settingsjhicksmy documentsscriptsposhGet-DiskSize.ps1?
'verify file exists
If objFSO.FileExists(strPath) Then
'return short path name
set objFile=objFSO.GetFile(strPath)
strCMD="powershell -nologo -command " & Chr(34) & "&{" &_
objFile.ShortPath & "}" & Chr(34)
'Uncomment next line for debugging
'WScript.Echo strCMD
'use 0 to hide window, True makes it wait until it's finishes.
objShell.Run strCMD,0, True
Else
'Display error message
WScript.Echo "Failed to find " & strPath
WScript.Quit
End If


In case you are wondering; the Chr(34) puts a double quote in the string.
Yet another way of dealing with quotes.

Or in Pure Powershell:

$a = New-Object -ComObject Scripting.FileSystemObject 
$f = $a.GetFile("C:\Program Files\Internet Explorer") 
$f.ShortPath

Sending mail with PowerShell

The process to send mail is pretty simple.
It’s the attachment process that gets a little complicated.
 
I found this at http://blogs.inetium.com/blogs/mhodnick/archive/2006/11/29/powershell…

Which answered the question, How do I attach a file?

[System.Net.Mail.Attachment] $attachment = new-object
System.Net.Mail.Attachment $file

It is necessary to cast the $attachment variable to type
System.Net.Mail.Attachment, since new-object (in this instance)
returns a REFERENCE to an object of that type, not an ACTUAL OBJECT of
the type. But $msg.Attachments.Add($attachment) requires an actual
object. Hence the need for the cast.

So, here is my working code, enjoy!
 
#<————– Begin —————————————–>
$file = "r:dailylog.txt" <—-the file you want to attach
$sender = "joe@work.com"
$recipient = "susie@work.com"
$server = "mail.smtp.com"
$subject = "Sending a File " + [System.DateTime]::Now
$body = "I’m sending a file!"
$msg = New-Object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
$attachment = New-Object System.Net.Mail.Attachment $file
$msg.Attachments.Add($attachment)
$client = new-object System.Net.Mail.SmtpClient $server
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$client.Send($msg)
 $attachment.Dispose()
#<——————–End—————————————–>

from Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar

To add 2 recipients
$msg.To.Add("te@domain.com")
$msg.To.Add(te@domain.com)
In the same way add cc and bcc:
$msg.cc.Add(…)
$msg.bcc.Add(…)

To compare and contrast here is basicaly the same thing in vbScript;
 
‘<————Begin———————————————–>
 
adminmail = "somebody@somewhere.com"

 smtp = "mail_server name or IP"
 Set mailing = CreateObject("CDO.Message")
 Set wshNet = CreateObject("WScript.Network")
 
 mailing.From = ("fromSomeone@somewhere.com")
 mailing.To = adminmail
 mailing.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 mailing.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtp
 mailing.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 mailing.Subject = header & "Data Transfer Success logs"
 sFullProductionMessage = "See attached file…"  & vbcrlf
 mailing.TextBody = sFullProductionMessage
 mailing.Configuration.Fields.Update
 mailing.AddAttachment "R:dailylog.txt"
 mailing.Send
 On Error Goto 0
 
‘<—————————- END ———————————–>
 
What if you wanted the Attachment to be the body text?
 
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
‘Open the file for reading
Set f = fso.OpenTextFile("C:SendLogsdailylog.txt", ForReading)
‘The ReadAll method reads the entire file into the variable BodyText
BodyText = f.ReadAll
‘Close the file
f.Close
Set f = Nothing
Set fso = Nothing
 
And in your message function or sub or whatever;
 
 mailing.TextBody = BodyText
 
How about CC?
 
 
 If you want to send an SMS (text) msg to a phone, just use the phone number and the appropriate SMTP gateways.

You can use the BorderAround() Method to set a border on a range:


Yet another fine example from Kiron on the Powershell discussion board.
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/threads
You can use the BorderAround() Method to set a border on a range:

# some Excel Constants used in the sample 
# for more Constants you can look here
# http://cid-c2db05eefa6c21a1.skydrive.live.com/browse.aspx/Public 
# line styles

 $xlLineStyleNone = -4142
 $xlContinuous = 1
 $xlDash = -4115
 # line weight
 $xlThin = 2
 $xlMedium = -4138  $xlThick = 4
 # color index
 $xlColorIndexBlue = 5 # <-- depends on default palette
 # border index
 $xlEdgeLeft = 7
 $xlEdgeTop = 8  $xlEdgeBottom = 9
 $xlEdgeRight = 10
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
 $xl = new-object -c excel.application
 $wb = $xl.workbooks.add()
 $sh = $wb.worksheets.item('Sheet1')
 $xl.visible = $true
 $range1 = $sh.range('b2:d4')
 $range2 = $sh.range('f2:h4')
 $range3 = $sh.range('b6:d8')
 $range4 = $sh.range('f6:h8')
 # thin blue border on a $range1; [void] suppresses the method's output
 [void]$range1.borderAround($xlContinuous,$xlThin,$xlColorIndexBlue)
 # set a medium border on $range2 (automatic color index)
 [void]$range2.borderAround($xlContinuous,$xlMedium)
 # medium blue dash border on $range3
 [void]$range3.borderAround($xlDash,$xlMedium,$xlColorIndexBlue)
 # set a thick line border on $range4 (automatic color index)
 [void]$range4.borderAround($xlContinuous,$xlThick)
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
 # to clear a range's borders, set its LineStyle to $xlLineStyleNone
 # clear $range2's borders, quick but not the safest way because it'll
 # also clear all borders on every cell within the range
 $range2.borders.lineStyle = $xlLineStyleNone
 # clear $range4's borders, safer, it clears specific borders one at
 # a time
 $xlEdgeLeft, $xlEdgeTop, $xlEdgeBottom, $xlEdgeRight | % {
  $range4.borders.item($_).lineStyle = $xlLineStyleNone
 }
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# close and release resources
 $wb.close($false)
 $xl.quit()
 spps -n excel
 

Don’t you hate it when Tab Complition does not work?

Here is a VBScript that will solve that problem for you;
‘<——— VBScript Starts here —————————————->
Dim strComputer
Dim objWMIService
Dim SWBemlocator
Dim UserName
Dim Password
Dim Title, Text1, Text2
Dim Message, result
Title = "Add Tab Complition"
Text1 = "User Input Cancelled"
Text2 = "You Entered: " & vbTab
‘ Define Msg Box Var
Message = "PLease Enter Server Name"
result = InputBox(Message, Title, "Type Server Name Here", 100, 100)
‘ Evaluate the user input.
If result = "" Then
 WScript.Echo Text1
 WScript.Quit
Else
 WScript.Echo Text2 & result & " Please wait"
End If
strComputer = result
‘ Add Tab Compition
Set WshShell = WScript.CreateObject("WScript.Shell")
SetTabComplet(strComputer)

 Sub SetTabComplet(strComputer)
  ‘ Use to configure Tab Complition
  ‘ provide system name
  Dim sPath, oReg
  Const HKLM = &H80000002
  sPath = "SOFTWAREMicrosoftCommand Processor"
Set oReg = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" _
   & strComputer & "rootdefault:StdRegProv")
 oReg.SetStringValue HKLM, sPath,"AutoRun"=""
 oReg.SetStringValue HKLM, sPath,"CompletionChar",9
 oReg.SetStringValue HKLM, sPath,"DefaultColor",0
 oReg.SetStringValue HKLM, sPath,"EnableExtensions",1
 oReg.SetStringValue HKLM, sPath,"PathCompletionChar",40
 End Sub
 
‘< End Script ———————————————————->