Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell从网站下载Excel文档_Powershell - Fatal编程技术网

使用Powershell从网站下载Excel文档

使用Powershell从网站下载Excel文档,powershell,Powershell,最近,我开始学习PowerShell来自动化我的工作任务 所以我想访问一个网页,点击一个按钮,自动下载一个Excel文件。这是我要单击的按钮: <div class="NormalButton"> <a class="ActiveLink" title="Excel" alt="Excel" onclick="$find('ctl32').exportReport('EXCELOPENXML');" href="javascript:void(0)" style="paddi

最近,我开始学习PowerShell来自动化我的工作任务

所以我想访问一个网页,点击一个按钮,自动下载一个Excel文件。这是我要单击的按钮:

<div class="NormalButton">

<a class="ActiveLink" title="Excel" alt="Excel" onclick="$find('ctl32').exportReport('EXCELOPENXML');" href="javascript:void(0)" style="padding:3px 8px 3px 8px;display:block;white-space:nowrap;text-decoration:none;">Excel</a>

</div>
如果我尝试从控制台运行它,我会收到一个错误,即您无法对空值表达式调用方法


如果有用的话,我正在使用PowerShell 4.0,并且网页在加载报告之前会有延迟

我认为您的问题是因为您使用的是双引号,powershell会像变量一样威胁它并尝试扩展它,所以请尝试将其更改为单引号,从:

$link = $ie.Document.getElementsByTagName('a') | Where-Object {$_.onclick -eq "$find('ctl32').exportReport('EXCELOPENXML');"}
致:

此外,您还可以将-eq更改为-match,并只取其中的一部分,如:

$_.onclick -match '$find('ctl32').exportReport'

有关更多信息,请参见:

我已通过以下代码完成此操作:

[void][System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")


$url = "https://webpage.com"


$ie = New-Object -com internetexplorer.application
$ie.navigate($url)
$ie.StatusBar = $false
$ie.ToolBar = $false
$ie.visible = $true



#Get Excel

Start-Sleep -s 40


$btnExcel = $ie.Document.links | where-object { $_.outerText -eq 'Excel' -and $_.innerText -eq 'Excel' }
$btnExcel.click()



# Get Internet Explorer Focus

Start-Sleep -s 5

[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::SendWait("{F6}");
[System.Windows.Forms.SendKeys]::SendWait("{TAB}");
[System.Windows.Forms.SendKeys]::SendWait(" ");
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait("$file");
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}");



# Get Internet Explorer Focus

Start-Sleep -s 1

[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::SendWait("^{F4}");

感谢大家的时间:

错误与哪个表达式有关?错误被关联到$link。单击文件路径是否更改?如果不调用webrequest可能没问题。路径不会改变,如果我尝试调用webrequest,它会告诉我im未经授权将无法工作,如果我使用单引号,它会给我另一个错误,如果我使用-match with double quotes仍然有旧的问题,我的意思是$link=$ie.Document.getElementsByTagName'a',然后手动搜索onclick对象并显示结果,它找到了什么吗?明白了,它说没有方法“getElementByTagName”
$_.onclick -match '$find('ctl32').exportReport'
[void][System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")


$url = "https://webpage.com"


$ie = New-Object -com internetexplorer.application
$ie.navigate($url)
$ie.StatusBar = $false
$ie.ToolBar = $false
$ie.visible = $true



#Get Excel

Start-Sleep -s 40


$btnExcel = $ie.Document.links | where-object { $_.outerText -eq 'Excel' -and $_.innerText -eq 'Excel' }
$btnExcel.click()



# Get Internet Explorer Focus

Start-Sleep -s 5

[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::SendWait("{F6}");
[System.Windows.Forms.SendKeys]::SendWait("{TAB}");
[System.Windows.Forms.SendKeys]::SendWait(" ");
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait("$file");
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}");



# Get Internet Explorer Focus

Start-Sleep -s 1

[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::SendWait("^{F4}");