我想编写一个脚本来创建一个不存在的目录并在该文件夹中创建一个文件。该脚本还应该能够为该文件分配一个值。以下是我的代码,但它没有将值分配给文件。在本例中,我尝试将数组插入到文件中。
$myarray=@()
for($x=0;$x -lt 20; $x++) {
$myarray += $x
}
New-Item -Path C:\temp -Force -Name myarray.log -ItemType File -Value $myarray
当我打开文件 myarray.log 时,我看到的只是“System.Object[]”,而不是数组。这里需要一些帮助。
最佳答案
它写的是“System.Object[]”,因为这就是 $myarray.ToString() 的输出。 一种解决方案是:
New-Item -Path C:\temp -Force -Name myarray.log -ItemType File -Value $($myarray -join ' ')
这会将数组的所有元素写入文件中,并用空格分隔。
更多解决方案在这里:How do I convert an array object to a string in PowerShell?
关于powershell - 在 Powershell 中创建文件夹和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22720667/