【问题标题】:Using Parameters in Powershell to assign folder locations在 Powershell 中使用参数分配文件夹位置
【发布时间】:2012-09-22 21:18:45
【问题描述】:

我是 powershell 新手,我需要一些帮助。下面是我编写的用于在文件夹中查找 excel 文件的脚本。 excel 工作表中的文件将与同一台机器上另一个文件夹的内容进行比较。位置是:“C:\MKS_DEV\”,生成的匹配文件将被压缩并放在另一个位置,如脚本中所示。这些脚本将被不同机器上的其他人使用,因此两个文件夹的位置在不同机器上可能不同。

我想为两个文件夹的位置编写一个参数或使用参数,这样我就不必一直指定位置,我必须运行脚本并且无法弄清楚如何实现它。

脚本运行良好,但我只需要将参数/参数合并到其中。任何帮助将不胜感激。

谢谢。

代码如下:

# Creating an object for the Excel COM addin
$ExcelObject = New-Object -ComObject Excel.Application

# Opening the Workbook
$ExcelWorkbook = $ExcelObject.Workbooks.Open("C:\Eric_Source\Test.xls")

# Opening the Worksheet by using the index (1 for the first worksheet)
$ExcelWorksheet = $ExcelWorkbook.Worksheets.Item(1)

# The folder where the files will be copied/The folder which will be zipped
# later
$a = Get-Date

$targetfolder = "C:\"+$a.Day+$a.Month+$a.Year+$a.Hour+$a.Minute+$a.Second

# Check if the folder already exists. Command Test-Path $targetfolder returns
# true or false.
if(Test-Path $targetfolder)
{
    # delete the folder if it already exists. The following command deletes a
    # particular directory
    Remove-Item $targetfolder -Force -Recurse -ErrorAction SilentlyContinue
}

# The following command is used to create a particular directory
New-Item -ItemType directory -Path $targetfolder

# Declaration of variables, COlumn value = 6 for Column F
$row = 1
$col = 6 

# Read a value from the worksheet with the following command
$filename = $ExcelWorksheet.Cells.Item($row,$col).Value2
$filename

# change the folder value below to specify the folder where the powershell
# needs to search for the filename that it reads from excel file.

$folder = "C:\MKS_DEV\"
$null = ""

【问题讨论】:

  • 请多注意代码格式。
  • 您希望脚本具有默认值对于 test.xls 还是希望脚本在本地计算机中搜索 test.xls 或者您只是希望脚本在文件夹中查找它是从名为 test.xls 的文件运行的,例如,如果您的脚本位于文件夹 c:\justin 中,则脚本将知道其根文件夹是 c:\justin 并在该文件夹中查找 test.xls 如果您移动脚本到 d:\mike 它会知道它在 d:\mike 文件夹中并在该文件夹中查找 test.xls 。
  • 感谢 Justinf,我想我希望脚本有一个默认值。 excel 位置:("C:\Eric_Source\Test.xls") 和另一个文件夹位置:"C:\MKS_DEV\ 这些文件夹位置会在不同的机器上改变,所以我想使用参数/参数,所以位置不会是硬编码的,可以在不同的机器上使用,而无需一直指定位置。

标签: excel function powershell parameters arguments


【解决方案1】:

您有多种方法来参数化您的脚本。

第一个是给我们$args[n] [自动变量]1。 如果您的脚本被称为MyScript.PS1,您可以使用:

MyScript.PS1 "C:\Eric_Source\Test.xls"

然后在你的脚本中使用$args[0] 作为第一个参数。

另一种方法是在脚本开头使用保留字 Param:

Param ($MyParam1, $MyParam2)

当您调用脚本时,$MyParam1 将包含第一个参数,依此类推。

【讨论】:

  • 非常感谢 JPBlanc,我对 Powershell 很陌生,所以我很难理解我必须做什么。查看我提供的脚本,我如何才能将其完全按照您在此处的解释将其合并到我的脚本中。
【解决方案2】:

您可以将它创建为一个函数并加载它。

Function Folder-Deletion ($ExcelWorkbook,$targetfolder) {

$ExcelObject = New-Object -ComObject Excel.Application
$ExcelOpen = $ExcelObject.Workbooks.Open($ExcelWorkbook)
$ExcelWorksheet = $ExcelOpen.Worksheets.Item(1)
$a = Get-Date

if(Test-Path $targetfolder)
{
    Remove-Item $targetfolder -Force -Recurse -ErrorAction SilentlyContinue
}

New-Item -ItemType directory -Path $targetfolder

$row = 1
$col = 6 

$filename = $ExcelWorksheet.Cells.Item($row,$col).Value2
$filename
}

然后像这样对电子表格和文件夹运行函数:

Folder-Deletion C:\Eric_Source\Test.xls C:\MKS_DEV

或者您可以创建一个包含以下内容的 PowerShell 脚本文件(例如 FolderDeletion.ps1):

param($ExcelWorkbook,$targetfolder)

$ExcelObject = New-Object -ComObject Excel.Application
$ExcelOpen = $ExcelObject.Workbooks.Open($ExcelWorkbook)
$ExcelWorksheet = $ExcelOpen.Worksheets.Item(1)
$a = Get-Date

if(Test-Path $targetfolder)
{
    Remove-Item $targetfolder -Force -Recurse -ErrorAction SilentlyContinue
}

New-Item -ItemType directory -Path $targetfolder

$row = 1
$col = 6 

$filename = $ExcelWorksheet.Cells.Item($row,$col).Value2
$filename

然后像这样针对电子表格和文件夹运行脚本:

FolderDeletion.ps1 C:\Eric_Source\Test.xls C:\MKS_DEV

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 2017-07-15
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多