使用PowerShell Ping服务器
我想ping我们的一个服务器,并继续旋转文件。为此,我创建了以下脚本使用PowerShell Ping服务器,powershell,ping,Powershell,Ping,我想ping我们的一个服务器,并继续旋转文件。为此,我创建了以下脚本 function main() { ping 192.168.1.1 > E:\Ping\TestCon.txt RotateLog } function RotateLog { $log = "E:\Ping\*.log_old" $target = Get-ChildItem $log -Filter "*.log_old" | sort LastWriteTime | select -last 1
function main()
{
ping 192.168.1.1 > E:\Ping\TestCon.txt
RotateLog
}
function RotateLog {
$log = "E:\Ping\*.log_old"
$target = Get-ChildItem $log -Filter "*.log_old" | sort LastWriteTime | select -last 1
# $threshold = 3000
$datetime = Get-Date -uformat "%Y-%m-%d"
$filename = "$("TestCon")_${datetime}.log_old"
if ( $filename -eq $targeta.name )
{
main
}
else
{
$newname = "$("TestCon")_${datetime}.log_old"
Rename-Item -Path "E:\Ping\TestCon.txt" $newname
main
}
}
main
问题是,当它返回到main时,它会创建新文件,但我想附加它,只要它的日期相同。请建议或者按照Mathias R.Jessen在评论中的建议去做,然后直接写信给该文件
$datetime = Get-Date -uformat "%Y-%m-%d"
Test-NetConnection -ComputerName 192.168.1.1 | Out-File -Path "TestCon_${datetime}.log_old"
或者,您可以测试文件是否存在并附加内容
if ([bool](Test-Path -Path "TestCon_${datetime}.log_old"))
{
Test-NetConnection -ComputerName 192.168.1.1 | Out-File -Path "TestCon_${datetime}.log_old" -Append
}
为什么不直接写一个名为TestCon{datetime}.log\u old的文件呢?谢谢Mathias R.Jessen和Alex\u P。。。它起作用了。。实际上是在搜索Append参数和-Append help。。非常感谢各位