Powershell 通过函数设置新的Timespan对象

Powershell 通过函数设置新的Timespan对象,powershell,timespan,Powershell,Timespan,背景:我正在尝试检测日志条目是否存在。如果是,则声明其字符串变量,否则继续。以下是脚本的一部分(按此顺序): 职能: Function SinvScan {$SMSCli.TriggerSchedule("{00000000-0000-0000-0000-000000000002}")} Function SinvStr {If (((($GetDate).Days) - $TSSinv) -ge $7Days){Add-Content $VerboseLog $InvLogSinvScn;

背景:我正在尝试检测日志条目是否存在。如果是,则声明其字符串变量,否则继续。以下是脚本的一部分(按此顺序):

职能:

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"
}
新变量SinvStrWarning-Value“警告!库存代理日志文件中未列出软件库存条目。”
$InvFile=“$SMSAgentFolder\logs\inventoryagent.log”
函数SetSinv{
$Sinv=选择字符串$Sinvpattern$Invfile |选择-最后1 |%{
$\.Matches.Groups[2]。值+''+$\.Matches.Groups[1]。值
}
}
$Sinvpattern='SinvEndpoint*
函数创建一个时间跨度,并将其分配给函数局部变量
$TSSInv
,一旦代码退出函数,该变量就会被丢弃。如果希望函数返回timespan:删除变量赋值。您可能还希望将
$Sinv
作为函数的参数:

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

删除变量可能会影响上述内容。。谢谢你的帮助

对不起,伙计们,我想得太多了。就我而言,这是可行的: 新变量SinvStrWarning-Value“警告!库存代理日志文件中未列出软件库存条目。”

$InvFile=“$SMSAgentFolder\logs\inventoryagent.log”
函数SetSinv{
$Sinv=选择字符串$Sinvpattern$Invfile |选择-最后1 |%{
$\.Matches.Groups[2]。值+''+$\.Matches.Groups[1]。值
}
}

$Sinvpattern='SinvEndpoint.@user2755028请不要在评论中发布代码。它变得不可读了。而是更新你的问题。
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"
}
Function SetTSSinv {
  $TSSInv = (New-TimeSpan $Sinv).Days
}
Function SetTSSinv($start) {
  (New-TimeSpan $start).Days
}
$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"
}