We have an application here that dumps data into a CSV, which is then used by another script. In the previous iteration of the application, one of the columns was "user_name" and contains user IDs. That is still there, but it now appends "_company" to the user ID, so instead of the user ID "tim" it is now "tim_company" in the column. We are looking to strip the company off, and I thought the best way would be to embed some VB into the existing script as a function, like this site suggested: http://www.out-web.net/?p=130.
This is what I've tried to add:
function Load-VbsCode{
param(
$Code = $(throw "No VBS code specified.")
)
# Convert an array of multiple text lines to a string
$vbsCode = [string]::Join("`n", $Code)
$vbs = New-Object -ComObject 'MSScriptControl.ScriptControl'
$vbs.Language = "VBScript"
$vbs.AddCode($vbsCode)
$vbs.CodeObject
}
# pass the entire vbs file to Load-VbsCode
$vbs = Load-VbsCode $(Get-Content .\ReplaceData.vbs)
# Ready to use the Removenavient function
$c = $vbs.RemoveData
The .vbs is just to remove/replace data as shown here:
Function Removedata
Const FromValue = "_company"
Const ToValue = ""
Dim objExcel : Set objExcel = CreateObject("Excel.Application")
'objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("D:\Location\JunkData.csv")
Dim objWorksheet : Set objWorksheet = objWorkbook.Worksheets(1)
'Dim objRange : Set objRange = objWorksheet.UsedRange
objWorksheet.Cells.Replace FromValue, ToValue
objExcel.DisplayAlerts = False
objExcel.Save
objExcel.Quit
End Function
I'm getting this error:
New-Object : Retrieving the COM class factory for component with CLSID
{0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC} failed due to the following error:
80040154.
At D:\Location\Remove__company_from_CSV.ps1:11 char:22
+ $vbs = New-Object <<<< -ComObject 'MSScriptControl.ScriptControl'
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
Property 'Language' cannot be found on this object; make sure it exists and
is settable.
At D:\Location\Remove__company_from_CSV.ps1:12 char:10
+ $vbs. <<<< Language = "VBScript"
+ CategoryInfo : InvalidOperation: (Language:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
You cannot call a method on a null-valued expression.
At D:\Location\Remove__company_from_CSV.ps1:13 char:17
+ $vbs.AddCode <<<< ($vbsCode)
+ CategoryInfo : InvalidOperation: (AddCode:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I don't know VB, and I don't know how to call VBScript in PS. I read this might be a missing DLL, but I'm more likely to believe I'm not calling something I should be calling. Any ideas?