这让我疯了。
我想创建一个脚本,对于名字以“John”开头的所有用户,创建一个包含用户用户名和名称的文本文件。
这是脚本:
Get-ADUser -Filter {name -like "John*"} -Property Name, SamAccountName |
ForEach-Object {
Get-ADUser -Filter {name -eq $_.Name} |
Select-Object -Property Name, SamAccountName |
Out-File "c:\PS\$_.Name.txt"
}
这可行,但是out-file(s)名称不是用户名,而是它的ADsPath; CN=John Doe,OU=Users,DC=contoso,DC=local.Name.txt
由于某种原因,$_.Name
无法在外部文件CMD-let中正确解析。 $_
正确引用了对象,但.Name
被解释为只是一个字符串
WriteHost $_.Name
有效,为什么不Out-File "C:\PS\$_.Name.txt"
?
答案 0 :(得分:1)
"$($_.Name)"
无法正常工作,因为您在字符串中 - PowerShell只展开变量,而不是您想要的表达式。为此,您需要$()
。 $oPM->SMTPDebug = 2;
$oPM->Debugoutput = 'html';
if ( !$oPM->send() ) echo "Mailer Error: " . $oPM->ErrorInfo;
else echo "Message sent!";
内的所有内容都被评估为单个表达式。有关详细信息,请参阅about_Quoting_Rules。
答案 1 :(得分:1)
只有变量在字符串中扩展,而不是属性。要完成这项工作,您需要使用$()
语法:
Out-File "c:\PS\$($_.Name).txt"
答案 2 :(得分:0)
只是这样做:
$
powershell应该如何知道变量停止在哪里并开始文本?这不是那么聪明。它正在寻找data.buffer.push("\n </div>\n </div>\n\n <div class=\"form-
row\">\n <div class=\"field w100\">\n <label for=\"primary_phone\">Primary phone (digits only) *</label>\n ");
hashContexts = {'type': depth0,'name': depth0,'required': depth0,'placeholder': depth0,'title': depth0};
hashTypes = {'type': "STRING",'name': "STRING",'required': "STRING",'placeholder': "STRING",'title': "STRING"};
options = {hash:{
'type': ("number"),
'name': ("primary_phone"),
'required': ("required"),
'placeholder': ("e.g. 12025556789 (country code + area code + local number without dashes)"),
'title': ("e.g. 12025556789 (country code + area code + local number without dashes)")
},contexts:[],types:[],hashContexts:hashContexts,hashTypes:hashTypes,data:data};
data.buffer.push(escapeExpression(((stack1 = helpers.input || depth0.input),stack1 ? stack1.call(depth0, options) : helperMissing.call(depth0, "input", options))));
data.buffer.push("\n </div>\n </div>\n\n <div class=\"form-row\">\n <div class=\"field w100\">\n <label for=\"confirm_phone\">Confirm Primary phone (digits only) *</label>\n ");
hashContexts = {'type': depth0,'name': depth0,'required': depth0,'placeholder': depth0,'title': depth0};
hashTypes = {'type': "STRING",'name': "STRING",'required': "STRING",'placeholder': "STRING",'title': "STRING"};
options = {hash:{
'type': ("number"),
'name': ("confirm_phone"),
'required': ("required"),
'placeholder': ("e.g. 12025556789 (country code + area code + local number without dashes)"),
'title': ("e.g. 12025556789 (country code + area code + local number without dashes)")
},contexts:[],types:[],hashContexts:hashContexts,hashTypes:hashTypes,data:data};
data.buffer.push(escapeExpression(((stack1 = helpers.input || depth0.input),stack1 ? stack1.call(depth0, options) : helperMissing.call(depth0, "input", options))));
符号并将其后面的单词视为变量。其余的都是文字。您可以将它与变量一起使用,但在这里您要将它与变量的属性一起使用。