1

我想用来new-item创建一个新文件,但我有一个未授权访问异常,似乎我无法访问我分配的路径

这是我的代码

$Random_Serial = Invoke-Expression .\Generate-Random-Number.ps1
Write-Host $Random_Serial
New-Item -Path ..\Database -Name "$Random_Serial" -ItemType file

我尝试使用文本名称替换变量名称,它可以成功创建文件

$Random_Serial = Invoke-Expression .\Generate-Random-Number.ps1
Write-Host $Random_Serial
New-Item -Path ..\Database -Name "11" -ItemType file

但我还是想不通是什么问题?

4

1 回答 1

2

确保您对路径具有安全权限。输出文件需要扩展名,Write-Host 通常用于控制台输出。您可以使用 Write-Output 作为替代方案。此外,对于您要完成的工作,Out-File 是比 New-Item 更好的选择。

改变

Write-Host $Random_Serial
New-Item -Path ..\Database -Name "11" -ItemType file

至:

write-output $Random_Serial | Out-File -Filepath C:\11.txt
于 2013-07-03T18:25:15.867 回答