我继承并运行下面的Powershell脚本,我遇到一个问题,解释它并将其一些功能应用到我正在尝试编写的其他脚本中。
特别是,我正在查看以下代码行:
ForEach ($doc in $srcfiles) {
saveas-document -docs $doc
}
从程序功能,我知道$srcfiles
中的每个文档实例都被分配给变量$doc
,并且该变量正被传递给saveas-document
函数,因为输入值。但是,我不确定$doc
来自哪里。此语句是否即时声明$doc
变量?或者它是一个Powershell保留字,代表我的源路径中的文档对象?此外,-docs
转换为dessence是否声明$doc
等于函数预期的$docs
变量?我需要一些帮助,了解这是如何工作的,这样我就可以将这些知识应用到其他项目中。
$global:word = new-object -comobject word.application
$word.Visible = $False
# PATHS
$backupPath = "\\Server\path\to\source\files\"
$srcfiles = Get-ChildItem $backupPath -filter "*htm.*"
$dPath = "\\Server\path\to\desitination\files\"
$htmPath = $dPath + "HT\" # Data path for HTML
$docPath = $dPath + "DO\" # Data path for *.DOC
$doxPath = $dPath + "DX\" # Data path for *.DOCX
$txtPath = $dPath + "TX\" # Data path for *.TXT
$rtfPath = $dPath + "RT\" # Data path for *.RTF
# SAVE FORMATS
$saveFormatDoc = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 0);
$saveFormatTxt = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 4);
$saveFormatRTF = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 6);
$saveFormatDox = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 16);
$saveFormatXML = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 14);
# Convert Documents
function saveas-document ($docs) {
$savepath = "$docPath$($docs.BaseName)"
"Converting to $savepath.doc"
$opendoc.saveas([ref]"$savepath", [ref]$saveFormatDoc)
$opendoc.close()
"Success with conversions."
" "
}
#
ForEach ($doc in $srcfiles) {
saveas-document -docs $doc
}
#
$word.quit()
答案 0 :(得分:1)
运行以下命令,并通读输出:
Get-Help about_foreach -ShowWindow
Get-Help about_functions -ShowWindow