小编BSA*_*AFH的帖子

Powershell中的Out-File -append不会生成新行并将字符串分解为字符

我试图了解这个cmdlet的一些奇怪的行为.

如果我在我创建的文本文件上使用"Out-File -append Filename.txt"并通过Windows上下文菜单输入文本,则该字符串将作为一系列空格分隔字符附加到该文件的最后一行.

所以:

"This is a test" | out-file -append textfile.txt
Run Code Online (Sandbox Code Playgroud)

会产生:T hisisatest

如果out-file创建文件,或者文本文件在追加之前没有文本,则不会发生这种情况.为什么会这样?

我还要注意,重复命令只会以相同的方式附加到同一行.我想它不会识别换行符或换行符终止符或由于更改编码而导致的东西?

powershell powershell-2.0

10
推荐指数
1
解决办法
8万
查看次数

绑定到Powershell中的其他活动目录ldap实例

我正在尝试连接到一些独立的LDAP存储(ADAM - Active Directory应用程序模式),使用一组特定的凭据进行绑定,但无法确定最佳方法.这是一个我希望可以工作的例子:

$ldapHost = New-Object System.DirectoryServices.DirectoryEntry("LDAP://{serverip}:{port}/dc=acme,dc=com","cn=myuser,dc=acme,dc=com","myPassw0rd")
$ldapQuery = New-Object System.DirectoryServices.DirectorySearcher
$ldapQuery.SearchRoot = $ldapHost
$ldapQuery.Filter = "(objectclass=*)"
$ldapQuery.SearchScope = "Base"
$ldapQuery.FindAll()
Run Code Online (Sandbox Code Playgroud)

这会让我:

Exception calling "FindAll" with "0" argument(s): "A local error has occurred.
"
At line:1 char:19
+ $ldapQuery.FindAll <<<< ()
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException    
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

$ldapHost = New-Object System.DirectoryServices.DirectoryEntry("LDAP://{myip}:{port}/dc=acme,dc=com")
$ldapHost.Username = "cn=myuser,dc=acme,dc=com"
Run Code Online (Sandbox Code Playgroud)

结果如下:

The following exception occurred while retrieving member "Username": "The specified directory            service attribute or valu
e does not …
Run Code Online (Sandbox Code Playgroud)

powershell ldap active-directory

7
推荐指数
1
解决办法
1万
查看次数

长字符串的Powershell脚本缩进

这个问题不必以这种方式解决,但是如果可以的话,它将为我节省很多宝贵的时间:

我一直在“一次记录”到一个日志文件。像这样:

$mylogentry = "A string of text containing information"
$mylogentry | out-file -append $log_file_path
$array_of_log_entries += $mylogentry
Run Code Online (Sandbox Code Playgroud)

数组的原因是我将它们加入了一个Send-Message正文中。

但是,当我有长引号时,我喜欢将它们分开。我可以使用反引号'`'来实现。我从来没有想过如果引号行嵌套在其他内容下,如何转义制表符。在每个缩进之前使用反引号不会删除该选项卡。

如果您浏览这样的网站:http : //technet.microsoft.com/zh-cn/magazine/hh475841.aspx,您会发现,即使他鼓励缩进,也不会为参数缩进代码。(您实际上可以在其中制表,因为引号之外的空白会被忽略。也许他只是在指出一个观点)

这是我所尝试的示例。(请注意,我无法在SE上复制格式。4个空格缩进似乎不再创建代码块)

if($somecondition){
   $mylogentry = "A string of really long text for non-technical people `
` ` continue to write text here. Additional info (technical): $techvar"
Run Code Online (Sandbox Code Playgroud)

由于我的缩进,该字符串在“人”和“继续”之间会有很大的差距。我当然可以从第0列开始“继续”,但是然后我的代码看起来更加愚蠢。

powershell

5
推荐指数
2
解决办法
5746
查看次数