【发布时间】:2020-01-20 22:18:58
【问题描述】:
长篇大论,感谢阅读:)
我正在尝试自动分析 AD 导出。我向客户发送 PowerShell 脚本以将 AD 数据导出到 CSV。他们将 CSV 文件发送给我,我将它们导入到一个名为 WeGalvanice ACL(简称 ACL)的分析工具中。在 ACL 中,我能够过滤/匹配/比较以及我需要做的任何事情。
到目前为止一切都很好。现在我想使用这些结果并将它们放在一个 .docx 文件中,麻烦就来了。 ACL不能导出到docx,只能导出xlsx/txt/csv/html/json/del。
好吧,我们可以解决这个问题(或者我想),我做了一个template.docx
在那个template.docx 中,我写了%certainvariable% 之类的占位符。
在 ACL 中,我创建了一个命令,将我需要的所有变量写入名为 %variablename%.txt 的文件中,并且在 txt 中是我需要的值。
例如:
ACL 创建disabledusers.txt。如果你打开 txt 它只显示 10 个,这意味着我们有 10 个禁用用户。
然后我尝试创建一个 VBA 宏来导入 txt 文件并使用包含的值替换 Word 文档中相应的 %...% 文本。
这一切看起来都很顺利,直到我注意到在运行脚本后我得到了20,后面跟着那个框,意思是“我不知道如何显示这个字符”。
在运行脚本之前:
运行脚本后:
我导入的 .txt 内容的图片:
最后但并非最不重要的一点是,我使用来自 word 宏管理器的 VBS 代码(为方便起见,我只上传了 1 个导入和替换作业,但在我的脚本中,我只是为每个文件和变量复制了 30 次):
Sub ACL()
' ACL Macro
FilePath = "local folderpath\date.txt"
TextFile = FreeFile
Open FilePath For Input As TextFile
new_date = Input(LOF(TextFile), TextFile)
old_date = "%date%"
Close TextFile
With ActiveDocument.Content.Find
.Forward = True
.Wrap = wdFindStop
.Execute findtext:=old_date, replacewith:=new_date, Replace:=wdReplaceAll
End With
End Sub
当我手动打开date.txt 并删除第二行时,一切似乎都正常了。但我希望它自动化。 ;)
我试过了
- 将
split textfile,/n和split new_date,/n都添加到\n\r和\r无济于事。 - 使用 sed 命令删除第二行。没用。
- 找到很多 findstr 方法来删除所谓的回车 (crlf) 但要么我的 txt 文件突然空了,要么我的 Word 文件中仍然有这些框。
我对任何编程语言都持开放态度,(我对它们都很糟糕)我只是按照正确的顺序将所有内容排队以使其正常工作;)
提前感谢大家! 如果您需要更多信息,当然可以询问
感谢 Tomalak 的更新,我得到了工作原理:
' removes CR & LF from the end of a string (not pretty but gets the job done)
Function TrimRight(InputStr As String) As String
While Right(InputStr, 1) = vbCr Or Right(InputStr, 1) = vbLf
InputStr = Left(InputStr, Len(InputStr) - 1)
Wend
TrimRight = Trim(InputStr)
End Function
' reads any text file and returns the content as-is
Function ReadFile(FilePath As String) As String
Dim filenum As Integer
If Dir(FilePath) > "" Then
filenum = FreeFile
Open FilePath For Input As filenum
ReadFile = Input(LOF(filenum), filenum)
Close filenum
Else
Debug.Print "File not found: " & FilePath
End If
End Function
' replaces all instances of a single word in the given document
Sub ReplaceAll(oldValue As String, newValue As String, doc As Document)
With doc.Content.Find
.Forward = True
.Wrap = wdFindStop
.Execute findtext:=oldValue, replacewith:=newValue, Replace:=wdReplaceAll
End With
End Sub
Sub ReplaceAllVariables()
Dim variables As Variant, variable As Variant
Dim placeholder As String, realValue As String
variables = Split("customer,c_adgroup,c_adgroup_Cannot_change_PW,c_adgroup_disabled,c_adgroup_enabled,c_adgroup_expire_and_disabled,c_adgroup_LockedOut,c_adgroup_Locked_and_disabled,c_adgroup_loginx,c_adgroup_loginx_disabled,c_adgroup_nolastpasswordset,c_adgroup_nologondate,c_adgroup_PWRQ_and_disabled,c_adgroup_PWx,c_adgroup_PWx_and_disabled,c_adgroup_PW_and_disabled,c_adgroup_PW_and_enabled,c_adgroup_PWRQ_and_enabled ,c_adgroup_PW_not_required,c_adgroup_PW_no_expire,c_adgroup_loginx_and_enabled,c_adusers_f_Cannot_change_PW,c_adusers_f_disabled,c_adusers_f_enabled,c_adusers_f_expire_and_disabled,c_adusers_f_LockedOut,c_adusers_f_Locked_and_disabled,c_adusers_f_loginx,c_adusers_f_loginx_disabled,c_adusers_f_nolastpasswordset,c_adusers_f_nologondate,c_adusers_f_PWRQ_and_disabled,c_adusers_f_PWx,c_adusers_f_PWx_and_disabled,c_adusers_f_PW_and_disabled,c_adusers_f_PW_not_required,c_adusers_f_PW_no_expire,c_adusers_f_loginx_and_enabled,date,c_adusers_f_PW_and_enabled,lastlogon", ",")
For Each variable In variables
placeholder = "%" & variable & "%"
realValue = ReadFile("C:\Users\jerryw\Documents\Klanten\ACL\AD analyse\var\" & variable & ".txt")
realValue = TrimRight(realValue)
ReplaceAll placeholder, realValue, ActiveDocument
Next
End Sub
【问题讨论】:
-
来自帮助 从 Visual Basic 的第一个版本开始,文件就一直使用 Open 语句和其他相关语句和函数(如下所列)进行处理。这些机制最终将被 FSO 对象模型淘汰,但它们在 Visual Basic 6.0 中得到完全支持。.
-
PS 那条评论是 25 岁。
-
除此弃用警告外,
Open会在 txt 文件采用意外编码(例如 UTF-8)时弄乱数据。如果您的情况是否正常,您会看到带有重音字符的文本。 -
FWIW,如果您将 .txt 文件的扩展名更改为 .doc,Word 会将其作为 Word 文档打开。