0

i have a problem with my PS code, i have to start my tests on browsers every tuesday in teamcity! and i wrote the function which goes on my server with credentials and try to make my tests on it

function HTTP-GetRequest($url, $username, $password)
{
 $properties = Resolve-Path "C:\Users\Uladzimir_Vaitsiakho\Documents\CI\Build\CI_2.0\vsphere\properties.ps1"
Write-Host $properties
. $properties

    $webRequest = [System.Net.WebRequest]::Create($url)
    $webRequest.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password 


    $webRequest.PreAuthenticate = $true
    $webRequest.Headers.Add("AUTHORIZATION", "Basic");

    [System.Net.WebResponse] $resp = $webRequest.GetResponse();
    $rs = $resp.GetResponseStream();
    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
    [string] $results = $sr.ReadToEnd();

    return $results
}
if (($today.DayOfWeek) -eq "Tuesday")
{
    if ($env:Browser -eq "Firefox"){
            $url = "http://1111111/httpAuth/action.html?add2Queue=bt6&&env.name=Browser&env.value=Chrome"
            HTTP-GetRequest $url, $teamcity_username, $teamcity_password
            Write-Host $teamcity_password
    Write-Host  $teamcity_username


    }
    if ($env:Browser -eq "Chrome"){
        $url = "http://11111/httpAuth/action.html?add2Queue=bt6&&env.name=Browser&env.Value=InternetExplorer"
        HTTP-GetRequest $url, $teamcity_username, $teamcity_password
    }
}

And have got the next output with troubles, but file with properties and credentials i gave to my function, what's can be wrong?!

Cannot convert argument "0", with value: "System.Object[]", for "Create" to type "System.Uri": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Uri"." At C:\Users\Uladzimir_Vaitsiakho\Documents\qw.ps1:6 char:47 + $webRequest = [System.Net.WebRequest]::Create <<<< ($url) + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Property 'Credentials' cannot be found on this object; make sure it exists and is settable. At C:\Users\Uladzimir_Vaitsiakho\Documents\qw.ps1:7 char:14 + $webRequest. <<<< Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password + CategoryInfo : InvalidOperation: (Credentials:String) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound

Property 'PreAuthenticate' cannot be found on this object; make sure it exists and is settable. At C:\Users\Uladzimir_Vaitsiakho\Documents\qw.ps1:10 char:14 + $webRequest. <<<< PreAuthenticate = $true + CategoryInfo : InvalidOperation: (PreAuthenticate:String) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression. At C:\Users\Uladzimir_Vaitsiakho\Documents\qw.ps1:11 char:25 + $webRequest.Headers.Add <<<< ("AUTHORIZATION", "Basic"); + CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression. At C:\Users\Uladzimir_Vaitsiakho\Documents\qw.ps1:13 char:58 + [System.Net.WebResponse] $resp = $webRequest.GetResponse <<<< (); + CategoryInfo : InvalidOperation: (GetResponse:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression. At C:\Users\Uladzimir_Vaitsiakho\Documents\qw.ps1:14 char:31 + $rs = $resp.GetResponseStream <<<< (); + CategoryInfo : InvalidOperation: (GetResponseStream:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

New-Object : Constructor not found. Cannot find an appropriate constructor for type System.IO.StreamReader. At C:\Users\Uladzimir_Vaitsiakho\Documents\qw.ps1:15 char:43 + [System.IO.StreamReader] $sr = New-Object <<<< System.IO.StreamReader -argumentList $rs; + CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression. At C:\Users\Uladzimir_Vaitsiakho\Documents\qw.ps1:16 char:35 + [string] $results = $sr.ReadToEnd <<<< (); + CategoryInfo : InvalidOperation: (ReadToEnd:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

4

1 回答 1

1

当前,对 HTTP-GetRequest 的调用正在将对象数组 ( $url, $teamcity_username , $teamcity_password) 传递给函数。从函数调用中删除逗号:

HTTP-GetRequest $url $teamcity_username $teamcity_password
于 2013-09-10T20:19:16.357 回答