我们有一个在Cognos下运行的服务器容量报告网站。
该网站已进行身份验证登录。然后只需输入服务器/主机名,您就会看到报告的不同链接,例如CPU / Memory&磁盘利用率。
该网站生成利用率的图像/图表。我希望能够下载/保存图像。
到目前为止,我所拥有的是......
## Initialize variables
$username = "TAUser1"
$password = "Pas$W0rd"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$Folder = "D:\Data\Scripts\Cognos\Images\"
## Link to CPU Util
$Url1 = "https://XYZ..com:12345/tarf/servlet/dispatch?b_action=cognosViewer&ui.action=run&ui.object=%2fcontent%2fpackage%5b%40name%3d%27Windows_OS_Reports%27%5d%2freport%5b%40name%3d%27WI_NT_ProcessorTotal%27%5d&ui.name=WI_NT_ProcessorTotal&run.outputFormat=&run.prompt=false&p_TCRDateFilter=<selectChoices><selectOption useValue=%22%20between cast( cast(_add_days(current_date,-30) as char(10) ) || ' 00:00:00.000' as TIMESTAMP) and cast( cast( current_date as char(10) ) || '23:59:59.999' as TIMESTAMP)%22 displayValue='Last 30 Days'/></selectChoices>&p_Param_Server=Primary:MyServer:NT"
## Open URL & Login
$ie.navigate($Url1)
while($ie.ReadyState -ne 4) {start-sleep -m 200}
$ie.document.getElementById("CAMUsername").value= "$username"
$ie.document.getElementById("CAMPassword").value = "$password"
$ie.document.getElementById("cmdOK").Click()
start-sleep 20
## -- after logon the image/chart of the CPU util is shown
## Get the link for the image/chart
$sources = $ie.document.getElementsByTagName('img') | Select-Object -ExpandProperty src -Last 1
## The link looks something like this .. dynamic?
# >> https://XYZ..com:12345/tarf/servlet/dispatch?b_action=dc&f=b3V0cHV0MzMyMTQwMTY5NDExMzQ3OTE_&k=FAAAAMoy9pBPf6OTheytEtxRbuihvYL-9uQyBEGBYwDFXnprWn6KQpc5Zig_&s=FAAAAMoy9pBPf6OTheytEtxRbuihvYL-JbPEBnix8kFdZ2*-LnYbI3NszrU_&did=2013-03-21-08.24.10.711381
## I am able to go/navigate to this link, show me the image.
$ie.navigate($sources)
## The image on that link ($sources) is the one i want to be able to download.
如果您有任何想法,请告诉我。提前谢谢。
答案 0 :(得分:0)
我使用PowerShell的内置Invoke-WebRequest及其SessionVariable / WebSession标志来取得巨大成功。
以下是如何将其用于您的方案的示例。我没有测试过这个,因为我无法访问Cognos服务器。您可能需要调整它,但基础知识就在这里。
# This is needed for UrlEncode.
[Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null
# These are the posted items. Expand if necessary.
# You may need "cmdOk" = <something>;
$postItems = @{
"CAMUsername" = $username;
"CAMPassword" = $password;
}
# This builds the POST data for the login.
$encodedItems = $postItems.Keys | foreach { "$([System.Web.HttpUtility]::UrlEncode($_))=$([System.Web.HttpUtility]::UrlEncode($postItems[$_]))" }
$body = $encodedItems -join "&"
# Passing SessionVariable lets us keep the requests under one "browser" session.
$mainPage = Invoke-WebRequest $url -SessionVariable $session -Body $body -Method Post
# PowerShell's WebRequest provides a convenient Images array with all of the images. Query this as necessary.
$imgSrc = $mainPage.Images | Select-Object -ExpandProperty src -Last 1
$imgName = [System.IO.Path]::GetFileName($p.Images[0].src)
$imgName += ".png" # Append the correct extension here!
# Finally, grab the img using the logged in session and write it out to a file as byte encoded.
$outPath = Join-Path $Folder $imgName
Invoke-WebRequest $imgSrc -WebSession $session | Select -ExpandProperty Content | Set-Content -Encoding Byte $outPath
神奇的是将-SessionVariable $session
传递给第一个请求。这会启动会话,就像您使用浏览器一样,这样您就可以执行登录等操作。您可以使用-WebSession $session
将其传递给将来的请求。
根据服务器登录功能的工作方式,您可能需要将早期的Invoke-WebRequest直接启动到登录页面,然后使用WebRequest Forms成员从那里获取相应的表单项。例如,我遇到了生成服务器端令牌的页面,该令牌期望使用登录凭据传递。我建议使用浏览器的开发人员工具来确切地检查发布的内容并使用$postItems
重现这一点。