RSS

Category Archives: VBScripts

VBScript to list shares and permissions in Windows OS

I thought that this would be an easy script to find, but surprising there are no complete script to be found. I found the following powershell script in the respository

http://gallery.technet.microsoft.com/scriptcenter/List-Share-Permissions-83f8c419

It works and lists the shares, but it does not product the path of the share!

Found another helpful posts for VBScript version here:

http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/55acaaf8-efc5-4432-8ac7-b37eed81defd/

It works but for some reasons the security permissions are not correct

The closest I find is this:

http://www.visualbasicscript.com/Enumerating-Share-Permissions-not-NTFS-m58860.aspx

Works well BUT it again does not list the path of share. Why in the world would I not want to know where a share path is? Read the rest of this entry »

 
3 Comments

Posted by on February 27, 2013 in Scripts, VBScripts, Windows

 

Tags:

Using Get-ADObject to query subnets and sites

Okay, I did not develop the scripts myself; I modified them from this site, I needed this for quick and easy queries

http://blogs.msdn.com/b/adpowershell/archive/2009/08/18/active-directory-powershell-to-manage-sites-and-subnets-part-3-getting-site-and-subnets.aspx

#List all subnets for a site (okay its a onliner, but you can always use a variable to replace the site name)

foreach ( $SubnetDN in (Get-ADObject -Identity
"CN=yousite,CN=Sites,CN=Configuration,DC=fabrika,DC=com"
 -properties *).siteObjectBL) 
{Get-ADObject -Identity $SubnetDN -properties * | % {$_.name} }

#List all subnets (long process)
Get-ADObject -Filter 'ObjectClass -eq "subnet"'
 -SearchBase 'CN=Configuration,DC=fabrika,DC=com'

#List all sites (long process)
Get-ADObject -Filter 'ObjectClass -eq "site"'
 -SearchBase 'CN=Configuration,DC=fabrika,DC=com'

 #List the site associated with a particular subnet 10.190.2.3" 
Get-ADObject -Filter 'ObjectClass -eq "subnet" 
-and name -like "10.190.2.3*"' -SearchBase 
'CN=Configuration,DC=fabrika,DC=com' -properties siteobject
 
Leave a comment

Posted by on November 29, 2012 in Scripts, VBScripts, Windows

 

Tags:

VBScript: Adding date/time stamp to log file name

Very often when we do logging for our script we would also want to apply a date/time stamp to them. Unfortunately VB does no offer any easy way to do this. This is how I do it:

LogFile = GetScriptPath() & "iStar_" & _
 cStr(Year(now())) & Pad(cStr(Month(now())),2) & _
 Pad(cStr(Day(now())),2) & Pad(cStr(Hour(now())),2) & _
 Pad(cStr(Minute(now())),2) & Pad(cStr(Second(now())),2) & _
 ".log"

You will see an additional function Pad(), this is necessary to ensure constant number of strings. The function simply added addition “0” for single digit numbers to make them 2 digits. It is of course a useful function for other stuff.

Function Pad(CStr2Pad, ReqStrLen)
Dim Num2Pad

 Pad = CStr2Pad
 if len(CStr2Pad) < ReqStrLen then
 Num2Pad = string((ReqStrlen - len(CStr2Pad)), "0")
 Pad = Num2Pad & CStr2Pad 
 end if
End Function
 
2 Comments

Posted by on October 16, 2012 in Scripts, VBScripts

 

VBScript: object not found, but its there… I am sure.

This is a simple VB logic, it checks if a file exists is another directory can move it there if not. It also logs an entry regarding the move. However, could you spot any problem with this?

  1. set f = fso.getfile(“c:\temp\myfile.txt”)
  2. newdir = “c:\temp2”
  3. If Not fso.FileExists( newdir & “\” & f.name) Then
  4. fso.MoveFile f.path, newdir
  5. l.writeline now & “: ” & “Moved file = ” & f.name & “, To = ” & newdir
  6. End If

When this is run, it returns an error “Object Not Found” at line 5.

It took me awhile to figure it out although it should have been obvious.

The error occurs at line 5 at “f.name” object. The problem is that in line 4, the file object is moved to a new location, but in line 5 we ended up reference a stale object or an object that is no more there, its been moved!

So the correct way to fix is to save the f.name into a variable (e.g. oldfile) and reference in line 5 such as:

l.writeline now & ": " & "Moved file = " & oldfile & ", To = " & newdir
 
Leave a comment

Posted by on October 16, 2012 in Scripts, VBScripts

 

VBScript to grab server names from a list of HP Blade enclosures

Challenge: How to get all blade’s server names (iLO name really) from a list of Enclosure’s IP address? This so far works only for HP Blade C series enclosure

