Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 创建文件是成功还是失败_Powershell_Logging_Append - Fatal编程技术网

Powershell 创建文件是成功还是失败

Powershell 创建文件是成功还是失败,powershell,logging,append,Powershell,Logging,Append,当我创建一个新文件时,我无法在代码中正确添加If(!){}else{}。我希望它在为true时附加到.log文件“log Created”,在为false时附加到“log failed”。我是不是走错路了 $foo = c:\path\to\folder $bar_bar = <variable> New-Item -path "$foo" -name "$bar_bar-test_archive.log" -type "file" -value "`n`r$(get-date -

当我创建一个新文件时,我无法在代码中正确添加
If(!){}else{}
。我希望它在为true时附加到.log文件“log Created”,在为false时附加到“log failed”。我是不是走错路了

$foo = c:\path\to\folder
$bar_bar = <variable>

New-Item -path "$foo" -name "$bar_bar-test_archive.log" -type "file" -value "`n`r$(get-date -f yyyy-MM-dd.HH:mm:ss) : Process started.`n"
$foo=c:\path\to\folder
$bar\u bar=
新项目-路径“$foo”-名称“$bar\u bar-test\u archive.log”-键入“file”-值“`n`r$(获取日期-f yyyy-MM-dd.HH:MM:ss):进程已启动。`n”
旁注:我必须将我的文件命名为
“$bar\u bar-test\u archive.log”
,但我更愿意将其命名为
“$bar\u-test\u archive.log”
,然而,由于变量名中的下划线以及变量和常量之间的下划线将其搞乱。有办法做到这一点吗

谢谢

旁注:我必须将我的文件命名为“$bar\u bar-test\u archive.log”,但我 不过,我更愿意将其命名为“$bar\u bar\u test\u archive.log”,因为 在变量名中添加下划线和下划线 变量和常量把它搞砸了。有办法做到这一点吗

您可以通过以下任一方式解决此问题:

-name "$bar_bar`_test_archive.log"

-name "$($bar_bar)_test_archive.log"
至于日志记录,如果它工作,与如果它不工作,正确的方法是使用try/catch构造,这是一种捕获错误的方法。比如说,

$LogFile = 'C:\PSTest\log.log'

try
{
    Some-Action
}
catch
{
    'Failed to run Some-Action' | Out-File $LogFile -Append
}
在捕获中,您通常也会添加一个中断或继续,如下所示:

$LogFile = 'C:\PSTest\log.log'

try
{
    Some-Action
}
catch
{
    'Failed to run Some-Action' | Out-File $LogFile -Append
    break # Exit Script
}

'Successfully ran Some-Action' | Out-File $LogFile -Append  # The script will only get here if there's no error as an error will trigger the break.
$foo = 'c:\PSTest'
$bar_bar = 'Test'
$LogFile = 'C:\PSTest\log.log'

try
{
    New-Item -path "$foo" -name "$bar_bar`_test_archive.log" -type "file" -value "`n`r$(get-date -f yyyy-MM-dd.HH:mm:ss) : Process started.`n" -ErrorAction Stop
}
catch
{
    "$(Get-Date -Format G) - Log Failed" | Out-File $LogFile -Append
    break 
}

"$(Get-Date -Format G) - Log Succeeded" | Out-File $LogFile -Append 
你最终会得到这样的结果:

$LogFile = 'C:\PSTest\log.log'

try
{
    Some-Action
}
catch
{
    'Failed to run Some-Action' | Out-File $LogFile -Append
    break # Exit Script
}

'Successfully ran Some-Action' | Out-File $LogFile -Append  # The script will only get here if there's no error as an error will trigger the break.
$foo = 'c:\PSTest'
$bar_bar = 'Test'
$LogFile = 'C:\PSTest\log.log'

try
{
    New-Item -path "$foo" -name "$bar_bar`_test_archive.log" -type "file" -value "`n`r$(get-date -f yyyy-MM-dd.HH:mm:ss) : Process started.`n" -ErrorAction Stop
}
catch
{
    "$(Get-Date -Format G) - Log Failed" | Out-File $LogFile -Append
    break 
}

"$(Get-Date -Format G) - Log Succeeded" | Out-File $LogFile -Append 
一些补充说明:

  • 使用比“Foo”和“Bar”更具描述性的变量名。这会使代码更具可读性,脚本越长,就越容易忘记变量名不好时包含的内容

  • 变量值周围缺少引号。因为字符串使用“”

  • 您会注意到我在newitem命令中添加了
    -ErrorAction Stop
    ;这样可以使锁扣正常工作


如果您不想像omniomi使用的那样使用
尝试
/
捕获
,这可能更合适,您可以使用
测试路径

$foo = c:\path\to\folder
$bar_bar = <variable>

New-Item -path "$foo" -name "$bar_bar-test_archive.log" -type "file" -value "`n`r$(get-date -f yyyy-MM-dd.HH:mm:ss) : Process started.`n"
If (!(Test-Path $foo)){} else{}
$foo=c:\path\to\folder
$bar\u bar=
新项目-路径“$foo”-名称“$bar\u bar-test\u archive.log”-键入“file”-值“`n`r$(获取日期-f yyyy-MM-dd.HH:MM:ss):进程已启动。`n”
If(!(测试路径$foo)){}else{}
对旁注的答复:
您可以使用
$()
在引号内进行如下计算:
“$($bar)\u bar\u test\u archive.log”

问题:“-Format G”是一种与先前定义的格式相对应的反向引用函数,还是您只是在快捷方式上进行了修改?@physlexic shotching。G是2007年8月30日上午11:20:24,这与你在OP中看到的不完全一样,但很接近。