我想将一个变量传递给SessionVariable
作为参数,以便我可以为SessionObject
使用任何名称,以便我可以测试/发送到其他函数,但不希望它被硬编码……即,传递$TCSessionVar
而不是硬编码TCSessionVar
参数。
$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
}
我得到的错误是:
-注意:我可以从响应中创建WebReuestSessionObject,但是SessionVariable毫无意义。
$TCCookieName="TCSESSIONID"
$Cookies=$Response.Headers.'set-cookie'
$TCCookie= $cookies.Split(";")| Where-Object {$_ -match $TCCookieName}
$CookieName=$TCCookie.Split("=")[0]
$CookieValue=$TCCookie.Split("=")[1]
$TCSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$cookie = New-Object System.Net.Cookie
$cookie.Name = $CookieName
$cookie.Value = $CookieValue
$cookie.Domain=([uri]$TeamCityUrl).Host
$TCSession.Cookies.Add($cookie);
最佳答案
有用!
$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
关于powershell - Invoke-WebRequest中的SessionVariable参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44695645/