简体   繁体   English

在 PowerShell 中使用 Move-Item 将主机名和日期附加到文件名

[英]Use Move-Item in PowerShell to append Hostname and date to file name

Complete PowerShell newbie so please forgive me as I might mess up what I am asking.完整的 PowerShell 新手,所以请原谅我,因为我可能会搞砸我的要求。

I have a file called debug.log , I need to rename it and move it.我有一个名为debug.log的文件,我需要重命名并移动它。

I know I can do that with Move-Item, but I would like to append the Hostname to the front of the file name and the date stamp to the end.我知道我可以使用 Move-Item 做到这一点,但我想将主机名附加到文件名的前面,并将日期戳附加到末尾。

For example: In: C:\\Temp\\debug.log Out: \\\\\\sharename\\MyHostdebug20201006.log例如:输入: C:\\Temp\\debug.log输出: \\\\\\sharename\\MyHostdebug20201006.log

The files will go through a log parse after they get copied to the share, and the HostName and Date stamp are import to my tracking of which logs get processed.这些文件在被复制到共享后将进行日志解析,并且主机名和日期戳被导入到我对哪些日志得到处理的跟踪中。 I have to do this for 8 separate servers on a monthly basis.我必须每月为 8 个独立的服务器执行此操作。 I plan on setting this up as a Windows Schedule Task when it is completed.我计划在完成后将其设置为 Windows 计划任务。

Using Get-Date and, as mentioned by @Scepticalist above, $env:ComputerName you can create a string variable which you can pass into your Move-Item as the destination:使用Get-Date和上面@Scepticalist 提到的 $env:ComputerName,您可以创建一个字符串变量,您可以将其作为目的地传递到您的Move-Item中:

$timestamp = get-date -Format yyyyMMdd
$newPath = "\\sharename\" + $env:computername + "debug" + $timestamp + ".log"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM