2012-11-04 41 views
3

好吧似乎有幾個問題與腳本,我似乎無法弄清楚他們中的任何一個,也有一些腳本反饋將大大apperciated。這仍然是我的第一個腳本,所以它可能需要很多小的調整,所以請告訴我任何想法的想法。Powershell v2 |腳本不會聲明變量,需要幫助調試

問題:大部分問題都圍繞着日誌。

  1. 日誌文件未被檢查,因此腳本不斷地將計算機反覆添加到日誌文件中。

  2. 日誌文件不更新,如操作系統,MAC,IP等

問題正在顯示生成的信息:

住宅「結果」不能被發現在這個對象上。確保它存在 。爲W:\ PowerShell腳本\測試實驗室\ TestFunction.ps1:86 字符:17 + IF($計算機< < < <結果-ne 「成功」。){ + CategoryInfo:InvalidOperation:(:OperatorToken) ],RuntimeException的 + FullyQualifiedErrorId:PropertyNotFoundStrict

腳本

Set-PSDebug -strict 
Set-StrictMode -Version latest 
$consoleObject = (Get-Host).UI.RawUI 

# Adjustable Variables 
$Computers_Path = ".\computers.txt" 
$Log_Path = ".\Log.txt" 
$Log_MaxTick = 5 
$RunOnServers = 0 

# Multi-Threading Variables 
$Jobs_MaxAtOnce = 20 
$SleepTimer = 500 

# Script Specific Variables 
$ScriptTitle = "Local Admin Check" 

# Validate Adjustable Variables 
$Computers = @(Import-CSV $Computers_Path -header "Name","MAC") 

# Framework Specific Variables (Pre-Setting Variables) 
$Run = 0; $Succssful = 0; $Jobs_Count = 0; $Log_Tick= 0; $WriteToLog = "No" 

# Misc 
$Total = $Computers.length 
IF (!(Test-Path $Log_Path)) { Add-Content $Log_Path "Name,OS,Mac,IPAddress,Status,Attempts,Result,LastAttempt" } 
$Log = @(Import-Csv $Log_Path) 
$Successful = ($Log | Where-Object {$_.Result -eq "Successful"} | Select-String -inputobject {$_.Name} -pattern $Computers | Measure-Object).Count 

# Load Functions 
Function GetOS { 
    $RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer.Name) 
    $RegKey = $RegCon.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\") 
    $RegValue = $RegKey.GetValue("ProductName") 
    $RegCon.Close() 
    $Computer.OS = $RegValue 
} 

Function SmartLogging { 

    If ($Args[0] -eq "AddComputer") { 
     Add-Content ($Computer.Name + "," + $Computer.OS + "," + $Computer.Mac + "," + $Computer.IPAddress + "," + $Computer.Status + "," + $Computer.Attempts + "," + $Computer.Result + "," + $Computer.LastAttempt) -path .\log.txt 
    } ELSEIF ($Log_Tick -eq $Log_MaxTick -OR $Args -eq "Update") { 
     $Log_Tick = 0; 
     Get-Content $Log_Path | Foreach-Object {$_ -replace "$Computer.Name,.*", ($Computer.Name + "," + $Computer.OS + "," + $Computer.Mac + "," + $Computer.IPAddress + "," + $Computer.Status + "," + $Computer.Attempts + "," + $Computer.Result + "," + $Computer.LastAttempt)} | Set-Content $Log_Path 
    } ELSEIF ($Args[0] -eq "CheckComputer") { 
     IF (!($Log | Select-String -pattern $Computer.Name -SimpleMatch)) { 
      $Log += New-Object PSObject -Property @{ Name = $Computer.Name; OS = $NULL; Mac = $Computer.MAC; IPAddress = $NULL; Status = $NULL; Attempts = 0; Result = $NULL; LastAttempt = $NULL;} 
      $Computer = $Log | Where-Object {$_.Name -eq $Computer.Name} 
      SmartLogging AddComputer 
     } ELSE { 
      $Computer = $Log | Where-Object {$_.Name -eq $Computer.Name} 
     } 
    } ELSEIF (-not $Args[0]) { 
     "Log Ticked" 
     $Log_Tick++ 
    } 
} 

Function GetIPAddress { 
    $IPAddress = [System.Net.Dns]::GetHostAddresses("TrinityTechCorp") | Where-Object {$_.IPAddressToString -like "*.*.*.*"}; 
    $Computer.IPAddress = $IPAddress.IPAddressToString 
} 

Function WindowTitle { 
    [int]$Successful_Percent = $Successful/$Total * 100 
    $consoleObject.WindowTitle = 「$ScriptTitle - $Successful Out Of $Total ($Successful_Percent%) Successful `| Run`: $Run」 
} 

# Start Script 
while ($Successful -le $Total) { 
    $Run++ 
    ForEach ($Computer in $Computers) { 
     WindowTitle 
     SmartLogging CheckComputer 
     IF ($Computer.Result -ne "Successful") { 
      IF (test-connection $Computer.Name -quiet) { 
       $Computer.Status = "Awake" 
       IF (!$Computer.OS){GetOS} 
       IF (!$Computer.IPAddress){GetIPAddress} 
       ## Start Script ## 
        $CheckComputer = [ADSI]("WinNT://" + $Computer.Name + ",computer") 
        $Group = $CheckComputer.psbase.children.find("Administrators") 
        $members= $Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)} 
        ForEach($user in $members) { 
         $Result = $Computer.Name + "," + $user.ToString() 
         Add-Content $Result -path .\Result.csv 
        } 
       ## End Script ## 
       SmartLogging Update 
       $Computer.Result = "Successful" 
       $Successful += 1 
      } ELSE { 
       $Computer.Status = "Unknown" 
      } 
      $Computer.Attempts = [int] $Computer.Attempts + 1 
      $Computer.LastAttempt = Get-Date -uFormat "%I:%M:%S%p %d%b%y" 
     } 
    SmartLogging 
    } 
} 
Write-Output "Script Completed" 
+2

太多的代碼在那裏。您可能想要將樣本縮小到比可以輕鬆再現您遇到的問題更小的樣本。 – Neolisk

回答

1

計算機$收集的這一聲明....

$Computers = @(Import-CSV $Computers_Path -header "Name","MAC") 

...不會有這個情況的工作:

IF ($Computer.Result -ne "Successful") 

異常消息明確指出這一點,其中:

無法在此對象上找到屬性「結果」。確保它存在。 \ TestFunction.ps1:86字符:17

要解決這個問題,我會建議初始化結果的屬性,最有可能像這樣:

$Computers = @(Import-CSV $Computers_Path -header "Name","MAC","Result") 
1

的問題這時候就是你沒有Result成員$Computers收集部件,只有NameMAC。除非您稍後在您的代碼中添加這樣的成員,我並不想真正閱讀它,因爲它已經是大約100行,並且包含很多的代碼是而不是與實際問題陳述有關。

$Computers = @(Import-CSV $Computers_Path -header "Name","MAC") 

您是否嘗試過在Powershell ISE上調試腳本?它是Powershell 2的內置調試器。請關注一下Technet article

+0

作爲PS ISE的替代方案,您可以使用powerGUI腳本編輯器進行調試。它適用於v2。 – Neolisk