Unnecessary space in output when using Write-Host

前端 未结 2 645
南笙
南笙 2021-01-19 07:56

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

write-host \"http://contoso.com/personal/\         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 08:26

    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)"
    

    or

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

    or

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

提交回复
热议问题