我有一个字符串中的路径,
"C:\temp\mybackup.zip"
我希望在该脚本中插入时间戳,例如,
"C:\temp\mybackup 2009-12-23.zip"
在PowerShell中有没有一种简单的方法可以做到这一点?
发布于 2009-12-24 02:03:42
您可以通过使用子表达式在双引号字符串中插入任意PowerShell脚本代码,例如$(),如下所示:
"C:\temp\mybackup $(get-date -f yyyy-MM-dd).zip"
如果你从其他地方获取路径--已经是一个字符串了:
$dirName = [io.path]::GetDirectoryName($path)
$filename = [io.path]::GetFileNameWithoutExtension($path)
$ext = [io.path]::GetExtension($path)
$newPath = "$dirName\$filename $(get-date -f yyyy-MM-dd)$ext"
如果路径恰好来自Get-ChildItem的输出
Get-ChildItem *.zip | Foreach {
"$($_.DirectoryName)\$($_.BaseName) $(get-date -f yyyy-MM-dd)$($_.extension)"}
发布于 2009-12-24 01:55:15
下面是一些应该可以工作的PowerShell代码。您可以将大部分代码合并到更少的代码行中,但我希望保持清晰和可读性。
[string]$filePath = "C:\tempFile.zip";
[string]$directory = [System.IO.Path]::GetDirectoryName($filePath);
[string]$strippedFileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath);
[string]$extension = [System.IO.Path]::GetExtension($filePath);
[string]$newFileName = $strippedFileName + [DateTime]::Now.ToString("yyyyMMdd-HHmmss") + $extension;
[string]$newFilePath = [System.IO.Path]::Combine($directory, $newFileName);
Move-Item -LiteralPath $filePath -Destination $newFilePath;
发布于 2013-09-12 03:45:03
我需要导出我们的安全日志,并想要协调世界时的日期和时间。事实证明,这是一个很难解决的挑战,但执行起来非常简单:
wevtutil export-log security c:\users\%username%\SECURITYEVENTLOG-%computername%-$(((get-date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ")).evtx
神奇的代码就是这一部分:
$(((get-date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ"))
https://stackoverflow.com/questions/1954203
复制相似问题