使用写入主机时输出中的不必要空间 [英] Unnecessary space in output when using Write-Host

查看:50
本文介绍了使用写入主机时输出中的不必要空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Foreach-Object 中使用 Write-Host 时,我在输出中得到了一个不必要的空间.

When I use Write-Host within a Foreach-Object, I get an unnecessary space in the output.

write-host "http://contoso.com/personal/"$_.ADUserName

输出:

http://contoso.com/personal/ john.doe

如何删除 john 之前的空格?Trim 不起作用,因为 $_.ADUserName

How can I remove the space before john? Trim does not work because there is no space in $_.ADUserName

推荐答案

发生这种情况是因为 Write-Host 将常量字符串和对象视为两个单独的参数——实际上并没有将字符串连接在一起你在叫它.而不是这样调用它,实际上连接字符串:

This is happening because Write-Host is considering your constant string and your object to be two separate parameters -- you aren't actually joining the strings together the way you're calling it. Instead of calling it this way, actually concatenate the strings:

write-host "http://contoso.com/personal/$($_.ADUserName)"

write-host ("http://contoso.com/personal/" + $_.ADUserName)

write-host ("http://contoso.com/personal/{0}" -f $_.ADUserName)

这篇关于使用写入主机时输出中的不必要空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