我正在尝试编写一个脚本,powershell
该脚本将自动登录到一个使用构建的网站AngularJS
并将登录会话存储在一个变量中以供将来登录会话使用。有人可以告诉/告诉我如何解决这个问题吗?预先感谢一百万。
我以前做过,它总是成功地工作,只有在这种情况下,网站是用它构建的,angularJs
并且表单InputFields
隐藏在“Div”标签中。所以行不通。
这是表单的外观:
<form name="loginForm" class="login-form gf-form-group ng-pristine ng-invalid
ng-invalid-required" ng-hide="disableLoginForm">
<!---->
<div class="gf-form" ng-if="loginMode">
<span class="gf-form-label width-7">User</span>
<input type="text" name="username" class="gf-form-input max-width-14 ng-
pristine ng-untouched ng-empty ng-invalid ng-invalid-required"
required="" ng-model="formModel.user" placeholder="email or username">
</div>
<!---->
<!---->
<div class="gf-form" ng-if="loginMode">
<span class="gf-form-label width-7">Password</span>
<input type="password" name="password" class="gf-form-input max-width-14
ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required"
required="" ng-model="formModel.password" id="inputPassword"
placeholder="password">
</div>
<!---->
<!---->
<div class="gf-form-button-row">
<button type="submit" class="btn btn-large
p-x-3 btn-inverse" ng-click="submit();" ng-class="{'btn-inverse':
!loginForm.$valid, 'btn-primary': loginForm.$valid}">Log in</button>
</div>
</form>
这是我写的脚本(我通常使用的):
$LoginResponse = Invoke-WebRequest 'https://mywebsite/login' -SessionVariable 'sVar'
$Body = @{loginForm = $LoginResponse.ParsedHtml.forms[0].elements['loginForm'].value
username = 'MyUsername'
password = 'MyPassword'
}
$LoginResponse = Invoke-WebRequest 'https://mywebsite/login' -WebSession $Session -Body $Body -Method 'POST'
这是我在运行脚本时遇到的错误Powershell
:
Cannot index into a null array.
At line:3 char:1
+ $Body = @{
+ ~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Invoke-WebRequest : [{"fieldNames":["User"],"classification":"RequiredError","message":"Required"}]
At line:10 char:18
+ ... nResponse = Invoke-WebRequest 'https://mywebsite/login' -WebSessi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand