通过Function设置New-Timespan对象

时间:2013-10-11 15:21:38

标签: powershell 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"
}

2 个答案:

答案 0 :(得分:0)

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

该函数创建一个时间跨度并将其分配给函数局部变量$TSSInv,一旦代码退出函数,该变量就会被丢弃。如果希望函数返回时间跨度:删除变量赋值。您可能还想使$Sinv成为函数的参数:

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

答案 1 :(得分: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"
}