Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 生成URL字符串_String_Url_Powershell - Fatal编程技术网

String 生成URL字符串

String 生成URL字符串,string,url,powershell,String,Url,Powershell,试图用一个变量和一个字符串建立一个链接,但我总是在它们之间留有一个空格。我怎样才能解决这个问题 $sub是来自sharepoint的SPWeb对象 Write Host$sub.Url”/default.aspx 结果: https://intra.mycompany/pages/sales /default.aspx将$sub变量放入字符串文本中,以便将其视为一个字符串: Write Host“$($sub.Url)/default.aspx” 请注意,您需要使用,因为您正在访问$sub的

试图用一个变量和一个字符串建立一个链接,但我总是在它们之间留有一个空格。我怎样才能解决这个问题

$sub
是来自sharepoint的
SPWeb
对象

Write Host$sub.Url”/default.aspx
结果:


https://intra.mycompany/pages/sales /default.aspx

$sub
变量放入字符串文本中,以便将其视为一个字符串:

Write Host“$($sub.Url)/default.aspx”
请注意,您需要使用,因为您正在访问
$sub
的属性


根据字符串的复杂程度,另一种方法是使用:

Write主机(“{0}/default.aspx”-f$sub.Url)

如果您有许多需要插入的变量,它可以使代码更清晰、更易于阅读

使用
URL
class'构造函数进行连接,而不是使用字符串操作。这样做的另一个好处是可以自动添加所需的任何斜杠

function Join-Uri {
    [CmdletBinding()]
    param (
        [Alias('Path','BaseUri')] #aliases so naming is consistent with Join-Path and .Net's constructor
        [Parameter(Mandatory)]
        [System.Uri]$Uri
        ,
        [Alias('ChildPath')] #alias so naming is consistent with Join-Path
        [Parameter(Mandatory,ValueFromPipeline)]
        [string]$RelativeUri
    )
    process {
        (New-Object -TypeName 'System.Uri' -ArgumentList $Uri,$RelativeUri)
        #the above returns a URI object; if we only want the string:
        #(New-Object -TypeName 'System.Uri' -ArgumentList $Uri,$RelativeUri).AbsoluteUri
    }
}

$sub = new-object -TypeName PSObject -Property @{Url='http://demo'}

write-host 'Basic Demo' -ForegroundColor 'cyan'
write-host (Join-Uri $sub.Url '/default.aspx')
write-host (Join-Uri $sub.Url 'default.aspx') #NB: above we included the leading slash; here we don't; yet the output's consistent

#you can also easily do this en-masse; e.g.
write-host 'Extended Demo' -ForegroundColor 'cyan'
@('default.aspx','index.htm','helloWorld.aspx') | Join-Uri $sub.Url | select-object -ExpandProperty AbsoluteUri
上面我创建了一个函数来包装这个功能;但您也可以轻松地执行以下操作:

[string]$url = (new-object -TypeName 'System.Uri' -ArgumentList ([System.Uri]'http://test'),'me').AbsoluteUri

相关文档链接:

为我糟糕的拼写和语法道歉。