2013-10-11 40 views
0

背景:我试图检测是否存在日志条目。如果是,则声明其字符串变量,否则继续。下面是脚本的一部分(按照这个顺序):通过函数设置New-Timespan对象

$Arch = (Get-WmiObject -Class Win32_OperatingSystem).OSArchitecture 

If ($Arch -eq "64-bit") { 
    $SMSAgentFolder = "$env:WinDir\SysWOW64\CCM"; 
    $CCMSetupFolder = "$Env:Windir\CCMSetup" 
} Else { 
    $SMSAgentFolder = "$Env:WinDir\System32\CCM"; 
    $CCMSetupFolder = "$Env:Windir\System32\CCMSetup" 
} 

New-Variable HinvStrWarning -Value "Warning! There is not a Hardware Inventory entry listed in the Inventory Agent Log file." 

Function SetHinv { 
    $Hinv = Select-String $Hinvpattern $Invfile | select -Last 1 | % { 
    $_.Matches.Groups[2].Value + ' ' + $_.Matches.Groups[1].Value 
    } 
} 

Function SetTSSinv { 
    $TSSInv = (New-TimeSpan $Sinv).Days 
} 

$InvFile = "$SMSAgentFolder\logs\inventoryagent.log" 

$Hinvpattern = 'HinvEndpoint.*<time="(.*?)" date="(.*?)"' 

$Hinv = Select-String $Hinvpattern $InvFile | select -Last 1 | % { 
    $_.Matches.Groups[2].Value + ' ' + $_.Matches.Groups[1].Value 
} 

If(![string]::IsNullOrWhiteSpace($Hinv)) { 
    SetHinv; SetTSHinv 
} else { 
    Add-Content $verboseLog "$HinvStrWarning" 
} 

功能Function SetTSSinv {$TSSInv = (New-TimeSpan $Sinv).Days}不通过调用函数名工作,但如果你做手工,这样的:$TSSInv = (New-TimeSpan $Sinv).Days,它确实没有问题。

这是据我在我的故障已经得到了,所以在这里的附加代码可能是不正确的:

用户互动,增加声明:

$7Days = $GetDate.AddDays(7) 
$SMSCli = [wmiclass] "\\$Env:ComputerName\root\ccm:SMS_Client" 

功能:

Function SinvScan {$SMSCli.TriggerSchedule("{00000000-0000-0000-0000-000000000002}")} 
Function SinvStr {If (((($GetDate).Days) - $TSSinv) -ge $7Days){Add-Content $VerboseLog $InvLogSinvScn; SinvScan}} 

变量删除可能会影响上面..谢谢你的帮助!

对不起,伙计们,我正在超越它。对于我的实例这个工程:

New-Variable SinvStrWarning -Value "Warning! There is not a Software Inventory entry listed in the Inventory Agent Log file." 

$InvFile = "$SMSAgentFolder\logs\inventoryagent.log" 

Function SetSinv { 
    $Sinv = Select-String $Sinvpattern $Invfile | select -Last 1 | % { 
    $_.Matches.Groups[2].Value + ' ' + $_.Matches.Groups[1].Value 
    } 
} 

$Sinvpattern = 'SinvEndpoint.*<time="(.*?)" date="(.*?)"' 

$Sinv = Select-String $Sinvpattern $Invfile | select -Last 1 | % { 
    $_.Matches.Groups[2].Value + ' ' + $_.Matches.Groups[1].Value 
} 

$SinvNull = $Sinv -ne $Null 

If ($SinvNull -eq "True"){ 
    SetSinv; 
    $TSSInv = (New-TimeSpan $Sinv).Days 
} Else { 
    Add-Content $verboseLog "$SinvStrWarning" 
} 

回答

0

变量去除可能会影响到以上..谢谢!

对不起,伙计们,我正在超越它。对于我的实例,这是有效的: 新变量SinvStrWarning -Value“警告!清单代理日志文件中没有列出软件清单条目。”

$InvFile = "$SMSAgentFolder\logs\inventoryagent.log" 

Function SetSinv { 
    $Sinv = Select-String $Sinvpattern $Invfile | select -Last 1 | % { 
    $_.Matches.Groups[2].Value + ' ' + $_.Matches.Groups[1].Value 
    } 
} 

$Sinvpattern = 'SinvEndpoint.*<time="(.*?)" date="(.*?)"' 

$Sinv = Select-String $Sinvpattern $Invfile | select -Last 1 | % { 
    $_.Matches.Groups[2].Value + ' ' + $_.Matches.Groups[1].Value 
} 

$SinvNull = $Sinv -ne $Null 

If ($SinvNull -eq "True"){ 
    SetSinv; 
    $TSSInv = (New-TimeSpan $Sinv).Days 
} Else { 
    Add-Content $verboseLog "$SinvStrWarning" 
} 
0
Function SetTSSinv { 
    $TSSInv = (New-TimeSpan $Sinv).Days 
} 

该函数创建一个时间跨度,并将其分配给函数局部变量$TSSInv,这是因为一旦你的代码从功能退出丢弃。如果您希望函数返回时间范围,请删除变量分配。您可能还需要做出$Sinv函数的参数:您的帮助

Function SetTSSinv($start) { 
    (New-TimeSpan $start).Days 
} 
+0

@ user2755028请不要在评论中发布代码。它变得不可读。而是更新你的问题。 –