0

我是 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 = ""
4

2 回答 2

1

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

第一个对我们来说是 $args[n] [automatic variable] 1。如果您的脚本被调用MyScript.PS1,您可以使用以下命令调用它:

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

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

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

Param ($MyParam1, $MyParam2)

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

于 2012-09-24T05:06:42.153 回答
0

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

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
于 2014-01-10T08:23:23.813 回答