小编Ros*_*ons的帖子

Powershell - if $var -like $var*

我正在 Powershell 中使用 SCSM,但遇到了 if 语句的问题。

我有一个函数,它根据作为变量传递给函数的条件收集数据。例子:

$JMLs1 = collectTickets -crit $JMLCriteria1
Run Code Online (Sandbox Code Playgroud)

collectTickets是函数,$JMLCriteria1是通过的标准。

这是collectTickets函数:

function collectTickets
{
    param (
        [parameter (Mandatory=$true)]
        $crit
    )
    $fullDate = Get-Date
    if ($fullDate.DayOfWeek -eq 'Monday') 
    {
        $olderThan = $fullDate.AddDays(-4)
    }
    elseif ($fullDate.DayOfWeek -eq 'Tuesday') 
    {
        $olderThan = $fullDate.AddDays(-4)
    }
    else 
    {
        $olderThan = $fullDate.AddDays(-2)
    }
    if ($crit -like '$JML*') 
    {
        $data = Get-SCSMObject -Criteria $crit | select 'Id', 'Title', 'LastModified', 'Priority'
    }
    else 
    {
        $data = Get-SCSMObject -Criteria $crit | …
Run Code Online (Sandbox Code Playgroud)

powershell

4
推荐指数
1
解决办法
4226
查看次数

Powershell 开关 - 多个子句

我正在创建一个脚本,该脚本将根据条件更新 Excel 电子表格。

这就是我目前所拥有的:

if ($endIRs -ne $null) {
$endIRs | ForEach-Object {
    try {
        $classification = $_.Classification
        $priority = $_.Priority
        $title = $_.Title 
        $id = $_.Id

        switch ($classification) {
            {($_ -eq 'Reports') -and ($priority -eq '1')} {
            $GeAppsReportSheet.Cells.Item(8,2).Interior.ColorIndex = 3
            $GeAppsReportSheet.Cells.Item(8,2) = 'RED'
            }
            #more switch statements to go here
        }
     catch {#catch tickets with $classification not listed}
    }
}
Run Code Online (Sandbox Code Playgroud)

$endIRs开始保存了过去 12 小时内记录的一系列高优先级“事件”。如果没有,一切都将是默认设置的“绿色”。

我试图通过该语句实现的switch目标是if (($classification -eq 'Reports') -and ($priority -eq '1')) {'change the cell …

powershell switch-statement

1
推荐指数
1
解决办法
3851
查看次数

标签 统计

powershell ×2

switch-statement ×1