Powershell 用于加快脚本创建的Powrshell模板

Powershell 用于加快脚本创建的Powrshell模板,powershell,templates,Powershell,Templates,已经编写了越来越多的脚本,特别是用于进行API调用、获取信息以及在满足某些条件时发出警报的脚本。警报将发送到microsoft团队频道。这些脚本将作为计划任务运行,而不作为计划任务运行。我只使用powershell v5+ 我试图制作一个简单的模板,从我在网上找到的内容开始,并做了一些修改。我需要错误检查和报告,我觉得可以通过Try/Catch/Finally来完成。我还需要一些基本的日志输出来检查错误 最后一个块中的日志记录是否足以知道脚本完成的日期和时间?将使用$error.exe异常是否捕

已经编写了越来越多的脚本,特别是用于进行API调用、获取信息以及在满足某些条件时发出警报的脚本。警报将发送到microsoft团队频道。这些脚本将作为计划任务运行,而不作为计划任务运行。我只使用powershell v5+

我试图制作一个简单的模板,从我在网上找到的内容开始,并做了一些修改。我需要错误检查和报告,我觉得可以通过Try/Catch/Finally来完成。我还需要一些基本的日志输出来检查错误

最后一个块中的日志记录是否足以知道脚本完成的日期和时间?将使用$error.exe异常是否捕获错误消息?我似乎很幸运地使用了它,但有时我没有。在确保脚本在无人值守的情况下运行以及在出现问题时报告错误方面,我遗漏了哪些关键内容。我还将在Catch部分包含一段代码,以将错误发送到MS teams通道

<#
.SYNOPSIS
    <Overview of script>

.SYNTAX
    <Cmdlet-Name> -Parameter <value>

.DESCRIPTION
    <Brief description of script>

.PARAMETER <Parameter_Name>
    <Brief description of parameter input required. Repeat this attribute if required>

.INPUTS
    <Inputs if any, otherwise state None>

.OUTPUTS
    <Outputs if any, otherwise state None - example: Log file stored in C:\Windows\Temp\<name>.log>

.EXAMPLE
    <Example goes here. Repeat this attribute for more than one example>

.REMARKS
    Version:        1.0
    Author:         <Name>
    Creation Date:  <Date>
#>

#---------------------------------------------------------[Variables]------------------------------------------------------------

$var1 = <stuff>
$var2 = <stuff>

#---------------------------------------------------------[Import Modules]--------------------------------------------------------

Import-Module <name>

#-----------------------------------------------------------[Functions]------------------------------------------------------------

Function <FunctionName> {
    [CmdletBindinging()]
    param(
        [Parameter()]
        [string]$MyOptionalParameter,

        [Parameter(Mandatory)]
        [int]$MyMandatoryParameter
    )

    Try{
        <code goes here>
    }

    Catch {
        Write-Host $Error.Exception
        Out-File $Error.Exception
        Break
    }

    Finally {
        $time = Get-Date
        "Script completed at $time" | Out-File $env:TEMP\MyScriptName.log
    }
}

#-----------------------------------------------------------[Execution]------------------------------------------------------------

<FunctionName>


#---------------------------------------------------------[变量]------------------------------------------------------------
$var1=
$var2=
#---------------------------------------------------------[导入模块]--------------------------------------------------------
导入模块
#-----------------------------------------------------------[职能]------------------------------------------------------------
作用{
[CmdletBinding()]
param(
[参数()]
[字符串]$MyOptionalParameter,
[参数(强制性)]
[int]$MyMandatoryParameter
)
试一试{

}
抓住{
写入主机$Error.Exception
输出文件$Error.Exception
打破
}
最后{
$time=获取日期
“脚本在$time完成”|输出文件$env:TEMP\MyScriptName.log
}
}
#-----------------------------------------------------------[执行]------------------------------------------------------------

在catch块中,如果您只对错误感兴趣,则需要使用
$\uux0.Exception.Messge
。。否则,您也可以执行
$\异常
来获取详细信息。无论try/catch中发生了什么,登录Finally块都将始终执行,并且只记录退出时间。您可能需要添加更多关于呼叫何时开始的日志记录。不太清楚你还想找什么不想找太多其他的。只需要一个简单的shell,我的所有脚本都可以从它开始。我需要记录它的启动和完成、脚本失败时的任何错误,以及脚本执行失败时的警报。