Invoke-webrequest 使用 PowerShell 在 Excel 中使用特定的单元格值
Invoke-webrequest using specific cell value in Excel using PowerShell
我正在尝试从 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
一旦它访问域,它应该打印页面标题,我也成功地工作了。非常感谢任何帮助,并在此先感谢您。
首先,我认为你想使用 Value2
而不是 .Text
查看 Whosebug post 了解 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
这是否回答了您的问题或您是否还有其他问题?
我正在尝试从 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
一旦它访问域,它应该打印页面标题,我也成功地工作了。非常感谢任何帮助,并在此先感谢您。
首先,我认为你想使用 Value2
而不是.Text
查看
你可以这样做:
"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
这是否回答了您的问题或您是否还有其他问题?