0

我想设置一个脚本来登录到特定的 SolarWinds 显示视图并每 30 秒不断刷新页面。我检查了其他几个代码示例,我的看起来不错,但它不起作用。

我的问题:自动登录不起作用,没有在字段中输入任何内容,也没有单击登录按钮。我也不知道如何让它刷新页面而不是每次打开一个新页面。

任何帮助表示赞赏!

这是我的代码:

$Run = 1

do {

#Create the IE com object to be used for logging into SAM
$ie = new-object -com InternetExplorer.Application 
$ie.visible = $true
$ie.ToolBar = $false
#$ie.Width = 1300
#$ie.Height = 1000
$ie.FullScreen = $true
$ie.Left = 0
$ie.Top = 0
$SolarWinds = "http://SolarWindsServer/Orion/Login.aspx"
$username = "username"
$password = "password"

$ie.navigate($SolarWinds)
do {
    Sleep 1000
   }while ($ie.Busy -eq "True")

sleep 30

#Find the username field and enter username 
$ie.Document.getElementById('ctl00_BodyContent_Username').value = $username;

#Find the password field and enter password
$ie.Document.getElementById('ctl00_BodyContent_Password').value = $password;

#Find and click the submit button 
$ie.Document.getElementById('SiteLoginText').submit() 

do {
    Sleep 5
   }while ($ie.Busy -eq "True")

sleep 30
}while ($Run -eq 1)

首先,我收到了这些错误,所以看起来登录不起作用:

您不能在空值表达式上调用方法。在 C:\Users\me\Desktop\SAMTEST.ps1:25 char:28 + $ie.Document.getElementById <<<< ('ctl00_BodyContent_Username').value = $username; + CategoryInfo : InvalidOperation: (getElementById:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

您不能在空值表达式上调用方法。在 C:\Users\me\Desktop\SAMTEST.ps1:28 char:28 + $ie.Document.getElementById <<<< ('ctl00_BodyContent_Password').value = $password; + CategoryInfo : InvalidOperation: (getElementById:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

您不能在空值表达式上调用方法。在 C:\Users\me\Desktop\SAMTEST.ps1:31 char:28 + $ie.Document.getElementById <<<< ('SiteLoginText').submit() + CategoryInfo : InvalidOperation: (getElementById:String) [] , RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

以下是页面源代码的片段:

<div id="gutter">
    <div id="dialog"><span class="crn crn-tl"></span><span class="crn crn-tr"></span><span class="crn crn-bl"></span><span class="crn crn-br"></span>
        <img id="logo" src="/orion/images/login_logo.png" width="182" height="50" alt="SolarWinds" />



        <div>
            <label for="username">User name:</label><div><input name="ctl00$BodyContent$Username" type="text" id="ctl00_BodyContent_Username" tabindex="1" automation="username" /></div>
        </div>

        <p class="sw-pg-suggestion">Enter domain\username or username@domain for windows accounts</p>

        <span id="ctl00_BodyContent_ctl04" class="sw-pg-errortext sw-validation-error" style="display:none;">
            <p>User name is required.</p>
        </span>

        <div>
            <label for="password">Password:</label><div><input name="ctl00$BodyContent$Password" type="password" id="ctl00_BodyContent_Password" tabindex="2" automation="password" /></div>
        </div>



        <div class="sw-btn-bar">
            <a tabindex="3" class=" sw-btn-primary sw-btn" automation="Login" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$BodyContent$ctl05&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))"><span class="sw-btn-c"><span class="sw-btn-t">Login</span></span></a>
        </div>
    </div>




    <div id="CookiesNotWorking" class="sw-pg-errortext sw-pg-align" style="display:none;"><p>Enable cookies to login</p></div>
    <noscript><div class="sw-pg-errortext sw-pg-align"><p>Enable JavaScript to login</p></div></noscript>

    <p id="SiteLoginText"></p>
</div>
4

1 回答 1

0

您最好只在 URL 上提供凭据。在这里查看我的答案:Automatically authenticate into SolarWinds and view data using a URL

于 2014-02-14T15:05:17.890 回答