title: Invoke-WebRequest, hashtable, Cannot index into a null array
body: <p>I'm trying to use <a href="https://technet.microsoft.com/en-us/library/hh849901.aspx" rel="nofollow"><code>Invoke-WebRequest</code></a>, but I am getting an error.</p>
  
  <p>I assume that the hash table is causing issues. How can I fix this error?</p>
  
  <pre class="lang-psh prettyprint-override"><code>$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
  </code></pre>
  
  <p>I am getting this error.</p>
  
  <pre class="lang-none prettyprint-override"><code>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
  </code></pre>
  
  <p><strong>So I edited like this:</strong></p>
  
  <pre><code>$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
  </code></pre>
  
  <p>The error is still same.</p>
  
  <pre class="lang-none prettyprint-override"><code>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
  </code></pre>
  
  <p><strong>This is re-re edited.
  Thank you so much for the help.</strong> </p>
  
  <pre><code>$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
  </code></pre>
  
  <p>the error is</p>
  
  <p>Invoke-WebRequest : {"data":null,"messages":{"warn":null,"error":["Unauthorized access"],"info":null},"statusCode":"ERROR"}
  At 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 : Cannot bind argument to parameter 'InputObject' because it is null.
  At 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</p>