如何从 Ansible 的 Powershell 中管理 var 值中的目录
How manage dirs in var value off Powershell from Ansible
Ansible:2.9 PowerShell:5 OS: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 更改目录中的简单属性?
再次感谢!
我有很多回复:
为什么我的代码没有 运行?
出于多种原因:
- 双“\”语法。
- 因为在我的系统中找不到“SetAccessRule”,所以我需要从 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'
我的文件更改为隐藏文件。那是我忽略这个“ReadOnly”的另一个原因。
最后,我的解决方案在 Powershell 命令中:
task: Create dir
win_shell: |
New-Item -Path c:\temp -ItemType directory # Of corse here without double '\'
Ansible:2.9 PowerShell:5 OS: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 更改目录中的简单属性?
再次感谢!
我有很多回复:
为什么我的代码没有 运行? 出于多种原因:
- 双“\”语法。
- 因为在我的系统中找不到“SetAccessRule”,所以我需要从 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'
我的文件更改为隐藏文件。那是我忽略这个“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 '\'