2

所以这是我想要做的事情的范围:

获取多个站点中 Windows 计算机的远程计算机信息,并将找到的信息写入 Active Directory 中每个计算机对象的 .Description 属性。如果脚本无法连接到远程计算机,请将该信息记录到文本文件中,并且不要对无法连接的计算机对象进行任何更改。

为了计算脚本运行的时间,我有第二个脚本来测量执行时间。

我将此设置作为计划任务运行第二个脚本(调用第一个脚本),该脚本通过 Windows 7 Pro 虚拟机上的批处理文件执行。

我的问题是,根据我在日志中看到的信息,我相信脚本可能会遇到内存问题。任何有关可能诊断根本原因的帮助将不胜感激。没有进一步的告别,这是我的两个脚本的代码以及奇怪的日志输出示例。

主脚本(脚本 1):

set-location \\myscriptcomputer\c$\somefolder\PSScripts

enter code here`function Measure-Latest {
    BEGIN { $latestlogon = $null }
    PROCESS {
            if (($_ -ne $null) -and (($latestlogon -eq $null) -or ($_ -gt $latestlogon))) {
                $latestlogon = $_ 
            }
    }
    END { $latestlogon }
}
Function CreateLog {

#Create a log file
    $global:path = "C:\Somefolder\PSScripts\WriteComputerDescriptions"
    $global:LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
    $global:LogName = 'CompDescriptions'
    $global:LogFile = 'C:\Somefolder\PSScripts\WriteComputerDescriptions\'+$LogName+$LogTime+'.txt'
    Write-Host "Creating log file" -foregroundcolor yellow
    if([IO.Directory]::Exists($global:path))
    {
        #Do Nothing
    }
    else
    {
        New-Item -ItemType directory -Path C:\Somefolder\PSScripts\WriteComputerDescriptions
    }

    cd C:\Somefolder\PSScripts\WriteComputerDescriptions
    echo "WriteComputerDescriptions Script Log" >> $global:logfile

}

Function WriteDescription {
    Write-Host "Gathering Computer information..." -foregroundcolor yellow
    $UserWorkstations = get-qadcomputer -sizelimit 0 -includeallproperties -searchroot my.domain.com/MyUserWorkstations

    $IPv4Regex = "^(\d{1,3}\.){3}\d{1,3}$"
    foreach ($computerobject in $UserWorkstations) {

    $computerIP = $NULL
    $computerIP2 = $NULL
    $computerIP3 = $NULL
    $computerserial = $NULL
    $computerserial2 = $NULL
    $findlastuser = $NULL
    $findlastuser2 = $NULL
    $lastlogontime = $NULL
    $findlastuserFname = $NULL
    $findlastuserFname2 = $NULL
    $findlastuserLname = $NULL
    $findlastuserLname2 = $NULL
    $fullname = $NULL
    $userlogon = $NULL

    $computerName = $computerobject.name
    $oldcomputerdescription = $computerobject.description
    Write-Host " "
    Write-Host "Testing connection to $computerName ..."

    $testConnection = test-connection -computername $computerName -count 2 -quiet
    Write-Host "Connection is $testconnection"


        if ($testConnection -eq $True) {
            $Connect = $testConnection

        #get IP address(es)
            try {
                $computerIP = get-wmiobject -class win32_networkadapterconfiguration -filter IPEnabled=TRUE -computername $computerName
                $computerIP2 = $computerIP.ipaddress[0]
                $computerIP3 = $computerIP.ipaddress[1]
                Write-Host = $computerIP2
                if ($computerIP3 -match $IPv4Regex){
                    Write-Host = $computerIP3
                    }
                }
             catch [system.exception]{
                $connect = $False
                Write-Host "Could not connect to $computerName. No IP collected."
                }
        #get computer serial
            try {
                $computerSerial = gwmi win32_bios -computername $computerName | select serialnumber
                $computerserial2 = $computerSerial.serialnumber.tostring()
                }
            catch [system.exception]{
                Write-Host "Could not get serial for $computerName."
                $computerSerial = "Unavailable"
                $computerSerial2 = "Unavailable"
                }
        #get username of currently logged in user
            try {
                $findlastUser = gwmi win32_computersystem -computer $computerName | select username
                $findlastuser2 = ($findlastUser.username).replace("mydomain\","")
                }
            catch [system.exception]{
                Write-Host "Could not get username of logged in user on $computerName"
                $findlastUser = "Unavailable"
                $findlastUser2 = "Unavailable"
                }

        #get last logon time of user
            try {
                if($findlastuser2 -ne $NULL -and $findlastuser2 -notlike "Unavailable") {
                    #ignore domain controllers in a datacenter due to connectivity stuff
                    $lastlogontime = get-qadcomputer -computerrole domaincontroller | where { $_.name -notmatch "-COLO"} | foreach {(get-qaduser -service $_.name -samaccountname $findlastuser2).LastLogon } | Measure-Latest
                    }
                }
            catch {
                if ($lastlogontime -eq $NULL -and $findlastuser2 -eq $NULL){
                    Write-Host "Could not find a last logon time"
                    Write-Host "No username available to query"
                    $lastlogontime = "Unavailable"
                    }
                if ($lastlogontime -eq $NULL -and $findlastuser2 -ne $NULL){
                    Write-Host "Could not find a last logon time for user $findlastuser"
                    $lastlogontime = "Unavailable"
                    }
                }

        #search AD for the user identified, select first name
            try {
                $findlastuserFname = get-qaduser $findlastuser2 | select firstname
                $findlastuserFname2 = $findlastuserFname.firstname.tostring()
                }
            catch [system.exception]{
                if ($findlastuserFname2 -eq $NULL) {
                    Write-Host "No first name for user found"
                    }
                }
        #search AD for the user identified, select last name
            try {
                $findlastuserLname = get-qaduser $findlastuser2 | select lastname
                $findlastuserLname2 = $findlastuserLname.lastname
                }
            catch [system.exception] {
                if ($findlastuserLname2 -eq $NULL) {
                    Write-Host "No last name for user found"
                    }
                }       
        #join the first and last names together if both properties are available

            if ($findlastuserFname2 -ne $NULL -and $findlastuserLname2 -ne $NULL){
                $fullname = "$findlastuserFname2" + " $findlastuserLname2"
                }
            elseif ($findlastuserFname2 -eq $NULL -and $findlastuserLname -ne $NULL){
                $fullname = $findlastuserLname2
                }
            elseif ($findlastuserFname2 -ne $NULL -and $findlastuserLname -eq $NULL){
                $fullname = $findlastuserFname2
                }
            else {
                $fullname = "Unavailable"
                }

        #Set the description data format
            #With only 1 IPv4 Address
            if ($computerIP3 -notmatch $IPv4Regex -or $computerIP3 -eq $NULL){
                $newcomputerdescription = "$fullname | $computerIP2 | $computerSerial2 | $lastlogontime"
                }

            #With 2 IPv4 Addresses

            if ($computerIP3 -match $IPv4Regex) {
                $newcomputerdescription = "$fullname | $computerIP2, $computerIP3 | $computerSerial2 | $lastlogontime"
                }

        #If the description data is the same, leave it as it is
            if ($newcomputerdescription -eq $oldcomputerdescription){
                Write-Host " "
                Write-Host "Information for $computerName has not" -foregroundcolor yellow
                Write-Host "changed. No edits were made on this object." -foregroundcolor yellow
                }

            if ($newcomputerdescription -ne $oldcomputerdescription -and $Connect -eq $TRUE) {
                set-qadcomputer -identity $computerName -Description $newcomputerdescription
                Write-Host " "
                Write-Host "Computer description updated for object $computerName" -foregroundcolor yellow
                Write-Host "New host information:"
                Write-Host "$newcomputerdescription"
                }
            }
        else {
            Write-Host "Could not connect to computer $computerName"
            Write-Host "No changes made to description for $computerName"
            $noconnecterror = "Could not connect to computer $computerName"
            $noconnecterror | Out-File $global:logfile -Append -Force
        }
    }
    Write-Host "Processing complete!"
}
CreateLog -erroraction silentlycontinue
WriteDescription -erroraction silentlycontinue
start-sleep -s 3
##END OF SCRIPT

第二个脚本:

set-location \\myscriptcomputer\c$\somefolder\PSScripts
Add-PSSnapin Quest.ActiveRoles.ADManagement -erroraction SilentlyContinue
$timeoutput = Measure-Command {\\myscriptcomputer\c$\Somefolder\PSScripts\WriteComputerDescriptions.ps1}
cd \\myscriptcomputer\c$\Somefolder\PSScripts\WriteComputerDescriptions
$scriptlog = get-childitem | sort creationtime | select -last 1
$logname = $scriptlog.name
Add-Content c:\somefolder\PSScripts\WriteComputerDescriptions\$logname "`nExecution Time: $timeoutput"
Write-Host "Script complete!"
Start-sleep -s 3
exit

在我的环境 Active Directory 中的结果中,这对数百个对象有效,但这是我在日志文件中看到的示例:

无法连接电脑computer391 无法连接电脑computer392 无法连接电脑computer393 无法连接电脑computer394 䔊数畣楴汤吠浩㩥ㄠ㨱㘰㈺⸱㜵㤵㐰ഷ</p>

乱码文本的最后一行让我认为可能存在与内存相关的问题。如果我在计算机数量少得多的容器/OU 上运行我的脚本,我日志中的最后一行是一个时间,这是我通常所期望的。

如果任何经验丰富的 Powershell 专业人士可以在这里提供一些建议,我将非常感谢您的帮助。

谢谢!

4

1 回答 1

0

我不知道为什么我的评论没有被添加。无论如何,让我在这里发布它。为了跟踪空闲内存,您只需查看它的性能计数器。

这是powershell命令:

Get-Counter -Counter "\Memory\Available MBytes"
于 2013-10-18T22:24:10.493 回答