问题描述
Ansible:2.9 PowerShell:5 操作系统:w2k16 服务器
大家好!
我在 Windows 目标主机中有一个使用“只读(仅适用于文件夹中的文件)”创建的目录。我需要删除此属性。
我在 Powershell 中编写此代码:
- name: "Change rules dir for all"
win_shell: |
$mypath= c:\tmp
$myacl= Get-ACL "$mypath"
$myaclentry= "everyone","FullControl","Allow"
$ruleexecutor= New-Object System.Security.AccessControl.FileSystemAccessRule($myaclentry)
$myacl.SetAccesRule($ruleexecutor)
$myACL | Set-Acl $mypath
Get-ACL "$mypath" | fl
但我有这个结果:
错误:
"stderr": "c:\\tmp : The term 'c:\\tmp' is not recognized as the name of a
cmdlet,function,script file,or operable program.
Check the spelling of the name,or if a path was included,verify that the path is correct and try again.
At line:1 char:73
+ ... ::InputEncoding = New-Object Text.UTF8Encoding $false; $mypath=c:\\tmp
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (c:\\tmp:String) [],CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Get-Acl : Cannot validate argument on parameter 'Path'.
The argument is null or empty. Provide an argument that is not
null or empty,and then try the command again.
At line:2 char:17
+ $myacl= Get-ACL \"$mypath\"
+
...
问题:
再次感谢!
解决方法
我有很多回复:
-
为什么我的代码不运行? 出于多种原因:
- 双“\”语法。
- 因为在我的系统中找不到“SetAccesRule”,我需要从 AD 服务器修改 GPO。
- 获取值变量时 win_shell ansible 模块中的显式语法。
- 路径中 ' 或 " 之间的差异(是的,下次我将创建有关此主题的问题)。
-
为什么无法识别“\”?
- 我需要在“c:\tmp”值变量中添加另一个“\”。 阅读:Using Ansible and Windows
-
如何使用 Powershell 更改目录中的简单属性?
- 我不知道,我忽略了“只读”,因为我可以在目录中写入。
更多信息请访问:how-to-remove-read-only-attribute-of-a-folder-by-powershell
但是当我执行此代码
$folder.Attributes -= 'ReadOnly'
我的文件更改为隐藏文件。这是我忽略这个“只读”的另一个原因。
- 我不知道,我忽略了“只读”,因为我可以在目录中写入。
更多信息请访问:how-to-remove-read-only-attribute-of-a-folder-by-powershell
但是当我执行此代码
最后,我的解决方案包含在 Powershell 命令中:
task: Create dir
win_shell: |
New-Item -Path c:\temp -ItemType directory # Of corse here without double '\\'