gpt4 book ai didi

powershell - Invoke-WebRequest中的SessionVariable参数

转载 作者:行者123 更新时间:2023-12-02 23:09:59 26 4
gpt4 key购买 nike

我想将一个变量传递给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
}

我得到的错误是:

Cannot convert the "Microsoft.PowerShell.Commands.WebRequestSession" value of type "System.String" to type "Microsoft.PowerShell.Commands.WebRequestSession".



-注意:我可以从响应中创建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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com