Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append string value of an object to a string variable

Status
Not open for further replies.

Snakeroot

Technical User
Oct 4, 2006
112
0
0
US
I'm completely new at PS and I'm stuck. I feel like I'm right on the cusp of what I'm trying to do.

Running Windows Server 2008 R2, IIS 7.5 in 32bit mode

Here's my script:

Code:
foreach ($i in Get-WebApplication -Site "Default Web Site" | Select-Object -Property Path)
{  
    echo $i
    $url = "[URL unfurl="true"]http://www.mydomain.com/$i"[/URL]
    echo $url
    $wc = New-Object System.Net.WebClient
    $data = $wc.downloadstring($url)
    $title = [regex] '(?<=<title>)([\S\s]*?)(?=</title>)'
    write-output $title.Match($data).value.trim()
}

This is the response I'm getting:

Code:
  IIS7
 /AZ
[URL unfurl="true"]http://www.mydomain.com/@{path=/AZ}[/URL]
Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (404) Not Found."
At line:7 char:31
+     $data = $wc.downloadstring <<<< ($url)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

My question is: Why does "echo $i" produce the results I want (just /AZ), but when I try to append $i to the end of my website url, it looks like @{path=/AZ}?

How can I get the value of $url to be ?
 
I figured it out. I had to add -ExpandProperty Path, so the whole thing looks like this:

Code:
foreach ($i in Get-WebApplication -Site "Default Web Site" | Select-Object -Property Path -ExpandProperty Path)
{  
    echo $i
    $url = "[URL unfurl="true"]http://www.mydomain.com$i"[/URL]
    echo $url
    $wc = New-Object System.Net.WebClient
    $data = $wc.downloadstring($url)
    $title = [regex] '(?<=<title>)([\S\s]*?)(?=</title>)'
    write-output $title.Match($data).value.trim()
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top