SSIS: Accessing a network drive using a different username and password

后端 未结 5 606
借酒劲吻你
借酒劲吻你 2020-12-14 13:17

Is there a way to connect to a network drive that requires a different username/password than the username/password of the user running the package?

I need to copy f

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 13:33

    You have to map the network drive, here's an example that I'm using now:

        profile = "false"
        landingPadDir = Dts.Variables("strLandingPadDir").Value.ToString
        resultsDir = Dts.Variables("strResultsDir").Value.ToString
        user = Dts.Variables("strUserName").Value.ToString
        pass = Dts.Variables("strPassword").Value.ToString
        driveLetter = Dts.Variables("strDriveLetter").Value.ToString
    
        objNetwork = CreateObject("WScript.Network")
        CheckDrive = objNetwork.EnumNetworkDrives()
    
        If CheckDrive.Count > 0 Then
            For intcount = 0 To CheckDrive.Count - 1 Step 2 'if drive is already mapped, then disconnect it
                If CheckDrive.Item(intcount) = driveLetter Then
                    objNetwork.RemoveNetworkDrive(driveLetter)
                End If
            Next
        End If
    
        objNetwork.MapNetworkDrive(driveLetter, landingPadDir, profile, user, pass)
    

    From There just use that driveLetter and access the file via the mapped drive.

    I'm having one issue (which led me here) with a new script that accesses two share drives and performs some copy/move operations between the drives and I get an error from SSIS that says:

    This network connection has files open or requests pending.
       at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
       at ScriptTask_3c0c366598174ec2b6a217c43470f581.ScriptMain.Main()
    

    This is only on the "2nd run" of the process and if I run it a 3rd time it all works fine so I'm guessing the connection isn't being properly closed or it is not waiting for the copy/move to complete before moving forward or some such, but I'm unable to find a "close" or "flush" command that prevents this error. If you have any solution, please let me know, but the above code should work for getting the drive mapped using your alternate credentials and allow you to access that share.

    Zach

提交回复
热议问题