![使用 powershell 创建日志文件](https://linux22.com/image/1367512/%E4%BD%BF%E7%94%A8%20powershell%20%E5%88%9B%E5%BB%BA%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6.png)
如何使用 powershell 创建日志文件?
我正在使用此代码来创建日志文件。
# Log file time stamp:
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
# Log file name:
$LogFile = 'C:\PSLogs\'+"LOG_"+$LogTime+".log"
if(---)
{
----
----
"done" | Out-File $LogFile -Append -Force
}
else
{
"Unlucky!!" | Out-File $LogFile -Append -Force
}
但它向我显示了一个错误
Out-File:无法找到路径的一部分
答案1
# Log file time stamp:
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
[xml] $var = Get-Content D:\ScriptMgr\Sample.xml
$ser = read-host "Enter the centre:"
# Log file name:
$path = "C:\Logs\"
if([IO.Directory]::Exists($path))
{
#Do Nothing!!
}
else
{
New-Item -ItemType directory -Path C:\PSLogs
}
$LogFile = 'C:\PSLogs\'+$ser+$LogTime+".html"
if (($sea=$var.detail.centre | where {$_.name -eq $ser}) -ne $null)
{
Write-Host "center:" $sea.name
Write-Host "Servername:" $sea.servername
Write-Host "ip:" $sea.ip
Write-Host "Username:" $sea.uname
Write-Host "Password:" $sea.pwd
Write-Host "Database:" $sea.dbname
"$LogTime Found" | Out-File $LogFile -Append -Force
}
else
{
write-host "not listed!!!"
"$LogTime Not Found" | Out-File $LogFile -Append -Force
}