none
Script is running from powershell ISE bu not from powershell RRS feed

  • Question

  • Hi,

    I am getting below error while running the script from powershell but same is working fine from Powershell ISE

    Unable to index into an object of type System.Int32.
    At D:\Scripts\AD_Replication_Monitor.ps1:49 char:13
    +             $next_index_msg = $index_array_dst[$next_index]
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (7:Int32) [], RuntimeException
        + FullyQualifiedErrorId : CannotIndex

    Can somebody help me resolve the problem

    Please find the complete script below

    #Variable
    $report_path = "D:\Replication_Status"
    $date = (get-date).tostring("yyyyMMddHHmmss")
    $array = @()
     
    #Powershell Function to delete files older than a certain age
    $intFileAge = 8  #age of files in days
    $strFilePath = $report_path #path to clean up
     
    #create filter to exclude folders and files newer than specified age
    Filter Select-FileAge {
          param($days)
          If ($_.PSisContainer) {}
                  # Exclude folders from result set
          ElseIf ($_.LastWriteTime -lt (Get-Date).AddDays($days * -1))
                {$_}
    }
    get-Childitem -recurse $strFilePath | Select-FileAge $intFileAge 'CreationTime' |Remove-Item
     
    Function send_mail([string]$message,[string]$subject) {
        $emailFrom = "AD_Repl_Status@xyz.com"
        $emailTo = "test@xyz.com"
        #$emailCC = ""
        $smtpServer = "mysmtp.domain.com"
        Send-MailMessage -SmtpServer $smtpServer -To $emailTo -From $emailFrom -Subject $subject -Body $message -BodyAsHtml
    }
     
    $myForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
    $dclist = $myforest.Sites | % { $_.Servers }
     
    $html_head = "<style type='text/css'>
    table {font-family:verdana,arial,sans-serif;font-size:12px;color:#333333;border-width: 1px;border-color: #729ea5;border-collapse: collapse;}
    th {font-family:verdana,arial,sans-serif;font-size:12px;background-color:#acc8cc;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;text-align:left;}
    tr {font-family:verdana,arial,sans-serif;background-color:#d4e3e5;}
    td {font-family:verdana,arial,sans-serif;font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;}
    </style>"
     
    foreach ($dcname in $dclist){
        $source_dc_fqdn = ($dcname.Name).tolower()
        $ad_partition_list = repadmin /showrepl $source_dc_fqdn | select-string "dc="
        foreach ($ad_partition in $ad_partition_list) {
            [Array]$NewArray=$NULL
            $result = repadmin /showrepl $source_dc_fqdn $ad_partition
            $result = $result | where { ([string]::IsNullOrEmpty(($result[$_]))) }
            $index_array_dst = 0..($result.Count - 1) | Where { $result[$_] -like "*via RPC" }
            foreach ($index in $index_array_dst){
                $dst_dc = ($result[$index]).trim()
                $next_index = [array]::IndexOf($index_array_dst,$index) + 1
                $next_index_msg = $index_array_dst[$next_index]
                $msg = ""
                if ($index -lt $index_array_dst[-1]){
                    $last_index = $index_array_dst[$next_index]
                }
                else {
                    $last_index = $result.Count
                }
                
                for ($i=$index+1;$i -lt $last_index; $i++){
                    if (($msg -eq "") -and ($result[$i])) {
                        $msg += ($result[$i]).trim()
                    }
                    else {
                        $msg += " / " + ($result[$i]).trim()
                    }
                }
                $Properties = @{source_dc=$source_dc_fqdn;NC=$ad_partition;destination_dc=$dst_dc;repl_status=$msg}
                $Newobject = New-Object PSObject -Property $Properties
                $array +=$newobject
            }
        }
    }
     
    $status_repl_ko = "<br><br><font face='Calibri' color='Red'><i><b>Problem Found in AD Replication :</b></i><br>"
    $status_repl_ok = "<br><br><font face='Calibri' color='Green'><i><b>Active Directory Replication OK :</b></i><br>"
    $subject = "AD_Replication_Status : "+$date
    #$message = "<br><br><font face='Calibri' color='black'><i>The full Active Directory Replication report is available <a href=" + $report_path + "\ad_repl_status_$date.html>here</a></i><br>"
    #$message += $status_repl_ko
     
    if ($array | where {$_.repl_status -notlike "*successful*"}){
        $status_repl_ko += $array | where {$_.repl_status -notlike "*successful*"} | select source_dc,nc,destination_dc,repl_status |ConvertTo-Html -Head $html_head -Property source_dc,nc,destination_dc,repl_status
        send_mail $status_repl_ko $subject
    }

    Thanks

    Yogesh Pathak


    Yogesh Pathak

    Monday, January 29, 2018 3:47 AM

Answers

  • Problem got resolved, there were some issue with variables that causing script to be execute on powershell but bypass the error in ISE.

    Thanks to everyone for suggestion.

    Regards

    Yogesh Pathak


    Yogesh Pathak

    Tuesday, February 6, 2018 12:55 PM

All replies