set objArg = WScript.Arguments
if objArg.Count >= 4 then

  set WshShell = WScript.CreateObject("WScript.Shell")

  Encl_IP = objArg(0)
  Encl_Name = objArg(1)
  LogonID = objArg(2)
  LogonPWD = objArg(3)

  WshShell.run "telnet.exe -f .\" & Encl_Name & "_" _
   & Encl_IP & ".log " & Encl_IP
  WScript.Sleep 2000

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys LogonID
  WshShell.SendKeys ("{Enter}")
  WScript.Sleep 1000

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys LogonPWD
  WshShell.SendKeys ("{Enter}")
  WScript.Sleep 1000

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys "SHOW SERVER LIST"
  WshShell.SendKeys("{Enter}")
  WScript.Sleep 1500

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys "exit"
  WshShell.SendKeys ("{Enter}")
  WScript.Sleep 1500

  wshShell.AppActivate ("telnet.exe " & objArg(0))
  WshShell.SendKeys ("{Enter}")

  Set fso = CreateObject("Scripting.FileSystemObject")
  set f = fso.OpenTextFile(".\" & Encl_Name & "_" & Encl_IP & ".log")

  Is_SOL = false  'Is start of line?
  SvrInfo = ""
  do while not f.AtEndOfStream
    t = f.readline
    if instr(t,"Bay iLO Name ") > 0 then
      t = f.readline
      t = f.readline
      Is_SOL = true
    elseif instr(t,"Totals:") > 0 then
      Is_SOL = false
      exit do
    end if

    if Is_SOL then  'grab server name from current line
      t = mid(t,5,29) & vbTab & Encl_Name & vbTab & Encl_IP
      SvrInfo = t & vbCrLf & SvrInfo
    end if

  loop

  f.close

  set f1 = fso.OpenTextFile(".\ServerList.log",8,true)
  f1.Writeline SvrInfo
  f1.close

else

  Wscript.echo " "
  Wscript.echo "<encl. IP> <encl. name>  "
  Wscript.echo " "
  Wscript.echo "Log file for telnet session = <encl. name>_log.log"

end if
 
Leave a comment

Posted by on August 1, 2012 in Scripts, VBScripts

 

Tags: ,

Setting the correct encoding when using Out-File

One caveat I realized today when trying to create a file using Out-file in powershell.

The command is simple enough:

dir c:\temp\*.exe | Out-file c:\test.txt -append

After that, I used for loop in command prompt to process the txt file

for /f %a in (c:\test.txt) do @echo %a

Guess what? Nothing happened! The above command does not work as it should!

This is due to the encoding used by Out-file. The default encoded in unicode, which the batch command could not read. So if you intend to generate a file for tools like batch commands to process, you must ensure that the encoding is correct.Using this command will make the txt file readable by the FOR command later.

dir c:\temp | Out-file c:\test.txt -append -Encoding "default"
 
Leave a comment

Posted by on June 27, 2012 in Scripts, VBScripts

 

Tags:

Using WMI to query HP blade Enclosure information

Recently, we had some issues with HP cSeries blade enclosures which requires me to gather the firmware information of the enclosure, blade iLO and blade ROM.

Using WMIExplorer I found that the WMI information is kept in the root\HPQ and got a quick and dirty script to query the information I need.

set objArg = WScript.Arguments
if objArg.Count >= 1 then
strComputer = objArg(0)
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\HPQ")
Set colItems = objWMIService.ExecQuery _
    ("Select * from HP_BladeEnclosureCS")
For Each objItem in colItems
    Encl_Name = objItem.Name
    Encl_IP = objItem.ManagementIPAddress
    exit for
Next
Set colItems = objWMIService.ExecQuery _
    ("select * from HP_BladeEnclosureFW")
For Each objItem in colItems
    Encl_FW = objItem.VersionString
    exit for
Next
Set colItems = objWMIService.ExecQuery _
    ("select * from HP_SystemROMFirmware")
For Each objItem in colItems
    If InStr(objItem.Caption, "Active") > 0 then
      Blade_FW = objItem.VersionString
    end if 
    exit for
Next
Set colItems = objWMIService.ExecQuery _
    ("select * from HP_MPFirmware")
For Each objItem in colItems
    Blade_iLO_FW = objItem.VersionString
    exit for
Next
Wscript.echo strcomputer & vbTab & Blade_FW & vbTab & Blade_iLO_FW _
  & vbTab & Encl_Name & vbTab & Encl_Ip & vbTab & Encl_FW
end if
 
Leave a comment

Posted by on May 28, 2012 in Scripts, VBScripts

 

Tags: ,