我正在使用Powershell。我的目标是将一项复制到另一个目录(我已经知道该怎么做),获取userID(我已经知道该怎么做),并使用存在于变量中的文件名重命名该文件(不知道我在做什么错)。
当我运行代码时,它给我一个错误:Rename-Item:由于参数null而无法将参数绑定到参数'NewName'。我不知道我在做什么错。
我的代码在下面列出:
$name = [Environment]::UserName
$mainFile = "\\example\example.txt"
$copiedFile = "\\example\example\copiedFolder\example.xlsx"
$name = [Environment]::UserName
#Once I have copied the file to the new directory, I do the following code which does not work:
$copiedFile = Rename-Item -Path $copiedFile -NewName $name.xlsx
您已经解决了这个问题。这是一个简单的复制命令。
Get-ChildItem -Path 'D:\temp\reconcile.*'
<#
# Results
Directory: D:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 03-Feb-20 12:54 644 reconcile.txt
#>
$mainFile = 'D:\temp\reconcile.txt'
$FileTarget = 'D:\temp\'
Copy-Item -Path $mainFile -Destination "$FileTarget\reconcile.xlsx" -Force
Get-ChildItem -Path 'D:\temp\reconcile.*'
<#
# Results
Directory: D:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 03-Feb-20 12:54 644 reconcile.txt
-a---- 03-Feb-20 12:54 644 reconcile.xlsx
#>