PowerShell“=”不是可识别的 cmdlet 错误
PowerShell "=" not a recognised cmdlet error
除了此处给出的响应: ,我在脚本中遇到了另一堵我无法解决的问题。下面的脚本 returns 错误:
The term '=' 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.
$sb = [ScriptBlock]::Create(@"
$Acl = (Get-Item -path D:\Websites$Sitename).GetAccessControl('Access')
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
"@)
Invoke-Command -Session $Session -ScriptBlock $sb
我可以创建脚本块变量 ($sb),但是当我调用它时,出现错误。我已将其缩小到 $Acl
变量的设置,并尝试重写各种方式但没有成功。我错过了什么?
当您在此处使用双引号字符串时,它的行为就像一个普通的双引号字符串 - 解析器将评估和扩展引号之间的任何变量或子表达式。
由于脚本块定义中的变量在定义上下文中尚不存在,您最终会得到一个具有以下定义的脚本块:
= (Get-Item -path D:\Websites\).GetAccessControl('Access')
= New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
.SetAccessRule()
Set-Acl -path -AclObject
如您所见,前两个语句仅以 =
作为第一个非空白字符开始,这就是您所看到的错误的原因。
改用单引号字符串:
$sb = [ScriptBlock]::Create(@'
$Acl = (Get-Item -path D:\Websites$Sitename).GetAccessControl('Access')
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
'@)
如果您需要从定义范围传入变量值,我建议在脚本块中定义一个 param
块:
$sb = [ScriptBlock]::Create(@'
param($Sitename)
$Acl = (Get-Item -path D:\Websites$Sitename).GetAccessControl('Access')
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
'@)
Invoke-Command -Session $Session -ScriptBlock $sb -ArgumentList $Sitename
或者在创建脚本块之前使用-f
字符串格式运算符在字符串中替换它:
$sb = [ScriptBlock]::Create(@'
$Acl = (Get-Item -path D:\Websites\{0}).GetAccessControl('Access')
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
'@ -f $Sitename)
有关引用和变量扩展的更多信息,请参阅 about_Quoting_Rules
help topic
除了此处给出的响应:
The term '=' 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.
$sb = [ScriptBlock]::Create(@"
$Acl = (Get-Item -path D:\Websites$Sitename).GetAccessControl('Access')
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
"@)
Invoke-Command -Session $Session -ScriptBlock $sb
我可以创建脚本块变量 ($sb),但是当我调用它时,出现错误。我已将其缩小到 $Acl
变量的设置,并尝试重写各种方式但没有成功。我错过了什么?
当您在此处使用双引号字符串时,它的行为就像一个普通的双引号字符串 - 解析器将评估和扩展引号之间的任何变量或子表达式。
由于脚本块定义中的变量在定义上下文中尚不存在,您最终会得到一个具有以下定义的脚本块:
= (Get-Item -path D:\Websites\).GetAccessControl('Access')
= New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
.SetAccessRule()
Set-Acl -path -AclObject
如您所见,前两个语句仅以 =
作为第一个非空白字符开始,这就是您所看到的错误的原因。
改用单引号字符串:
$sb = [ScriptBlock]::Create(@'
$Acl = (Get-Item -path D:\Websites$Sitename).GetAccessControl('Access')
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
'@)
如果您需要从定义范围传入变量值,我建议在脚本块中定义一个 param
块:
$sb = [ScriptBlock]::Create(@'
param($Sitename)
$Acl = (Get-Item -path D:\Websites$Sitename).GetAccessControl('Access')
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
'@)
Invoke-Command -Session $Session -ScriptBlock $sb -ArgumentList $Sitename
或者在创建脚本块之前使用-f
字符串格式运算符在字符串中替换它:
$sb = [ScriptBlock]::Create(@'
$Acl = (Get-Item -path D:\Websites\{0}).GetAccessControl('Access')
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule('BUILTIN\IIS_IUSRS', 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
'@ -f $Sitename)
有关引用和变量扩展的更多信息,请参阅 about_Quoting_Rules
help topic