Search code examples
iis-7wmiget-wmiobject

How to change LogFile.Directory property via WMI on a remote computer?


I was able to retrieve, using WMI, the value of LogFile.Directory for a specific IIS site located on a remote computer. Now I need to change the LogFile.Directory property - this is a step in an automation process - but I am hitting a wall. This is what I have so far, although it doesn't work.

    Write-Output "Making IIS connection to $($XmlCloudNodeFullyQualifiedDomainName)"
    $site = Get-WmiObject -ComputerName $XmlCloudNodeFullyQualifiedDomainName -Namespace "root/webadministration" -Class Site -Authentication 6 -Filter "Name = '$IISSiteName'"
    If ($site.Name -eq $IISSiteName) { 
        Write-Output "Found site $IISSiteName"
        Write-Output ("Existing log folder: " + $site.LogFile.Directory)
        $newLogFolder = "E:\" + $IISSiteName + "\logs"
        Write-Output ("Set IIS log folder to " + $newLogFolder)
        $site.LogFile.Directory = $newLogFolder
        $site.Put()
    } 

I don't get any errors. Simply the LogFile.Directory value does not change on the remote machine when I check in IIS Manager. I've read that I should be using Set-WMIInstance instead, so I tried:

    Write-Verbose "Making IIS connection to $($XmlCloudNodeFullyQualifiedDomainName)"
    $site = Get-WmiObject -ComputerName $XmlCloudNodeFullyQualifiedDomainName -Namespace "root/webadministration" -Class Site -Authentication 6 -Filter "Name = '$IISSiteName'"
    $sitePath = (Get-WmiObject -ComputerName $XmlCloudNodeFullyQualifiedDomainName -Namespace "root/webadministration" -Class Site -Authentication 6 -Filter "Name = '$IISSiteName'").__path
    If ($site.Name -eq $IISSiteName) { 
        Write-Output "Found site $IISSiteName"
        $newLogFolder = "E:\" + $IISSiteName + "\logs"
        Write-Output ("Existing log folder: " + $site.LogFile.Directory)
        Set-WmiInstance -Path $sitePath -argument @{LogFile.Directory = $newLogFolder}
        Write-Output ("Set IIS log folder to " + $newLogFolder)
    }

But this throws errors:

At E:\\Test.ps1:71 char:54
+             Set-WmiInstance -Path $sitePath @{LogFile.Directory = $newLogFolder}
+                                                      ~
Missing '=' operator after key in hash literal.
At E:\\Test.ps1:72 char:18
+             Write-Output ("Set IIS log folder to " + $newLogFolder)
+                  ~
Missing '=' operator after key in hash literal.
At E:\\Test.ps1:41 char:54
+     foreach ($FSMappingNode in $FSMappingNodesArray) {
+                                                      ~

Is there a way to change this particular value remotely or is it a read-only property? Any help would be greatly appreciated. Thanks,

// Francesco


Solution

  • After much tinkering, I finally found the way to change the LogFile.Directory of an IIS site remotely:

        Write-Output "Making IIS connection to $($XmlCloudNodeFullyQualifiedDomainName)"
        $site = Get-WmiObject -ComputerName $XmlCloudNodeFullyQualifiedDomainName -Namespace "root/webadministration" -Class Site -Authentication 6 -Filter "Name = '$IISSiteName'"
        $sitePath = $site.__path
        Write-Verbose $sitePath
        If ($site.Name -eq $IISSiteName) { 
            Write-Output "Found site $IISSiteName"
            Write-Verbose ("Existing log folder: " + $site.LogFile.Directory)
            $newLogFolder = "E:\" + $IISSiteName + "\logs"
            $siteLogFile = $site.LogFile
            $siteLogFile.Directory = $newLogFolder
            Set-WmiInstance -Path $sitePath -Arguments @{LogFile = $siteLogFile}
            Write-Output ("Set IIS log folder to " + $siteLogFile.Directory)
        }