【问题标题】:Cannot create a folder on the partition that was just created within a DSC script无法在刚刚在 DSC 脚本中创建的分区上创建文件夹
【发布时间】:2018-01-23 21:17:40
【问题描述】:

我正在尝试使用 DSC 格式化驱动器并在新格式化的驱动器上创建特定的文件夹结构。

在我创建分区、格式化磁盘并给它一个驱动器号之后,我正在尝试使用新的磁盘路径创建一个文件夹,但是我在 New-Item 调用时收到了 Cannot find drive. A drive with the name 'K' does not exist 类型的错误.此代码是在Script DSC 中执行的示例:

$someDiskNumber = 3 # for the sake of testing

Initialize-Disk -Number $someDiskNumber -PartitionStyle GPT -PassThru
$partition = New-Partition -DiskNumber $someDiskNumber -UseMaximumSize -DriveLetter 'K'
Format-Volume -Partition $partition -FileSystem NTFS -AllocationUnitSize 65536 -NewFileSystemLabel "san-root" -Confirm:$False
New-Item -Path "K:\Test" -ItemType Directory -Force -ErrorAction Stop

如何让 DSC 脚本“重新发现”新卷以允许为它运行 New-Item

我尝试过的(没有成功):

  • Format-Volume 调用之后运行Update-Disk -DiskNumber $someDiskNumber
  • Format-Volume 调用之后运行Update-HostStorageCache
  • Format-Volume 调用之后添加一个while 循环以等待Test-Path "K:\" 返回True(它永远循环)

附:我知道xStorage DSC 模块,但不幸的是,由于限制(与问题无关),我无法使用它。

【问题讨论】:

  • xStorage 做你想做的但你不能使用它,你仍然可以查看它的源代码来确定它如何做你想做的。你试过吗?
  • @briantist 我不确定xStorage 是否符合我的要求。我遇到的问题不是创建挂载点(xStorage 所做的),它是最后一行(New-Item),它不属于xStorage

标签: powershell dsc


【解决方案1】:

在尝试 StackOverflow 五分钟后想出一个答案。添加以下命令将强制 PowerShell 刷新缓存并获取丢失的驱动器。

$null = Get-PSDrive

【讨论】:

  • 好发现!我以前没有遇到过这种情况,但我会记得的。
  • 我认为有一个特殊的 dsc 资源。总体而言,该错误非常迟钝。 xWaitForDrive 或类似的东西
猜你喜欢
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多