0

I want to list (in Powershell) all the websites from IIS that have a name containing the keyword "UK". And then, I want to replace the physical path of the returned site with another one, for example "C:\Sites\Sitex".

What am I doing wrong here?

So far, I've tried this: get-website | select name,physicalpath | where { $_.name -like "*UK*" } which works fine to display all the sites in my IIS that have a name containing the keyword "UK". However, when I do this: get-website | select physicalpath | where { $_.name -like "*UK*" } it displays nothing!

I imagine it may be because I can't have in my "where" clause a field which is not present in my select! (in SQL this would work though). So, I would try to wrap up the initial query into a table and then select only a column from that table. Do nested queries as such exist in Powershell? Please help with syntax. Other solutions are also welcome.

I would then do something like

foreach ($site in $sites) {

Set-ItemProperty ???? -name physicalpath -value "C:\Sites\Sitex"

}

Any help with the syntax please and what parameter should be written after Set-ItemProperty. Many thanks in advance!

================= Also trying something like this in parallel:

Import-Module WebAdministration

dir IIS:\Sites # lists all sites

dir IIS:\Sites | ForEach-Object {

    # web site name $_Name 


    if($_.Name -like "*UK*") { 
    $_.Name 
    $_.PhysicalPath = "C:\Inetpub"

    }


}

No errors and it doesn't seem to work either. if($_.Name -like "*UK*") does not behave as expected as it outputs other sites that don't contain UK in their name. By the way, I am using Powershell_ISE.exe.

dear1
  • 103
  • 11

1 Answers1

1

I did not import the WebAdministration module for this answer.

I used the iis manager ui's configuration code generation capability to generate the statement used in the foreach loop (reference link later in answer), the rest of the script I customized.

$searchWildCard="*UK*"
$targetDirectory="<INSERT_NEW_DIRECTORY_PATH_HERE>"

 #Select the entire site object since i might make other config changes too.
 $sites=get-website | Where-Object { $_.name -like $searchWildCard }

foreach ($site in $sites) {
$filter=[string]::Format("system.applicationHost/sites/site[@name='{0}']/application[@path='/']/virtualDirectory[@path='/']",$site.name)

    # note the backticks below
    Set-WebConfigurationProperty `
    -pspath 'MACHINE/WEBROOT/APPHOST' `
    -filter $filter `
    -name "physicalPath" `
    -value $targetDirectory
}

# I tested without performing an iisreset. 
# One definitely needs to refresh the
#iis manager ui after running the script if it was open during execution.

#iisreset

Script Notes:

The comments in the script pointing out specific considerations.

The script points all sites to the same directory as with your foreach example.

Tested on:

Windows 10

Powershell 5

SO reference:

code generation using iis management ui

SO related:

changing-the-physical-path-of-an-iis-website-on-a-remote-machine-via-powershell

Community
  • 1
  • 1
Elmar
  • 1,236
  • 1
  • 11
  • 16