简体   繁体   English

如何验证Powershell脚本中的密钥?

[英]How can I verify my keys in my powershell script?

I need to verify my parameters(keys) when I run it. 我需要在运行它时验证我的参数(键)。 For example, when I set wrong parameters looks like (myscript.ps1 -p1 blabla -p2 blabla) I have error in console(wrong type). 例如,当我设置错误的参数看起来像(myscript.ps1 -p1 blabla -p2 blabla)时,控制台出现错误(类型错误)。 How I can throw this error? 我如何抛出此错误? Also, I need to write logs in different levels(Debug,Error,Warning). 另外,我需要写不同级别的日志(Debug,Error,Warning)。 I know only one cmdlet Start-Transcript, but it write all actions. 我只知道一个cmdlet起始脚本,但它记录了所有操作。

        param
        (
             [datetime]$sleep,
             [datetime]$wake_up
        )   
        #Starting log process
Start-Transcript .\logger.txt -Append
function do_sleep () 
{
    if (!$sleep)
    {
        [datetime]$sleep = Read-Host "Input time when you go to sleep"
    }
    if (!$wake_up)
    {
        [datetime]$wake_up = Read-Host "Input time when you wake up"
    }
    if ($wake_up.Hour -le 8 ) {
        Write-Host "You are lark"
    }
    if ($wake_up.Hour -gt 8) {
        Write-Host "You are owl"
    }
    if ($wake_up -lt $sleep) {
        $sleeping_time = ($wake_up.AddDays(1) - $sleep)
        $normal_sleep = $sleeping_time.hours;
    }
    else {
        $sleeping_time = $wake_up - $sleep;
        $normal_sleep = $sleeping_time.hours;
    }   
    if ($normal_sleep -ge 8 ) {
        Write-Host "You slept more"  $sleeping_time.Hours  "hours. You are lucky man. " 
    }
}
do
{
    try
    {
        do_sleep
        exit
    }
    catch 
    {
        Write-Host ("Wrong input. Please input data again.")
        $g = 1;
    }
}
while ($g -eq 1)
Stop-Transcript

try Something like this: 尝试这样的事情:

$ScriptName = "Script1"

try
{

    #create log
    if (![System.Diagnostics.EventLog]::SourceExists($ScriptName))
    {
        New-EventLog -LogName Application -Source $ScriptName
    }


    #Log information
    Write-EventLog –LogName Application –Source $ScriptName –EntryType Information –EventID 1 –Message "Starting..."

    $Test="test1"


    if ($Test -eq "test1") 
    {
    #throw exception 1
    throw "$Test is bad"
    }

    if ($Test -eq "test2") 
    {
    #throw exception 2
    throw "$Test is really bad"
    }

    if ($Test -eq "test3") 
    {
    #Log warning
    Write-EventLog –LogName Application –Source $ScriptName –EntryType Warning –EventID 1 –Message "Starting..."
    }

}
catch 
{
        #log all exceptions 
        $result="Message : {0}, Type : {1}, Exception : {2}, StackTrace : {3}" -f $_, $_.GetType(), $_.Exception, $_.Exception.StackTrace

        Write-EventLog –LogName Application –Source $ScriptName –EntryType Error –EventID 1 –Message $result

        #rethrow if you want print errors to output standard error
        throw
}
finally
{
    #Log information
    Write-EventLog –LogName Application –Source $ScriptName –EntryType Information –EventID 1 –Message "Ending..."
}

To be able to use different logging levels decorate param with the following attribute: 为了能够使用不同的日志记录级别,请使用以下属性修饰参数:

[CmdletBinding()]Param

To validate parameters you can use validation attributes, eg 要验证参数,您可以使用验证属性,例如

[ValidateNotNullOrEmpty()] $MyParam

For more information search for 'advanced function PowerShell' or have a look at the snippets that you get when you press Ctrl+J in the PowerShell ISE 有关更多信息,请搜索“高级功能PowerShell”或查看在PowerShell ISE中按Ctrl + J时获得的代码段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复我的Powershell脚本,该脚本读取Assembly属性但留下打开的文件句柄? - How can I fix my Powershell script which reads Assembly attributes but leaves an open file handle? 当我的构造函数接收大量字节时,如何在 PowerShell 脚本中使用 New-Object cmdlet? - How can I use the New-Object cmdlet in a PowerShell script when my constructor takes in a large array of bytes? 如何调用脚本 Powershell 到我的后端 .net 核心? - how to invoke script Powershell to my backend .net core? 我可以在PowerShell 2.0中创建自己的属性类吗? - Can I create my own attribute classes in PowerShell 2.0? 我可以从PowerShell访问My Custom .NET Class吗? - Can I access My Custom .NET Class from PowerShell? 在我的PowerShell FTP脚本中转义@字符 - Escape the @ character in my PowerShell FTP script 我的 PowerShell 脚本使用哪个 .NET 版本? - Which .NET version is my PowerShell script using? 如何在PowerShell中删除点源脚本? - How can I remove a dot sourced script in PowerShell? 有人可以澄清我对模拟验证概念的理解吗? - Can someone please clarify my understanding of a mock's Verify concept? 无法验证...的代码签名...无法在我的iPhone上安装应用程序 - Failed to verify code signature of… Can't install application on my iPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM