1

我正在尝试从 Excel 文档中提取特定的单元格值,并将该单元格值添加到 URL 的末尾。目前其余代码工作,成功打开文件,获取特定单元格值并打印它,但我不确定如何将变量保存在 Invoke-webrequest 中。

# start Excel
$excel = New-Object -comobject Excel.Application

#open file
$FilePath = 'FilePath'
$workbook = $excel.Workbooks.Open($FilePath)

#make it visible (just to check what is happening)
$excel.Visible = $true

#print file name
$workbook.sheets.item(1).activate()
$workSheet = $Workbook.Sheets.Item(1)
$WorkSheet.Name

#print cell value
$WorkbookTotal=$workbook.Worksheets.item(1)
$value = $WorkbookTotal.Cells.Item(1, 1)
$value.Text 

# First retrieve the website
$result = Invoke-webrequest -Uri "https://www.website.com/$value" -Method Get
$resultTable = @{}

# Get the title
$resultTable.title = $result.ParsedHtml.title

# Return the table we have built as an object
Write-Output New-Object -TypeName PSCustomObject -Property $resultTable

#close excel
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
spps -n Excel

一旦它访问域,它应该打印页面标题,我也成功地工作了。非常感谢任何帮助,并提前感谢您。

4

1 回答 1

0

首先,我认为不是.Text你想使用Value2

查看这篇stackoverflow 帖子,了解 PowerShell 中的字符串插值。

你可以这样做:

"https://www.website.com/$($value.Value2)"

或使用第二个变量:

$cell = $WorkbookTotal.Cells.Item(1, 1)
$value = $value.Value2
$result = Invoke-webrequest -Uri "https://www.website.com/$value" -Method Get

这能回答您的问题还是您有其他问题?

于 2020-11-05T19:14:56.107 回答