【问题标题】:PowerShell DSC - how to delete child directories in a pathPowerShell DSC - 如何删除路径中的子目录
【发布时间】:2018-06-16 13:55:04
【问题描述】:

我已经在 PowerShell DSC 中开始了小步骤。

到目前为止,我已经创建了几个 DSC 脚本并且都按预期工作。

我尝试删除特定路径中的子目录。但是文件资源正在删除所有子文件夹和父文件夹。

我希望根文件夹处于活动状态并删除子目录。

请为此任务提供建议。

这是我的代码

    #requires -version 4.0
#requires -RunasAdministrator

param(

   [parameter(Position=0,Mandatory)]
   [string[]]$Computers
)

Configuration RemoveWorkspace{

param(

[parameter(Position=0,Mandatory)]
[string[]]$Computers
)
Node $computers {
File RemoveDiretory{
   Type = "Directory"
   DestinationPath = "E:\Builds\1946"
   Force = $true
   Ensure = "Absent"
   Recurse = $true   

}# end File
}# end node
}#end configuration

#create config data to allow plain text passwords

$configData=@{AllNodes=$null}
#initialize an array for node information
$nodes=@()

ForEach($computer in $Computers){

  write-host " Adding $computer "-ForegroundColor Green
  #define a hashtable for each computername and add to the nodes array

  $nodes+=@{

       NodeName="$computer"
       PSDscAllowPlainTextPassword=$true

  }

}#end foreach

#add the nodes to AllNodes
$configData.AllNodes=$nodes

#you will be prompted to enter a credential

Write-Host "Enter Admin credentials" -ForegroundColor Green

RemoveWorkspace $Computers -configurationdata $configData -outputpath C:\Temp\DeleteDirs\$computer

Start-DscConfiguration -Path C:\Temp\DeleteDirs\$computer -Wait -Verbose -Force

经过一番调查,我已经解决了我的问题。我将在稍后发布最新代码

【问题讨论】:

  • 您是说您不想/不能使用 DSC 脚本资源来执行发布的操作?
  • 我无法使用 DSC 文件资源执行操作。
  • 不基于文档显示的与 DSC 文件资源功能相关的内容。然而,这就是 DSC 自定义资源和脚本资源退出的原因。去做那些不是 OOBE 的事情。然而,从技术上讲,我明白你的意思。如果我看到来自供应商的文件提供程序,我们会自动假设它的文件夹很好,但情况并非总是如此。需要注意的是,如果您开始尝试使用通配符/多文件类型过滤器,那么至少现在您会遇到相同的建议。

标签: powershell dsc


【解决方案1】:

很难说你的问题是什么,如果你不发布你的代码块供审查和评论......但我是否接受你所说的表面价值,快速和肮脏,它应该像这样简单。

Clear-Host 

remove all subfolders of a given folder 

$RootFolder = 'D:\TestRoot' 
"Showing $RootFolder directories"
Get-ChildItem -Path $RootFolder -Directory


Remove these sub dirs only

(Get-ChildItem -Path $RootFolder -Directory) | 
ForEach { Write-Warning -Message "Removing $($_.Name) from $RootFolder" 
Remove-Item $_.FullName #-WhatIf
} 

"Showing $RootFolder directories after remove"
Get-ChildItem -Path $RootFolder -Directory


Results

Showing D:\TestRoot directories


    Directory: D:\TestRoot


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----      Sun 07 Jan 02018                     TestRootSubDir01
                            02:15                                                                                                            
d-----      Sun 07 Jan 02018                     TestRootSubDir02
                            02:15                                                                                                            
d-----      Sun 07 Jan 02018                     TestRootSubDir03
                            02:15                                                                                                            
WARNING: Removing TestRootSubDir01 from D:\TestRoot
WARNING: Removing TestRootSubDir02 from D:\TestRoot
WARNING: Removing TestRootSubDir03 from D:\TestRoot
Showing D:\TestRoot directories after remove

更新

根据我的评论,关于您的更新/评论,请采取上述措施并利用以下措施。

DSC 脚本资源文档 Windows PowerShell Desired State Configuration (DSC) 中的脚本资源提供了一种在目标节点上运行 Windows PowerShell 脚本块的机制。 https://docs.microsoft.com/en-us/powershell/dsc/scriptresource

【讨论】:

  • 感谢您的帖子。但我需要通过 DSC 提供建议,当我们从 powershell 运行时,上述脚本将完美运行。
  • 我已经发布了代码 .. 现在查看并查看 .. 如果可能,请提供您的想法。
  • 根据您的更新,我仍然建议您使用脚本资源或自定义资源来完成您的任务。
  • 非常感谢@Postanote,我会按照你的建议尝试。
  • 不用担心,但如果它满足您的要求,请务必将其标记为已回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
相关资源
最近更新 更多