0

Does anyone know how I can load a DLL without having it on each remote server I am using in a persistent connection and running the invoke-command cmdlet with?

I am using DotNetZip to backup folders on about 13 servers. Everything is working locally, but when it gets to a remote server (the first one in the array is the local server), it errors because it doesn't see the DLL on the remote server.

I execute this script on one server and it should zip up folders on each remote server:

foreach($i in $appServers) {
        $sessionForI = New-PSSession -computername $i
        Invoke-Command -Session $sessionForI -ScriptBlock {
            if (!(Test-Path -path C:\\newDeploy)) {
                New-Item C:\\newDeploy -type directory
            }
            [System.Reflection.Assembly]::LoadFrom("C:\\newDeploy\\Ionic.Zip.dll");
            $directoryToZip = "C:\\Program Files (x86)\\SubDir\\$folder"
            $zipfile = new-object Ionic.Zip.ZipFile
            $e = $zipfile.AddSelectedFiles("name != '*.e2e'",$directoryToZip, "",1)
            if (!(Test-Path -path C:\\newDeploy\\backup)) {
                New-Item C:\\newDeploy\\backup -type directory
            }
            $zipfile.Save("C:\\newDeploy\\backup\\" + $folder+ ".zip")
            $zipfile.Dispose()
        }
        remove-PSSession -session $sessionForI
    }

Thank you .

-Jim

user1161625
  • 642
  • 1
  • 8
  • 13

3 Answers3

1

I'm pretty sure you are going to need to copy Ionic.Zip.dll to the remote machines to do this. You could try sharing it out from your lead system and using a UNC path to load it from the remote machines (i've never tried that... going to now...) :-)

Update - yep just confirmed you can pass a UNC path to [System.Reflection.Assembly]::LoadFrom.

Update 2 - While the assembly loaded, using it didn't work so well:

Exception calling "AddFile" with "1" argument(s): "Request for the permission of type 'System.Security.Permissions.File
IOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
At line:1 char:11
+ $z.AddFile <<<< ("C:\AMCleanUp.log")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

When I loaded a local copy of the the DLL the AddFile method worked fine. You're only option might be to copy this DLL to all your servers...

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • Are you running .NET 3.5 SP1? As of that service pack, managed code is allowed to be launched from a network share. See this post - http://blogs.msdn.com/b/brada/archive/2008/08/13/net-framework-3-5-sp1-allows-managed-code-to-be-launched-from-a-network-share.aspx. – Keith Hill Jan 21 '12 at 00:57
  • @KeithHill Yep I got that error with .NET 3.5 SP1 installed. When I do `[environment]::Version` it's 2.0... Not sure if it matters... Interestingly I couldn't even call `LoadFrom` on Win7 with PS3 CPT2 installed `HRESULT: 0x80131515`. No alternate data stream either... Worked fine on XP with PS2. I was able to call .NET console program from a UNC path though. – Andy Arismendi Jan 21 '12 at 23:48
  • Thank you for your quick response :) I will just bite the bullet and copy this DLL to all of my remote machines. Maybe even add that in to the script I will be running if the DLL doesn't exist, just copy it over. Thanks again! – user1161625 Jan 23 '12 at 13:57
0

You can use a UNC path in the LoadFrom for the remote boxes, but I see that someone has had problem doing the same with DotNetZip:

http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/dd5dcae2-1ccc-4be2-b986-61c069102ffb/

manojlds
  • 290,304
  • 63
  • 469
  • 417
0

I think your problems with accessing remote resources in an already remote session has to do with double-hop authentication. Check this link http://www.ravichaganti.com/blog/?p=1230

goAtsy
  • 1