【发布时间】:2015-12-29 20:09:02
【问题描述】:
我正在尝试使用Invoke-WebRequest,但出现错误。
我认为哈希表引起了问题。我该如何解决这个错误?
$loginBase = "https://itunesconnect.apple.com";
$loginUrl = "$($loginBase)/WebObjects/iTunesConnect.woa";
# Load the login-page
$r = Invoke-WebRequest $loginUrl -SessionVariable ws;
# Use the session variable that you created in Example 1. Output displays
# values for Headers, Cookies, Credentials, etc.
$ws;
# Gets the first form in the Forms property of the HTTP response object in the
# $r variable, and saves it in the $form variable.
$form = $r.Forms[0];
# Displays the keys and values in the hash table (dictionary) object in the
# Fields property of the form.
$form.fields
# The next two commands populate the values of the "email" and "pass" keys of
# the hash table in the Fields property of the form. Of course, you can replace
# the email and password with values that you want to use.
#$form.Fields["email"] = "User01@Fabrikam.com"
#$form.Fields["pass"] = "P@ssw0rd"
# I changed like this, following above example.
# Set the login data
$form.Fields["Apple ID"] = "1234@yahoo.com";
$form.Fields["Password"] = "myP2ssword";
# Log in
$r = Invoke-WebRequest -Uri ($loginBase + $form.Action) -WebSession $ws -Method $form.Method -Body $form.Fields
$r.StatusDescription
我收到此错误。
Cannot index into a null array.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:26 char:1
+ $form.Fields["Apple ID"] = "1234@yahoo.com";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:27 char:1
+ $form.Fields["Password"] = "myP2ssword";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Invoke-WebRequest : Cannot bind parameter 'Method' to the target. Exception
setting "Method": "Object reference not set to an instance of an object."
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:30 char:79
+ $r=Invoke-WebRequest -Uri ($loginBase + $form.Action) -WebSession $ws -Method $f ...
+ ~~
+ CategoryInfo : WriteError: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Invoke-WebRequest : {"data":null,"messages":{"warn":null,"info":null,"error":["Unauthorized access"]},"statusCode":"ERROR"}
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:35 char:13
+ $response = Invoke-WebRequest $jsonSummaryUrl -WebSession $ws;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:36 char:27
+ $json = $response.Content|ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
所以我这样编辑:
$loginBase = "https://itunesconnect.apple.com";
$loginUrl = $loginBase + "/WebObjects/iTunesConnect.woa";
# Load the login-page
$r = Invoke-WebRequest $loginUrl -SessionVariable ws;
# Use the session variable that you created in Example 1. Output displays
# values for Headers, Cookies, Credentials, etc.
$ws;
# Gets the first form in the Forms property of the HTTP response object in the
# $r variable, and saves it in the $form variable.
$form = $r.Forms[0];
# Displays the keys and values in the hash table (dictionary) object in the
# Fields property of the form.
$form.fields
# The next two commands populate the values of the "email" and "pass" keys of
# the hash table in the Fields property of the form. Of course, you can replace
# the email and password with values that you want to use.
#$form.Fields["email"] = "User01@Fabrikam.com"
#$form.Fields["pass"] = "P@ssw0rd"
# I changed like this, following above example.
# Set the login data
$form.Fields["Apple ID"] = "myusername";
$form.Fields["Password"] = "mypassword";
# Log in
$r = Invoke-WebRequest -Uri ($loginBase + $form.Action) -WebSession $ws -Method $form.Method -Body $form.Fields
$r.StatusDescription
错误还是一样。
Cannot index into a null array.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:27 char:1
+ $form.Fields["Apple ID"] = "myusername";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:28 char:1
+ $form.Fields["Password"] = "mypassword";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Invoke-WebRequest : Cannot bind parameter 'Method' to the target. Exception
setting "Method": "Object reference not set to an instance of an object."
At C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:31 char:81
+ ... on $ws -Method $form.Method -Body $form.Fields
+ ~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
这是重新编辑。 非常感谢您的帮助。
$loginBase = "https://itunesconnect.apple.com";
$loginUrl = $loginBase + "/WebObjects/iTunesConnect.woa/wo";
# Load the login-page
$r = Invoke-WebRequest $loginUrl -SessionVariable ws;
# Use the session variable that you created in Example 1. Output displays
# values for Headers, Cookies, Credentials, etc.
$ws;
# Gets the first form in the Forms property of the HTTP response object in the
# $r variable, and saves it in the $form variable.
$form = $r.Forms[0];
# Displays the keys and values in the hash table (dictionary) object in the
# Fields property of the form.
$form.fields
# The next two commands populate the values of the "email" and "pass" keys of
# the hash table in the Fields property of the form. Of course, you can replace
# the email and password with values that you want to use.
#$form.Fields["email"] = "User01@Fabrikam.com"
#$form.Fields["pass"] = "P@ssw0rd"
# I changed like this, following above example.
# Set the login data
$form.Fields["Apple ID"] = "myusername";
$form.Fields["Password"] = "mypassword";
# Log in
$r = Invoke-WebRequest -Uri ($loginBase + $form.Action) -WebSession $ws -Method $form.Method -Body $form.Fields
$r.StatusDescription
# above this is totally working, under this I am having issue
$jsonSummaryUrl = "https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa/ra/apps/manageyourapps/summary";
$response = Invoke-WebRequest $jsonSummaryUrl -WebSession $ws;
$json = $response.Content|ConvertFrom-Json
# Show me what apps I have
$json.data.summaries
错误是
Invoke-WebRequest : {"data":null,"messages":{"warn":null,"error":["Unauthorized access"],"info":null},"statusCode":"ERROR" } 在 C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:36 char:13 + $response = Invoke-WebRequest $jsonSummaryUrl -WebSession $ws; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand ConvertFrom-Json:无法将参数绑定到参数“InputObject”,因为它为空。 在 C:\Users\kazuakimai\Desktop\AzurePowerShellISE\Itune.ps1:37 char:27 + $json = $response.Content|ConvertFrom-Json + ~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
【问题讨论】:
-
您发布的错误与您发布的代码不匹配。
-
@AnsgarWiechers,对不起,我编辑了,如果您知道解决方案,请告诉我。非常感谢。
-
@Kazu,错误告诉我们
$form.Fields["Apple ID"] = "myusername";失败,好像$form的值是$null。如果对$loginUrl的 Web 请求失败,就会发生这种情况。$r.StatusCode等于什么?你希望它是 200。 -
@AnsgarWiechers,感谢您的回复,我的密码和用户名绝对正确,所以我对如何解决这个问题感到困惑。我应该在哪里以及如何改变它
标签: arrays powershell