views:

621

answers:

2

I have a task: files available over WebDAV on a remote server (SSL required) must be checked for whether they may have been updated recently, and if so copied to a local folder. There are a number of other actions that need to be performed after they arrive (copied to other folders, processed, etc.). The operating system I'm working from is Windows 2003 Server. I'd love to be able to use PowerShell for this task.

Naturally, I need to browse the files. I've looked tentatively at several solutions:

  • Trying to map a drive using "net use" (so far, I get a system 67 error)
  • Using a product like WebDrive to map a drive (as it happens, WebDrive and another utility on the server seem to conflict with each other for mysterious reasons)
  • Browse and manipulate the files by issuing http requests using the .NET HTTPWebRequest object hierarchy through PowerShell (works, but seems a bit complicated)
  • Purchase a commercial .NET assembly that simplifies working with WebDAV (ones that I've seen look pricey)

Have you needed to do something similar? Which approach is best? Any that I have missed? TIA.

+1  A: 

It will work from powershell. Note this example:

http://thepowershellguy.com/blogs/posh/archive/2008/05/31/cd-into-sysinternals-tools-from-powershell.aspx

The problem is that the 'web client service' not running on the windows 2003 server (it's disabled by default).

The clue was the "System 67 error"

I confirmed this from a win2k3 server, starting the 'web client service' will get WebDAV working (and probably powershell). It will work out of the box on an XP client (service is running by default).

Let me know if this doesn't resolve it for you.

atom255
Thanks! As it happens, I can't test this just yet (need to get clearance from IT team), but a great tip.
Eldergriffon
A: 

As an alternative to PowerShell, you could always do this from a WSH script. Example:

<job>
  <reference object="ADODB.Connection"/>
  <object id="cnIPP" progId="ADODB.Connection"/>
  <object id="recDir" progId="ADODB.Record"/>
  <script language="VBScript">
  Option Explicit

  Private waArgs
  Private strSubDir
  Private rsItems
  Private strLine

  Set waArgs = WScript.Arguments

  If waArgs.Count < 3 Then
    WScript.Echo "Parameters: FolderURL User PW [SubDir]"
    WScript.Quit
  End If

  cnIPP.Open "Provider=MSDAIPP.DSO;Prompt=NoPrompt;" _
           & "Connect Timeout=10;" _
           & "Data Source=" & waArgs(0), _
             waArgs(1), waArgs(2), adConnectUnspecified

  If waArgs.Count = 4 Then
    strSubDir = waArgs(3)
  Else
    strSubDir = vbNullString
  End If
  Set waArgs = Nothing

  recDir.Open strSubDir, cnIPP, adModeRead, adFailIfNotExists, _
              adDelayFetchFields Or adDelayFetchStream
  Set rsItems = recDir.GetChildren()
  With rsItems
    WScript.Echo .Fields("RESOURCE_PARENTNAME").Value
    Do Until .EOF
      If .Fields("RESOURCE_ISCOLLECTION").Value Then
        strLine = "  [DIR] " & .Fields("RESOURCE_PARSENAME").Value
      Else
        strLine = "       " _
                & " " & .Fields("RESOURCE_PARSENAME").Value _
                & " " & CStr(.Fields("RESOURCE_LASTWRITETIME").Value)

      End If
      WScript.Echo strLine
      .MoveNext
    Loop
    .Close
  End With
  Set rsItems = Nothing
  recDir.Close

  cnIPP.Close
  </script>
</job>

A sample run:

D:\Scripts>cscript WebDAV.wsf https://my.dav.com/~fred fred fredPW
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

https://my.dav.com/~fred
        junk.htm 2/26/2008 4:28:44 AM
        test.log 3/30/2009 12:30:45 PM
  [DIR] _private
  [DIR] stuff

D:\Scripts>

This approach should work with both WebDAV and FrontPage enabled servers without change. The example defaults to protocol auto-negotiation.

To actually retrieve data you'd open an ADODB.Stream on an ADODB.Record opened on the non-directory item.

Bob Riemersma