$doc是Powershell中的保留字/引用



我已经继承并正在运行下面的Powershell脚本,我在解释它并将其某些功能应用于我尝试编写的其他脚本时遇到了一个问题。

特别是,我正在查看以下代码行:

ForEach ($doc in $srcfiles) { 
saveas-document -docs $doc
}

从程序功能中,我知道$srcfiles文档的每个实例都被分配给变量$doc,并且该变量作为输入值传递给saveas-document函数。但是,我不确定$doc来自哪里。此语句是否动态声明 $doc 变量?还是表示源路径中的文档对象的 Powershell 保留字?另外,dessence 中的-docs开关是否声明$doc等于函数预期的$docs变量?我需要一些帮助来理解它是如何工作的,以便我可以将这些知识应用于其他项目。

$global:word   = new-object -comobject word.application 
$word.Visible  = $False 
 # PATHS
$backupPath    = "\Serverpathtosourcefiles" 
$srcfiles      = Get-ChildItem $backupPath -filter "*htm.*"
$dPath         = "\Serverpathtodesitinationfiles"      
$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()

运行以下命令,并通读输出:

Get-Help about_foreach -ShowWindow
Get-Help about_functions -ShowWindow

最新更新