【问题标题】:Including Arrays from one Script into Another Script将一个脚本中的数组包含到另一个脚本中
【发布时间】:2015-03-01 02:29:29
【问题描述】:

我有多个脚本使用相同的数组来定义文件路径和获取文件日期。例如:

$file1path = "C:\filepath\file1.exe"
$file1date = (Get-Childitem $file1path).LastWriteTime.ToString("m/d/yyyy")

这些脚本然后使用这些数组构建 .ps1 校验和脚本和带有更新文件日期信息的 VB 监控脚本文件,因为文件版本控制在这里有点开玩笑。我开始想,我可以通过将这 200 行数组放入仅 1 个主数组文件中来简化这些脚本,然后这些其他构建脚本可以只包含某种包含行来从这个主脚本中提取它调用的数组。

我有什么好办法吗?不幸的是,我需要在我们的环境中使用 PowerShell v1。

【问题讨论】:

    标签: arrays include powershell-1.0


    【解决方案1】:

    您可以将一个 .ps1 文件的内容点源到另一个文件中。这使您可以有效地将通用 .ps1 中定义的任何变量和函数“包含”到您点源到的任何 .ps1 脚本文件中。例如:

    Common.ps1 contents
    -------------------
    $files = @(Get-ChildItem c:\filepath\*.exe)
    
    Doit.ps1 contents
    -----------------
    . .\common.ps1
    foreach ($file in $files) {
        $fileDate = $file.LastWriteTime.ToString("MM/dd/yyyy")
    }
    

    【讨论】:

    • 谢谢。对 PowerShell 来说还是相当新的,所以我会看看我能用它做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 2021-08-28
    相关资源
    最近更新 更多