VB script converted to Powershell

Bill Warren
Bill Warren used Ask the Experts™
on
I have a vbscript that works for Windows 2003 but when I cannot get it to work in Windows 2008. I believe I need to convert this script to Powershell for it to work.
Option Explicit 
  
Dim nameSpace 
Dim objSet 
Dim dateTime 
Dim vDate 
Dim obj 
Dim age 
  
Dim Time, AmPm, Hour 
Dim aParts 
  
  
If Hour >= 18 Then 
   Msg = "  This occurred after 6 PM" 
'Else 
   'Msg = "  This occurred before 6 PM" 
End If 
  
  Set dateTime  = CreateObject("WbemScripting.SWbemDateTime") 
  Set nameSpace = GetObject("winmgmts://localhost/root/cimv2") 
  Set objSet    = nameSpace.ExecQuery("select * from Win32_ShadowCopy") 
  
  If objSet.Count > 0 Then 
     For Each obj In objSet 
        dateTime.Value = obj.InstallDate 
        vDate  = dateTime.GetVarDate(True) 
  
        aParts = Split(vDate, " ") 
        Time   = aParts(1) 
        AmPm   = aParts(2) 
        aParts = Split(Time, ":") 
        If AmPm = "PM" Then 
           Hour = aParts(0) + 12 
        Else 
           Hour = aParts(0) 
        End If 
  
        'WScript.Echo "- Snapshot on " & obj.VolumeName & " @ " & vDate 
  
        age = DateDiff("d", vDate, Date) 
        If age > 1 And Hour <> 18 Then 
           'WScript.Echo "[Deleting snapshot...] for " & obj.Caption & " created on " & vDate 
      obj.Delete_() 
        End If 
     Next 
  End If

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill WarrenIT Manager

Author

Commented:
The script basically deletes any Shadow Copies that are over 3 days old except for those that are created at 6PM so I could have 8 or so shadow copies for each day for 3 days and from then on it keeps only 6PM for as many as it can keep.
Chris DentPowerShell Developer
Top Expert 2010

Commented:

You could convert it to PowerShell, but unless you have a better access method in mind it won't make any difference.

Where is it failing at the moment? Failing to get the list of Shadow Copies? Or failing when you call Delete? Or something else entirely?

Of course, if you want to convert it to PowerShell anyway I'll happily help out :)

Chris
Bill WarrenIT Manager

Author

Commented:
I think the SWbemObjectSet has been depreciated for Windows Server 2008
Here is the error (see screenshot) if I run the script on the 2008 server

scrshot.bmp
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Chris DentPowerShell Developer
Top Expert 2010

Commented:

Fair enough, in that case fire up PowerShell and we'll get started :)

I have nothing to test against here, but it shouldn't be too tricky. Can you tell me how InstallDate is formatted here? We may have to convert it, but there's a converted built into .NET we can use.

Chris
Get-WMIObject Win32_ShadowCopy | Select-Object Caption, InstallDate

Open in new window

Chris DentPowerShell Developer
Top Expert 2010

Commented:

Hmm I think you may find the entire Win32_ShadowCopy class is missing from 2008 64-bit. Trying to check.

Chris
Bill WarrenIT Manager

Author

Commented:
Sorry i haven't dealt with powershell much at all. Did you want me to post your
 Get-WMIObject Win32_ShadowCopy | Select-Object Caption, InstallDate
into the Powershell? I did that and here is the result.
Get-WmiObject : Initialization failure
At line:1 char:14
+ Get-WMIObject <<<<  Win32_ShadowCopy | Select-Object Caption, InstallDate
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Bill WarrenIT Manager

Author

Commented:
This serer is 32 bit Enterprise
Chris DentPowerShell Developer
Top Expert 2010

Commented:

Hmm, no further on than the VbScript. Guess that was too much to hope for. Need to figure out what MS did to the class now.

Chris
PowerShell Developer
Top Expert 2010
Commented:

Heh...

It's so simple, quite annoying.

Run the Script as Administrator, User Account Control is preventing it accessing the WMI class.

Chris
Chris DentPowerShell Developer
Top Expert 2010

Commented:

The VbScript that is, although in PowerShell it becomes:

Chris
Get-WMIObject Win32_ShadowCopy | Select-Object *, `
    @{n='CreationDate';e={ [Management.ManagementDateTimeConverter]::ToDateTime( $_.InstallDate ) }} | `
  ?{ $_.CreationDate -lt (Get-Date).AddDays(-1) -And $_.CreationDate.Hour -ne 18 } | `
  %{
    Write-Host $_ 
    # $_.Delete_ 
  }

Open in new window

Chris DentPowerShell Developer
Top Expert 2010

Commented:

Sorry, there's an error in the code above, it won't let you execute the Delete method. This should be better.

Chris

Get-WMIObject Win32_ShadowCopy | %{
  $Date = [Management.ManagementDateTimeConverter]::ToDateTime($_.InstallDate)

  If ($Date -lt (Get-Date).AddDays(-1) -And $Date.Hour -ne 18)
  {
    $_ | gm
    # Commented out, but should allow you to execute Delete
    # $_.PSBase.Delete()
  }
}

Open in new window

Bill WarrenIT Manager

Author

Commented:
You are right that was annoying and correct.... ARRRGGGHHH!
Thanks a ton Chris-Dent! you rule I'll keep the powershell change for future use as well.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial