- USE [YourSQLDba]
- GO
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- ALTER proc [Maint].[CreateNetworkDrives]
- @DriveLetter nvarchar(2)
- , @unc nvarchar(255)
- as
- Begin
- Declare @errorN int
- Declare @cmd nvarchar(4000)
-
- Set nocount on
-
- Exec yMaint.SaveXpCmdShellStateAndAllowItTemporary
-
- Set @DriveLetter=rtrim(@driveLetter)
- Set @Unc=rtrim(@Unc)
-
- If Len(@DriveLetter) = 1
- Set @DriveLetter = @DriveLetter + ':'
-
- If Len(@Unc) >= 1
- Begin
- Set @Unc = yUtl.NormalizePath(@Unc)
- Set @Unc = Stuff(@Unc, len(@Unc), 1, '')
- End
-
- Set @cmd = 'net use <DriveLetter> /Delete'
- Set @cmd = Replace( @cmd, '<DriveLetter>', @DriveLetter)
-
-
- begin try
- Print @cmd
- exec xp_cmdshell @cmd, no_output
- end try
- begin catch
- end catch
-
- -- suppress previous network drive definition
- If exists(select * from Maint.NetworkDrivesToSetOnStartup Where DriveLetter = @driveLetter)
- Begin
- Delete from Maint.NetworkDrivesToSetOnStartup Where DriveLetter = @driveLetter
- End
-
- Begin Try
-
- Set @cmd = 'net use <DriveLetter> <unc>'
- Set @cmd = Replace( @cmd, '<DriveLetter>', @DriveLetter )
-
- Set @cmd = Replace( @cmd, '<unc>', @unc )
- Print @cmd
- exec xp_cmdshell @cmd
-
- Insert Into Maint.NetworkDrivesToSetOnStartup (DriveLetter, Unc) Values (@DriveLetter, @unc)
-
- Exec yMaint.RestoreXpCmdShellState
-
- End Try
- Begin Catch
- Set @errorN = ERROR_NUMBER() -- return error code
- Print convert(nvarchar, @errorN) + ': ' + ERROR_MESSAGE()
- Exec yMaint.RestoreXpCmdShellState
- End Catch
-
- End -- Maint.CreateNetworkDrives