【问题标题】:Simple powershell success or fail log简单的powershell成功或失败日志
【发布时间】:2021-10-12 04:44:10
【问题描述】:

我正在寻找将预定脚本的成功或失败写入 csv 文件的最简单方法。我不是在寻找一种方法来记录错误或失败的原因,只是一个简单的“成功/失败”。出于这个原因,我不会尝试修复/记录非终止错误。

我认为将每个脚本放在一个最简单的方法

    try {
Write-Host "test"
    }
    catch{
Code to write to csv here 
    }

阻止我在 Catch 部分中写入 csv 文件的位置。有没有更好/更简单的方法来做到这一点,或者这是正确的方法吗?

【问题讨论】:

  • edit分享您的问题minimal reproducible example
  • catch 仅在终止错误的情况下输入,需要通过-ErrorAction Stop 或设置$ErrorActionPreference = 'Stop'。您可能需要添加一个 finally 块来检查 $Error 变量以收集非终止错误。
  • @zett42 感谢您的输入,但基本上我想要记录的唯一内容是脚本是否在没有终止错误的情况下执行。
  • @JosefZ 完成。但不会真正增加很多,因为我的意思是笼统的,而不是特定的脚本。
  • 根据我的经验,如果不控制流量,就没有“简单”的方法可以解决这个问题。所以你不能仅仅以此为基础。

标签: powershell error-handling powershell-3.0


【解决方案1】:

继续我的评论。 . .

老实说,这真的取决于具体情况,即您想要完成什么。所以,让我们组成一个查询计算机以获取一些基本信息的场景:

Function Get-SystemInfo {
    Param (
        
        [Parameter(Mandatory=$false,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [string[]]$ComputerName = $env:COMPUTERNAME
    
    )
    Begin {

        $ErrorActionPreference = 'SilentlyContinue'

    }
    Process {

        foreach ($Computer in $ComputerName) 
        {
            try {

                # attempt cim session with -EA Stop to not allow it to go
                # any further and we can calculate the results.
                $CIMSession = New-CimSession -ComputerName $Computer -ErrorAction Stop

                $Status     = $true

                # Perform the actual queries
                $CS = Get-CimInstance -ClassName Win32_COmputerSystem -CimSession $CIMSession
                $BS = Get-CimInstance -ClassName Win32_BIOS -CimSession $CIMSession 

                # evaluate against the returned objects
                $UserName     = if ($CS.UserName -eq $null) { 'No User Logged in' } else { $CS.UserName }
                $Manufacturer = if ($CS.Manufacturer -eq $null) { 'N/a' } else { $CS.Manufacturer }
                $Model        = if ($CS.Model -eq $null) { 'N/a' } else { $CS.Model }
                $SerialNumber = if ($BS.SerialNumber -eq $null) { 'N/a' } else { $BS.SerialNumber }

            }
            catch {

                # Set the variables to $null
                $Status       = $false

                $UserName     = $null
                $Manufacturer = $null
                $Model        = $null
                $SerialNumber = $null

            }
            finally {

                # Output the filled variables
                [PSCustomObject] @{

                    ComputerName = $Computer
                    Connected    = $Status
                    UserLoggedIn = $UserName
                    Manufacturer = $Manufacturer
                    Model        = $Model
                    SerialNumber = $SerialNumber 

                }

            }

        }

    }

    End {
        
        # cleanup
        
        # some people argue this should be in the finally block
        # to disconnect any machine, but looking at this from both
        # sides, they both have pros/cons.    
        Get-CimSession | Remove-CimSession

    }

}

...这个快速功能最大的收获是-ErrorAction Stop 在尝试创建CIM 会话时。这就是放眼大局的地方。如果您无法连接到计算机,为什么还要继续?这包括从快速 ping 中获得回显回复,因为这并不意味着您可以仅仅因为收到回复就可以连接到远程 PC。

其余的是ifelse 语句,它们处理light 工作,对返回的对象进行评估以更好地控制输出。

结果是:

PS C:\Users\Abraham> Get-SystemInfo

ComputerName : OER
Connected    : True
UserLoggedIn : Abraham
Manufacturer : LENOVO
Model        : 22251
SerialNumber : 55555555




PS C:\Users\Abraham> Get-SystemInfo -ComputerName BogusComputerName

ComputerName : BogusComputerName
Connected    : False
UserLoggedIn : 
Manufacturer : 
Model        : 
SerialNumber : 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多