SessionVariable在Invoke-WebRequest中进行参数化

时间:2017-06-22 09:37:24

标签: powershell cookies webrequest

我想将一个变量作为参数传递给@IBAction func viewProfileButtonPressed(_ sender: UIButton) { let stb = UIStoryboard(name: "Main", bundle: nil) let tabBar = stb.instantiateViewController(withIdentifier: "tabBar") as! TabBarViewController let sharkTabBar = stb.instantiateViewController(withIdentifier: "sharkTableView") as! SharksTableViewController let sharkProfile = stb.instantiateViewController(withIdentifier: "sharkProfile") as! SharkProfileTableViewController sharkProfile.selectedShark = shark as JSONObject tabBar.selectedIndex = 3 self.present(tabBar, animated: true) { } } ,以便我可以使用SessionVariable的任何名称,以便我可以测试/发送到其他函数,并且不高兴它是正在硬编码...即传递SessionObject而不是硬编码$TCSessionVar参数。

TCSessionVar

我得到的错误是:

  

无法转换" Microsoft.PowerShell.Commands.WebRequestSession" type" System.String"的值输入" Microsoft.PowerShell.Commands.WebRequestSession"。

- 注意:我可以从响应中创建WebReuestSessionObject,但是SessionVariable是没有意义的。

$Response= Invoke-WebRequest -Uri $TeamCityUrl `
   -SessionVariable $TCSessionVar -Method `
   Get -Headers @{"Authorization"="Basic $Encoded"}

$ReturnObject =New-Object PSCustomObject @{
    SessionVar=[Microsoft.PowerShell.Commands.WebRequestSession]$TCSessionVar
    Response=$Response
}   

2 个答案:

答案 0 :(得分:3)

有效!

$sess = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$IWRParams = @{
    SessionVariable = Get-Variable -name sess -ValueOnly
    Method = 'GET'
    Uri = "$($Server)/login"
    Headers = @{'X-Redmine-API-Key'=$Key}
}
$Response = Invoke-WebRequest @IWRParams
$this.CSRFToken = $Response.Forms.Fields['authenticity_token']
$this.Session = Get-Variable -name $sess -ValueOnly

答案 1 :(得分:1)

如果我有效     -SessionVariable (get-variable -name "TCSessionVar" -ValueOnly)并获得响应:(get-variable -name (get-variable "TCSessionVar" -valueonly) -valueonly)

$Response= Invoke-WebRequest -Uri $TeamCityUrl -SessionVariable (get-variable -name "TCSessionVar" -ValueOnly) -Method Get -Headers @{"Authorization"="Basic $Encoded"}
$ReturnObject =New-Object PSCustomObject @{
        SessionVar=[Microsoft.PowerShell.Commands.WebRequestSession](get-variable -name (get-variable "TCSessionVar" -valueonly) -valueonly)
        Response=$Response
}