微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何管理来自 Ansible 的 Powershell 中 var 值中的目录

如何解决如何管理来自 Ansible 的 Powershell 中 var 值中的目录

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\"
+ 
...

问题:

  • 为什么我的代码无法运行?
  • 为什么无法识别“\”?
  • 如何使用 Powershell 更改目录中的简单属性

再次感谢!

解决方法

我有很多回复:

  • 为什么我的代码不运行? 出于多种原因:

    • 双“\”语法。
    • 因为在我的系统中找不到“SetAccesRule”,我需要从 AD 服务器修改 GPO。
    • 获取值变量时 win_shell ansible 模块中的显式语法。
    • 路径中 ' 或 " 之间的差异(是的,下次我将创建有关此主题的问题)。
  • 为什么无法识别“\”?

  • 如何使用 Powershell 更改目录中的简单属性?

    • 我不知道,我忽略了“只读”,因为我可以在目录中写入。 更多信息请访问:how-to-remove-read-only-attribute-of-a-folder-by-powershell 但是当我执行此代码 $folder.Attributes -= 'ReadOnly' 我的文件更改为隐藏文件。这是我忽略这个“只读”的另一个原因。

最后,我的解决方案包含在 Powershell 命令中:

task: Create dir
win_shell: |
  New-Item -Path c:\temp -ItemType directory # Of corse here without double '\\'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。